PHP   发布时间:2019-11-21  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了php – MySQLI Prepared Statement:num_rows&fetch_assoc大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
下面是一些写得不好且严重误解的 PHP代码,没有错误检查.说实话,我正在努力让我的脑袋绕着php-> MySQLi函数的迷宫!有人可以提供一个示例,说明如何使用预准备语句收集关联数组中的结果,同时从$stmt获取行数?下面的代码是我正在玩的东西.我认为让我失望的是在store_result之后使用$stmt值然后尝试收集一个关联数组,我不太清楚为什么……
$mysqli = mysqli_connect($config['host'],$config['user'],$config['pass'],$config['db']);
$stmt = $mysqli->prepare("SELECT * FROM licences WHERE generated = ?");
$stmt->bind_param('i',$core['id']);
$result = $stmt->execute();
$stmt->store_result();

if ($stmt->num_rows >= "1") {

    while($data = $result->fetch_assoc()){ 
        //Loop through results here $data[] 
    }

}else{

    echo "0 records found";

}

我觉得有点厚颜无耻只是要求代码,但它是我的情况的工作演示,我觉得我需要最终了解实际发生了什么.太感谢了!

解决方法

没错,数据库功能有点奇怪.你会到达那里.

代码看起来有点不确定,但是它是如何工作的:

建立连接,准备一个语句,绑定参数并执行它,一切都很好.

$result = $stmt->execute(); //execute() tries to fetch a result set. Returns true on succes,false on failure.
$stmt->store_result(); //store_result() "binds" the last given answer to the statement-object for... reasons. Now we can use it!

if ($stmt->num_rows >= "1") { //Uses the stored result and counts the rows.

  while($data = $result->fetch_assoc()){ 
        //And here,the answer-object is turned into an array-(object)
        // which can be worked with nicely.
        //It loops trough all entries in the array.
  }

}else{

   echo "0 records found";
}

大佬总结

以上是大佬教程为你收集整理的php – MySQLI Prepared Statement:num_rows&fetch_assoc全部内容,希望文章能够帮你解决php – MySQLI Prepared Statement:num_rows&fetch_assoc所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。