24

我正在尝试这段代码:

    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()

而且我也试过:

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

这使得:

[Fri Jun 15 12:22:59 2012] [error] [client 127.0.0.1] PHP 致命错误:在 [myfile.php] 中的非对象上调用成员函数 fetch_assoc()

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

4

6 回答 6

27

事实上你可以很容易地做到这一点,你不能用mysqli_stmt对象做到这一点,你必须提取底层mysqli_result,你可以通过简单地调用来做到这一点mysqli_stmt::get_result()。注意:这需要 mysqlnd (MySQL Native Driver) 扩展,它可能并不总是可用。

然而,下面关于推荐 PDO 而不是 MySQLi 的观点仍然存在,这就是一个很好的例子:MySQLi 用户空间 API 没有意义。我花了几年间歇性地使用 MySQLi 来发现上述机制。现在,我承认将语句和结果集概念分开确实是有道理的,但是在那种情况下,为什么语句有fetch()方法呢?深思熟虑(如果您仍然坐在 MySQLi 和 PDO 之间的栅栏上)。

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

// 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>";
于 2012-06-15T10:09:31.190 回答
11

你可以做:

$stmt = $this->mysqli->prepare("SELECT * FROM `mytable` WHERE `rows1`=?");
$stmt->bind_param("i",$id);
$stmt->execute();
$result = $stmt->get_result();
$statistic = $result->fetch_all(MYSQLI_ASSOC);

$statistic包含二维数组中的所有结果。

*需要注意的是,此mysqli_fetch_all()功能仅适用于mysqlnd包。 http://php.net/manual/en/mysqli-result.fetch-all.php

于 2014-12-20T18:20:54.147 回答
4

我不喜欢 Mysqli,但你可以在准备时这样做。

mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$db = new mysqli('hostAddress', 'username', 'password', 'databaseName');
$db->set_charset('utf8mb4');

$userID = 2;

$stmt = $db->prepare("SELECT * FROM users WHERE ID = ?");
$stmt->bind_param("i", $userID);

// because the variable is bound by reference you can assign the value after binding, too
//$userID = 2;

$stmt->execute();

如果你想要结果;

$result = $stmt->get_result();
$user   = $result->fetch_array(MYSQLI_ASSOC); //one row

或多行

$users  = $result->fetch_all(MYSQLI_ASSOC);
于 2019-11-02T17:58:06.793 回答
0

使用准备好的语句,您不需要数组,但您可以自由使用它。使用 mysqli_stmt 的 bind_result 方法,你不会有任何麻烦。在 bind_result 中,您定义了变量,列的值应该按照您从数据库中请求它们的顺序存储。您可以使用本机变量或数组键来执行此操作。见php 文档 mysqli_stmt::bind_result

你问你的代码有什么问题是滥用 $result->execute。$result->execute() 返回一个布尔值,而不是您期望的实例 mysqli_result。请参阅php 文档 mysqli_stmt::execute

假设您的数据库表 `mytable` 具有以下列:id (int), field1 (varchar), field2 (date)

然后代码将如下所示:

// Initialize the array with some default values if you really want to use an array
$data = array('id'=>0,'field1'=>'','field2'=>'');
/* I recommend to explicitly define which columns you want to retrieve so you don't have to rely that the columns in the database are set in the desired order and on otherhand it doesn't matter if the database table has more columns that you want to retrieve, the code will still work as long as the columns with this names exist.*/
if ($result = $this->mysqli->prepare("SELECT `id`,`field1`,`field2` FROM `mytable` WHERE `rows1`=?"))
    {

        $result->bind_param("i",$id);
        $result->bind_result("iss", $data['id'], $data['field1'], $data['field2']);
        $result->execute();
        while($result->fetch())
        {

            $statistic[] = $data;

        }

        echo "<pre>";
        var_dump($statistic);
        echo "</pre>";
    }
于 2022-01-21T08:26:22.757 回答
-1

方法 execute() 返回 TRUE 或 FALSE 而不是结果集,因此不能使用 fetch_assoc 方法,因为它不存在于布尔值中。你需要的是

mysqli_fetch_assoc($result); //Use in association with query();
于 2012-06-15T10:25:23.797 回答
-2

没有尝试过,但使用以下内容更改您的 while 循环条件并再次测试

$data = $result->fetch()

谢谢

于 2012-06-15T10:13:42.317 回答