加入收藏 | 设为首页 | 会员中心 | 我要投稿 安卓应用网 (https://www.0791zz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > PHP > 正文

php – MySQLI Prepared Statement:num_rowsfetch_assoc

发布时间:2020-05-25 09:17:03 所属栏目:PHP 来源:互联网
导读:下面是一些写得不好且严重误解的 PHP代码,没有错误检查.说实话,我正在努力让我的脑袋绕着PHP- MySQLi函数的迷宫!有人可以提供一个示例,说明如何使用预准备语句收集关联数组中的结果,同时从$stmt获取行数?下面的代码是我正在玩的东西.我认为让我失望的是在st

下面是一些写得不好且严重误解的 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";
}

(编辑:安卓应用网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读