Getting results of statement

让人想犯罪 __ 提交于 2019-12-25 08:00:02

问题


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.


回答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;

};


来源:https://stackoverflow.com/questions/41140462/getting-results-of-statement

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!