mysqli_multi_query()-而不是返回数据返回 true

标签: sql MySQL PHP
发布时间: 2017/1/9 22:18:33
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

所以,我写了一些查询代码返回的错误︰

Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given

我明白这引起响应布尔查询,和我已签出,它返回的布尔值等于 true。所以,我不明白为什么会有一个数据数组也没有响应相反......这里是我的代码︰

$data = mysqli_multi_query($connection, 'UPDATE teams SET teams.teamViews = teams.TeamViews
 + 1 WHERE (teams.teamID, \''.$userToken.'\') NOT IN (SELECT teams_views.teamId,
teams_views.'.$viewType.' FROM teams_views) AND teams.teamUrl = \''.TEAM_URL.'\';
INSERT INTO teams_views (teamId, '.$viewType.') SELECT t.teamId, \''.$userToken.'\'
FROM teams t WHERE t.teamUrl = \''.TEAM_URL.'\' AND NOT EXISTS (SELECT \''.$userToken.'\'
FROM teams_views WHERE t.teamId = teamId);
SELECT * FROM teams WHERE teams.teamUrl = \''.TEAM_URL.'\';');
$dataRow = mysqli_fetch_array($data, MYSQLI_ASSOC);

有三个查询在 SQL-更新、 插入和选择。

如何能改变我的查询或 PHP 返回数据,而不是一个布尔值?谢谢你

解决方法 1:

如评论所说 spencer7593 ,我的问题解决组合使用 mysqli_store_resultmysqli_free_result ,和 mysqli_next_result 。以下是用来执行此操作的函数︰

function multi_queries($query, $numQueries) {
    $connection = new database_connection();
    $data = mysqli_multi_query($connection->connection, $query) or die(mysqli_error($connection->connection));
    $data = mysqli_store_result($connection->connection);
    if (sizeof($data) > 0) {
        $this->success = true;
        do {
            if ($result = mysqli_store_result($connection->connection)) {
                while ($row = mysqli_fetch_row($result)) {
                    $this->data[sizeof($this->data)] = $row;
                }
                mysqli_free_result($result);
            }
        } while (mysqli_next_result($connection->connection));
    }
    $connection->close_connection();
}
官方微信
官方QQ群
31647020