MySQLI 准备好的语句:num_rows &fetch_assoc [英] MySQLI Prepared Statement: num_rows & fetch_assoc

查看:27
本文介绍了MySQLI 准备好的语句:num_rows &fetch_assoc的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是一些写得不好且被严重误解的 PHP 代码,没有进行错误检查.老实说,我在 PHP->MySQLi 函数的迷宫中有点挣扎!有人可以提供一个示例,说明如何使用准备好的语句在关联数组中收集结果,同时还从 $stmt 获取行数?下面的代码是我正在玩的.我认为让我失望的是在 store_result 之后使用 $stmt 值,然后尝试收集一个关联数组,我不太确定为什么...

Below is some poorly written and heavily misunderstood PHP code with no error checking. To be honest, I'm struggling a little getting my head around the maze of PHP->MySQLi functions! Could someone please provide an example of how one would use prepared statements to collect results in an associative array whilst also getting a row count from $stmt? The code below is what I'm playing around with. I think the bit that's throwing me off is using $stmt values after store_result and then trying to collect an assoc array, and I'm not too sure why...

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

}

我只是要求提供代码就觉得有点厚颜无耻,但这是对我的情况的有效演示,我觉得我需要最终了解实际发生的事情.感谢一百万!

I feel a little cheeky just asking for code, but its a working demonstration of my circumstances that I feel I need to finally understand what's actually going on. Thanks a million!

推荐答案

我搜索了很长时间,但从未找到正确响应所需的文档,但我做了我的研究.

I searched for a long time but never found documentation needed to respond correctly, but I did my research.

$stmt->get_result() 为此目的替换 $stmt->store_result().所以,如果我们看到

$stmt->get_result() replace $stmt->store_result() for this purpose. So, If we see

$stmt_result = $stmt->get_result();
var_dump($stmt_result);

我们得到

object(mysqli_result)[3]
  public 'current_field' => int 0
  public 'field_count' => int 10
  public 'lengths' => null
  public 'num_rows' => int 8  #That we need!
  public 'type' => int 0

因此,我提出以下通用解决方案.(我包括我使用的错误报告)

Therefore I propose the following generic solution. (I include the bug report I use)

#Prepare stmt or reports errors
($stmt = $mysqli->prepare($query)) or trigger_error($mysqli->error, E_USER_ERROR);

#Execute stmt or reports errors
$stmt->execute() or trigger_error($stmt->error, E_USER_ERROR);

#Save data or reports errors
($stmt_result = $stmt->get_result()) or trigger_error($stmt->error, E_USER_ERROR);

#Check if are rows in query
if ($stmt_result->num_rows>0) {

  # Save in $row_data[] all columns of query
  while($row_data = $stmt_result->fetch_assoc()) {
    # Action to do
    echo $row_data['my_db_column_name_or_ALIAS'];
  }

} else {
  # No data actions
  echo 'No data here :(';
}
$stmt->close();

这篇关于MySQLI 准备好的语句:num_rows &fetch_assoc的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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