0

I have made this example code:

$db = new mysqli("localhost","root","password","xxx");

$statement = $db->prepare("SELECT name, password FROM persons WHERE name=? LIMIT 1");

$statement->bind_param('s', "kevin");

$statement->execute();

if($statement->num_rows){

$statement->bind_result($dbname, $dbpassword);

$statement->free_result();

};

echo $dbname;
echo $dbpass;

How can use/get $dbname and $dbpassword directly without using something like:

while($statement->fetch()){
    echo $dbname;
}

I want to use $dbname and dbpassword directly. What is the best approach? What am I doing wrong.

Nomistake
  • 893
  • 2
  • 17
  • 32
  • 2
    Change `$num_rows` to `num_rows` – SuperDJ Dec 14 '16 at 10:52
  • @SuperDJ Indeed thats a mistake thanks. – Nomistake Dec 14 '16 at 10:56
  • But how to get and use the single row of results. maybe fetch_row or something? – Nomistake Dec 14 '16 at 10:58
  • Did you try Google? – Your Common Sense Dec 14 '16 at 11:51
  • @Your Commen Sense Of course, but get to many confusing results – Nomistake Dec 14 '16 at 11:56
  • 1
    Well, mysqli *is* confusing, so you should use PDO. But still, what's exactly confusing about results you get? – Your Common Sense Dec 14 '16 at 12:00
  • @YourCommonSense Wel, I always used the procedural but now i thought, i'll switch to oop. But this leaves me confused. I just need to query the db an get 1 row of result. Then, I would like to use this result as simple as possible... I hoped i could use 'bind_result' to get the result in $dbname an $dbpassword an the just use those to variables further in my php code... But it seems that its not the case – Nomistake Dec 14 '16 at 12:05
  • @YourCommonSense And for the moment i dont get any result... – Nomistake Dec 14 '16 at 12:06
  • @YourCommonSense I added some code to show what i want. Let's say i want to echo $dbname and $dbpass; – Nomistake Dec 14 '16 at 12:09
  • why don't you want to use the while loop to get the result? – SaschaP Dec 14 '16 at 12:14
  • 2
    maybe interesting? show how to use: http://stackoverflow.com/questions/18753262/example-of-how-to-use-bind-result-vs-get-result – Ryan Vincent Dec 14 '16 at 12:14
  • why don't you use fetch()? – Your Common Sense Dec 14 '16 at 12:29
  • @YourCommonSense Are you willing to rapidly alter my code so it works with fetch? For the moment my mind is like the far west... – Nomistake Dec 14 '16 at 12:31
  • 1
    @YourCommonSense Like in this treath: http://stackoverflow.com/questions/18753262/example-of-how-to-use-bind-result-vs-get-result they use a while statement, but i dont just need to echo the result, i"ll need to access them troughout my code... Do I need to put them in variables using the while loop? This seems so overkill for just one row of result – Nomistake Dec 14 '16 at 12:36
  • @RyanVincent I'll go to that article once again... but why do i need the while statement for one row of results. Do I need to put them in variables using the while loop? This seems so overkill for just one row of result – Nomistake Dec 14 '16 at 12:38
  • you do't need while statement for one row of results. but you still need to fetch – Your Common Sense Dec 14 '16 at 12:39
  • @YourCommonSense Ah, and please tell me how.. $row = $statement->fetch()? --- with $row['$dbname'] or with $row['dbname'] or just with $row['name'] – Nomistake Dec 14 '16 at 12:40
  • imo, The nice point about using a a while loop is that you can use the same code everywhere for one or or many rows. The overhead of the while loop is minimal. I wouldn't bother with the `num_rows` value, as it is more confusing than not, and just keep a count in the 'while loop' but that is just me.. – Ryan Vincent Dec 14 '16 at 12:52
  • 1
    @RyanVincent You pointed me in the right direction. Thanks. – Nomistake Dec 14 '16 at 13:04

1 Answers1

1

I found the answer:

I needed to store the result (Store the result (to get properties))

I needed to fetch the result (Fetch results from a prepared statement into the bound variables) Gladfully without a while loop.

$db = new mysqli("localhost","root","password","xxx");

$statement = $db->prepare("SELECT name, password FROM persons WHERE name=? LIMIT 1");

$statement->bind_param('s', "kevin");

$statement->execute();

$statement->store_result(); // this is what I was missing

if($statement->num_rows){

    $statement->bind_result($dbname, $dbpassword);

    $statement->fetch(); // this is what I was missing

    $statement->free_result();

    echo $dbname;

    echo $dbpass;

};
Nomistake
  • 893
  • 2
  • 17
  • 32