获取语​​句结果 [英] Getting results of statement

查看:40
本文介绍了获取语​​句结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了以下示例代码:

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;

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

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

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

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

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

推荐答案

我找到了答案:

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

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

我需要获取结果(将准备好的语句的结果获取到绑定变量中),并且没有while循环.

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;

};

这篇关于获取语​​句结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆