php - get_result() 函数的替代品?

标签 php mysql mysqli

所以我的主机没有为 mysqli 安装 mysqlnd 驱动程序,所以我无法使用 get_result()函数(并且它不是安装 mysqlnd 驱动程序的选项)。以下查询的替代方案是什么,以便我不使用 get_result()

我知道我可以使用以下方法从数据库获取单个结果:

if ($stmt = $cxn->prepare("SELECT `count` FROM `numbers` WHERE `count_id` = ?")) {
    $stmt->bind_param('i', $count_id);
    $stmt->execute();
    $stmt->bind_result($count);
    $stmt->fetch();
    $stmt->close();
}

但是如果我必须从数据库中选择多个结果,循环遍历每个结果并获得另一个结果怎么办?

if ($stmt = $cxn->prepare("SELECT `count` FROM `numbers` WHERE `number` = ?")) {
    $stmt->bind_param('i', $number);
    $stmt->execute();
    $result = $stmt->get_result(); // uses get_result()
}

if ($result->num_rows) { // uses get_result()
    while ($row = $result->fetch_assoc()) { // uses get_result()
        if($stmt = $cxn->prepare("SELECT `value` FROM `values` WHERE `number` = ?")) {
            $stmt->bind_param('i', $row['count']);
            $stmt->execute();
            $stmt->bind_result($value);
            $stmt->fetch();
            $stmt->close();
        }
    }
}

我无法使用它,因为某些语句需要 get_result()我无法使用。

我有什么选择?

最佳答案

试试这个,

if ($stmt = $cxn->prepare("SELECT `count` FROM `numbers` WHERE `count_id` = ?")) 
{

    $stmt->bind_param('i', $count_id);
    $stmt->execute();
    $result = $stmt->fetchAll();

        if ( count($result) ) 
        {

            foreach($result as $row) 
            {

                print_r($row);

            }
        } 
        else 
        {

            echo "Nothing Returned.";

        }
    }

关于php - get_result() 函数的替代品?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21560141/

相关文章:

php - mysqli fetch() 不获取

MySQL INTO OUTFILE 查询问题

javascript - 空变量 PHP

php - 在 mPDF 生成的文档中使用字体

php如果用户名存在于数据库中将用户名加一

php - 在 SQL Select 中正确排序小数数据

mysql - 如何更快地删除重复项?

c# - 在 C# 中进行 mysqldump 备份不会检索数据

php - PHP 中的 Mysqli 资源作为参数或全局

php - WHILE 循环中的 mysqli 查询