dscss8996 2016-06-08 22:56
浏览 212
已采纳

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

So, I have written some query code returns the error:

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

I understand that this is caused to a boolean response from the query, and I have checked it out, and the Boolean returned is equal to true. So I don't see why there is no response with a data array instead... Here's my code:

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

There are three queries in the SQL - An update, insert, and selection.

How could I alter my query or PHP to return data, rather than a boolean? Thanks

  • 写回答

1条回答 默认 最新

  • duagfgfn1981 2016-06-09 09:55
    关注

    As suggested in a comment by spencer7593, my question was solved using a combination of mysqli_store_result, mysqli_free_result, and mysqli_next_result. The following is the function used to do this:

    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();
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题