【问题标题】:How to get the resultset from a SELECT query using mysqli_multi_query()?如何使用 mysqli_multi_query() 从 SELECT 查询中获取结果集?
【发布时间】:2013-02-15 16:02:52
【问题描述】:

我的查询是:

CREATE TEMPORARY TABLE `hcaconsumptions_temp` (
    `DeviceID` INT (11) NOT NULL,
    `TimeStamp` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    `Consumption` FLOAT NOT NULL,
    `DeviceBrand` VARCHAR (255) CHARACTER
SET utf8 COLLATE utf8_unicode_ci NOT NULL,
 `SerialNumber` VARCHAR (255) CHARACTER
SET utf8 COLLATE utf8_unicode_ci NOT NULL
) ENGINE = MyISAM DEFAULT CHARACTER
SET = utf8 COLLATE = utf8_unicode_ci;

INSERT INTO hcaconsumptions_temp 
    (DeviceBrand, SerialNumber, `TIMESTAMP`, Consumption) 
    VALUES
    ('Adunos','24100008','2013-01-14 19:39:48','157'),
    ('Adunos','24100010','2013-01-14 18:50:38','134'),
    ...
    ('Adunos','24100019','2013-01-14 18:40:58','117'),
    ('Adunos','24100020','2013-01-14 18:42:22','74');

UPDATE hcaconsumptions_temp
SET DeviceID = (
    SELECT
        DeviceID
    FROM
        hcadevices
    WHERE
        hcadevices.DeviceBrand = hcaconsumptions_temp.DeviceBrand
    AND hcadevices.SerialNumber = hcaconsumptions_temp.SerialNumber
);

SELECT
    count(DeviceID)
FROM
    hcaconsumptions_temp
WHERE
    DeviceID = '0';

正如您在末尾看到的那样,有SELECT 查询,但我无法得到结果。

可能是因为之前有 3 个查询,所以它给出了错误。如何获得结果?

我的 PHP 代码是:

$test_result = mysqli_multi_query($link, $multi_test_query);
$count = mysqli_fetch_assoc($test_result);
print_r($count);

给出的警告是:

mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, boolean given

此代码给出 2 个错误:

$test_result = mysqli_multi_query($link, $multi_test_query);
$count = mysqli_fetch_assoc($test_result);
echo mysqli_error();
print_r($count);

警告:mysqli_fetch_assoc() 期望参数 1 为 mysqli_result,C:\Program Files (x86)\EasyPHP-12.1\www\App\php\post\excel_yukle 中给出的布尔值.php123 行

警告:mysqli_error() 需要 1 个参数,0 在 C:\Program Files (x86)\EasyPHP-12.1\www\App\php\post\excel_yukle.php 中给出 124 行

编辑:我试过这个:

/* execute multi query */
if (mysqli_multi_query($link, $multi_test_query)) {
    do {
        /* store first result set */
        if ($result = mysqli_store_result($link)) {
            while ($row = mysqli_fetch_row($result)) {
                printf("%s\n", $row[0]);
            }
            mysqli_free_result($result);
        }
        /* print divider */
        if (mysqli_more_results($link)) {
            printf("-----------------\n");
        }
    } while (mysqli_next_result($link));
}

结果是:

-----------------
-----------------
-----------------
46
<br />
<b>Strict Standards</b>:  mysqli_next_result(): There is no next result set. Please, call mysqli_more_results()/mysqli::more_results() to check whether to call this function/method in <b>C:\Program Files (x86)\EasyPHP-12.1\www\HCAWebApp\php\post\excel_yukle.php</b> on line <b>136</b><br />

【问题讨论】:

  • 哦,没有错误。我已经做到了。因为查询没有问题。查询有效。但我无法得到结果。

标签: php mysqli resultset mysqli-multi-query


【解决方案1】:

第一次编码尝试的错误是因为您将mysqli_multi_query() 的布尔结果存储为$test_result,然后尝试将$test_result 提供给mysqli_fetch_assoc()。您打算将查询结果发送到 fetching 函数,但这不是 multi_query 的方式——这与 mysqli_query() 的工作方式相比是可以理解的。

这是一个重要的报价from the manual

要从第一个查询中检索结果集,您可以使用 mysqli_use_result()mysqli_store_result()。所有后续查询结果都可以使用mysqli_more_results()mysqli_next_result()处理。

...此引用还涉及您第二次发布尝试的问题,该尝试仅检查mysqli_next_result($link)。明确遵循手册的说明意味着编写以下while 条件:

} while(mysqli_more_results($link) && mysqli_next_result($link));

OP 提供/选择的答案是无法找到解决方案的解决方法。这整个$i 业务应该被删除;你在任何mysqli docs pages 都看不到这种类型的工作。

在您正在运行的查询中,只有其中一个应该有记录集。

CREATE TEMPORARY TABLE //returns true/false. *structural, no rows in this case.

INSERT //returns true/false. *mysqli_affected_rows if desired.

UPDATE //returns true/false. *mysqli_affected_rows if desired.

SELECT //returns record set.  *max of 1 row in this case.

我提出的解决方案是:

if (mysqli_multi_query($link, $multi_test_query)) {
    do {
        if ($result = mysqli_store_result($link)) { // record set was generated for this query
            $satirsayisi = mysqli_fetch_row($result)[0];
            mysqli_free_result($result);
        }
    } while (mysqli_more_results($link) && mysqli_next_result($link));
}
if ($hata = mysqli_error($link)) {
    echo "Error / hata: $hata";  // don't display error messages when site is public
} else {
    echo "Number of lines / satır sayısı: $satirsayisi";
}

如果有任何错误,将回显$hata

如果您的 SELECT 查询成功,$satirsayisi 将是您的计数值回显到屏幕。

其他建议/要点:

从单值结果中获取:使用mysqli_fetch_row()[0] 可以在一行中完成工作;不需要while循环。

循环mysqli_multi_query():参考文档使用DO WHILE 循环,以便始终在第一次迭代时进入循环,并在每次迭代结束时检查条件语句。

如果任何查询出现任何错误,mysqli_multi_query 将停止执行后续查询——这就是函数的设计方式。

【讨论】:

    【解决方案2】:

    我已经解决了。

    $i = 1;
    if (mysqli_multi_query($link, $multi_test_query)) {
        while(mysqli_next_result($link)) {
            /* store first result set */
            if ($result = mysqli_store_result($link)) {
                while ($row = mysqli_fetch_row($result)) {
                    //printf("%s\n", $row[0]);
                    if($i==3) $satirsayisi = $row[0];
                }
                mysqli_free_result($result);
            }
            /* print divider */
            if (mysqli_more_results($link)) {
                //printf("-----------------\n");
            }
    
            $i = $i + 1;
            if($i>3) break;
        }
    }
    
    
    echo $satirsayisi;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-19
      • 1970-01-01
      • 2018-01-19
      • 1970-01-01
      • 2023-03-25
      • 1970-01-01
      相关资源
      最近更新 更多