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

标签 php mysql sql

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

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

我知道这是查询的 bool 响应导致的,我已经检查过了,返回的 bool 值等于 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 以返回数据而不是 bool 值?谢谢

最佳答案

正如 spencer7593 的评论中所建议的,我的问题是使用 mysqli_store_resultmysqli_free_resultmysqli_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();
}

关于php - mysqli_multi_query() - 返回 true 而不是返回数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37714266/

相关文章:

php - 搜索具有可点击结果的用户,点击后将直接转到其个人资料

PHP - 使用 STARTTLS 和自签名证书的 Swiftmailer

mysql - 1 列中是否可以包含 1 个以上的日期?

mysql - Assets 库存数据库设计

mysql - 即使没有行,左连接也返回 null

java - sql、jsp、java - 为什么此插入语法会执行错误条件?

php - 用于 MySQL 和 PHP 的最佳排序规则是什么?

php - symfony - 学说 - 恢复我的 bdd 的所有数据,除了少数

php - 我想在 codeigniter 中加入 4 个表

sql - Firebird SQL : query slow due to coalesce or can it be rewritten