【问题标题】:Getting results of statement获取语​​句结果
【发布时间】:2017-04-29 15:46:04
【问题描述】:

我已经做了这个示例代码:

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

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

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

【问题讨论】:

  • $num_rows 更改为num_rows
  • 嗯,mysqli 令人困惑,所以你应该使用 PDO。但是,您得到的结果到底有什么令人困惑的地方?
  • 也许很有趣?展示如何使用:stackoverflow.com/questions/18753262/…
  • @YourCommonSense 就像在这篇文章中一样:stackoverflow.com/questions/18753262/… 他们使用了一个 while 语句,但我不只需要回显结果,我需要在我的代码中访问它们......我需要使用 while 循环将它们放入变量中?这对于仅一行结果来说似乎太过分了
  • @RyanVincent 你给我指出了正确的方向。谢谢。

标签: php mysql mysqli


【解决方案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;

};

【讨论】:

    猜你喜欢
    • 2023-03-10
    • 1970-01-01
    • 1970-01-01
    • 2021-07-29
    • 2015-10-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-03
    相关资源
    最近更新 更多