如何从准备好的语句中提取所有的assoc数组? [英] How to fetch all in assoc array from a prepared statement?

查看:58
本文介绍了如何从准备好的语句中提取所有的assoc数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试以下代码:

    if ($result = $this->mysqli->prepare("SELECT * FROM `mytable` WHERE `rows1`=?"))
    {

        $result->bind_param("i",$id);
        $result->execute();
        while ($data = $result->fetch_assoc())
        {

            $statistic[] = $data;

        }

        echo "<pre>";
        var_dump($statistic);
        echo "</pre>";
    }

但是会引发以下错误

[2012年6月15日星期五12:13:11] [错误] [客户端127.0.0.1] PHP致命错误: 调用[myfile.php]中未定义的方法mysqli_stmt :: fetch_assoc()

[Fri Jun 15 12:13:11 2012] [error] [client 127.0.0.1] PHP Fatal error: Call to undefined method mysqli_stmt::fetch_assoc() in [myfile.php]

我也尝试过:

if ($result = $this->mysqli->prepare("SELECT * FROM `mytable` WHERE `rows1`=?"))
    {

        $result->bind_param("i",$id);
        $rows = $result->execute();
        while ($data = $rows->fetch_assoc())
        {

            $statistic[] = $data;

        }

        echo "<pre>";
        var_dump($statistic);
        echo "</pre>";
    }

这使得:

[2012年6月15日星期五12:22:59] [错误] [客户端127.0.0.1] PHP致命错误: 在非对象中调用成员函数fetch_assoc() [myfile.php]

[Fri Jun 15 12:22:59 2012] [error] [client 127.0.0.1] PHP Fatal error: Call to a member function fetch_assoc() on a non-object in [myfile.php]

我还能做些什么来获得结果或做错什么?我需要来自DB的assoc数组,看起来像$data[0]["id"] = 1

What else I can do for getting result or what I doing wrong? I need the assoc array from DB looking like $data[0]["id"] = 1

推荐答案

实际上,您可以轻松完成此操作,而 mysqli_stmt::get_result() .注意:这需要mysqlnd(MySQL本机驱动程序)扩展名,该扩展名可能并不总是可用.

In fact you can do this quite easily, you just can't do it with the mysqli_stmt object, you have to extract the underlying mysqli_result, you can do this by simply calling mysqli_stmt::get_result(). Note: this requires the mysqlnd (MySQL Native Driver) extension which may not always be available.

但是,关于在MySQLi上推荐PDO的以下观点仍然存在,这是一个主要的示例:MySQLi用户界面API没有意义.我花了几年时间间歇性地与MySQLi合作,以发现上面概述的机制.现在,我承认将语句和结果集概念分开确实有意义,但是在那种情况下,为什么语句具有fetch()方法?值得深思(如果您仍然坐在MySQLi和PDO之间的篱笆上).

However, the point below about recommending PDO over MySQLi still stands, and this is a prime example of why: the MySQLi userland API makes no sense. It has taken me several years of intermittently working with MySQLi for me to discover the mechanism outlined above. Now, I'll admit that separating the statement and result-set concepts does make sense, but in that case why does a statement have a fetch() method? Food for thought (if you're still sitting on the fence between MySQLi and PDO).

为完整起见,这是一个(简单地)基于问题原始代码的代码示例:

For completeness, here's a code sample based (loosely) on the original code in the question:

// Create a statement
$query = "
    SELECT *
    FROM `mytable`
    WHERE `rows1` = ?
";
$stmt = $this->mysqli->prepare($query);

// Bind params and execute
$stmt->bind_param("i", $id);

// Extract result set and loop rows
$result = $stmt->get_result();
while ($data = $result->fetch_assoc())
{
    $statistic[] = $data;
}

// Proof that it's working
echo "<pre>";
var_dump($statistic);
echo "</pre>";

这篇关于如何从准备好的语句中提取所有的assoc数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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