本文介绍了mysqli多个查询-设置变量产生布尔错误/如何跳过呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

得到以下简单查询,该查询通过phpmyadmin可以正常工作,但是当我将其添加到php网站时,没有返回结果,也没有错误/警告消息.如果我删除"SET @ N = -1;"然后就可以了.

Got the following simple query which works fine through phpmyadmin but when I add it to my php website no results are returned and no error/warning messages either. If I remove "SET @N=-1;" then it works fine.

<?php 
$db_connect = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD, true);
mysql_select_db(DB_NAME, $db_connect);

$test_query = mysql_query("SET @N=-1;SELECT `id`, (@N:=@N+1) AS `mycount` FROM `mydb`;");

for ($i = 0; $i <= mysql_num_rows($test_query)-1; $i++) {
   echo mysql_result($db_directorymap, $i, 0) . " " . mysql_result($db_directorymap, $i, 1) . "<br />";
}
?>

更新:我刚搬到mysqli,但是当然我仍然对mysql语句和mysqli_multi_query有问题.似乎当它运行查询的第一部分时,返回的结果为空,因此给出了布尔错误.我猜我必须跳过第一组结果,但是我不知道该怎么做?

UPDATE: I just moved to mysqli but of course I'm still having a problem with the mysql statement and mysqli_multi_query. It seems when it runs the first part of the query the results returned are empty thus a boolean error is given. I'm guessing I have to skip the first set of results but I don't know how to do that?

推荐答案

这是因为mysql_query函数将仅接受一个查询,但您却给了两个查询,并用分号隔开.尝试之一:

It's because the mysql_query function will only accept one query, but you've given it two, separated by a semicolon. Try either:

  1. 分别运行每个查询(不知道是否可行):

  1. Running each query separately (don't know if this will work):

mysql_query( "SET @N=-1" );
mysql_query( "SELECT `id`, (@N:=@N+1) AS `mycount` FROM `mydb`" );

  • mysqli multi_query 函数(或等同于PDO (如果有的话).

  • Using mysqli with the multi_query function (or a PDO equivalent if there is one).

    要回答更新的问题:请检查PHP手册页中的multi_query.我认为您将要使用mysqli::next_result.使用程序样式,类似这样:

    To answer your updated question: check the PHP manual page for multi_query. I think you'll want to use mysqli::next_result. Something like this, using procedural style:

    mysqli_multi_query($link, $query);
    mysqli_next_result($link);
    
    if ($result = mysqli_store_result($link)) {
        while ($row = mysqli_fetch_row($result)) {
            printf("%s\n", $row[0]);
        }
        mysqli_free_result($result);
    }
    

    这篇关于mysqli多个查询-设置变量产生布尔错误/如何跳过呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

  • 10-30 00:33