获得陈述

时间:2016-12-14 10:37:20

标签: php mysql mysqli

我制作了这个示例代码:

$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;

如何直接使用/ get $ dbname和$ dbpassword而不使用类似的东西:

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

我想直接使用$ dbname和dbpassword。 什么是最好的方法?我做错了什么。

1 个答案:

答案 0 :(得分:1)

我找到了答案:

我需要存储结果(存储结果(获取属性))

我需要获取结果(从准备好的语句中获取结果到绑定变量中)很好地没有while循环。

$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;

};