mysqli和multi_query无法工作

4
 <?php

$mysqli=mysqli_connect("localhost","root","","politicalforum");

 $query="SELECT query_title FROM administrator";
  $query.="SELECT thread_id FROM threads";

 if($mysqli->multi_query($query))
 { 
    do
    {

        if($result=$mysqli->store_result())
        {
            while($row=$result->fetch_row())
            {
                printf("%s\n",$row[0]);
            }
            $result->free();
        }

        if($mysqli->more_results())
        {
            print("-------------------------------");
        }
    }while($mysql->next_result());
 }


$mysqli->close();

它不起作用...它没有进入第一个识别是否为多重查询的if条件语句..

我还有另一个问题,为什么multi_query()很有用..

更新:

严格标准: mysqli :: next_result() [mysqli.next-result]: 没有下一个结果集。请调用 mysqli_more_results() / mysqli :: more_results() 检查是否应该在 C:\xampp\htdocs\PoliticalForum2\test.php 的第42行调用此函数/方法。

解决方案:

 <?php

$mysqli=mysqli_connect("localhost","root","","politicalforum");

 $query="SELECT query_title FROM administrator;";
  $query.="SELECT thread_id FROM threads;";

 if($mysqli->multi_query($query))
 { 
    do
    {

        if($result=$mysqli->store_result())
        {
            while($row=$result->fetch_row())
            {
                printf("%s<br/>",$row[0]);
            }
            $result->free();
        }

        if($mysqli->more_results())
        {
            print("-------------------------------<br/>");
        }
        else
        {
            echo '<br/>';
        }
    }while($mysqli->more_results() && $mysqli->next_result());
 }


$mysqli->close();

?>


这两个查询一起运行有什么意义? - Your Common Sense
2
mysqli_multi_query -> 执行一个或多个由分号连接的查询。您需要在查询之间添加分号。我个人从未使用过multi_query,所以无法为您提供更多建议。 - JoshStrange
2
这基本上是从multi_query PHP文档页面http://php.net/manual/en/mysqli.multi-query.php复制粘贴过来的,只是对查询结构进行了轻微修改。 - Marc B
true。我尝试自己做这个例子。 - Dmitry Makovetskiyd
你也应该检查这一行... "执行一个或多个由分号连接的查询。",同时复制粘贴。 - Sudhir Bastakoti
哈哈..我应该看一下更新..当我结束执行查询时出现了错误。 - Dmitry Makovetskiyd
4个回答

0

你收到这个警告的原因很简单,就是因为你使用了一个 do...while 循环,在运行命令块后才评估条件。所以当没有更多结果时,循环的内容会再运行一次,导致出现该警告。

使用 while ($mysql->next_result())...do 循环可以解决这个问题。(总的来说:像你这样使用后测试循环在数据库编程中相当不常见)

如果代码是诗歌,我正在努力成为莎士比亚!


0
你可以像这样修复它:
if ($res)  {

    do {
      $mycon->next_result();   //// instead of putting it in a while, put it here

        if ($result = $mycon->store_result()) {

            while ($row = $result->fetch_row()) {

                foreach ($row as $cell)

                    $flag = $cell;
            }

            ///$result->close();
        }   
      $sale=$sale+1;

    }   while ($sale > 2);
}

1
Please write in english. - Lorenz Meyer

0

在第一个查询的末尾需要加上分号。

$query="SELECT query_title FROM administrator;";
$query.="SELECT thread_id FROM threads";

mysqli::multi_query


严格标准:mysqli::next_result() [mysqli.next-result]:没有下一个结果集。请调用mysqli_more_results()/mysqli::more_results()来检查是否应该调用此函数/方法,在C:\xampp\htdocs\PoliticalForum2\test.php的第42行。 - Dmitry Makovetskiyd
那个严格的标准怎么回事?!? - Dmitry Makovetskiyd
如果“严格标准”是指所需的分号,那么请记住您只是将两个字符串连接在一起。当代码运行时,$query看起来像“SELECT query_title FROM administratorSELECT thread_id FROM threads”。加入分号告诉SQL您已完成一个语句并开始另一个语句。就它所知道的而言,您的意思是“administratorSELECT”是表的名称;-) - Brigand

-1

我已经得到了相同的答案。

请在下面找到我的函数。

public function executeStoredProcedureMulti($strQuery)
{
    $yml    = sfYaml::load(sfConfig::get('sf_config_dir').'/databases.yml');
    $params = $yml['all']['doctrine']['param'];
    $dsnName = $params['dsn'];
    $arrDsn = explode(";",$dsnName);
    $hostName =  explode("=",$arrDsn[0])[1];
    $schemaName = explode("=",$arrDsn[1])[1];
    $this->dbconn = mysqli_connect($hostName, $params['username'], $params['password'], $schemaName);

    //return if connection was created successfully
    if($this->dbconn)
    {
        mysqli_set_charset($this->dbconn,"utf8");
        //return true;
    }
    else
    {
        $this->nErrorNumber = mysqli_connect_errno();
        $this->strErrorDesc = mysqli_connect_error();
        return false;   
    }   


    //check if connection exists
    if($this->dbconn)
    {

        /* close connection */
        //execute the query
        if($this->dbconn->multi_query($strQuery))
        {
            //create table array in dataset object
            $dataSet = array();
            do {
                //store first result set
                if ($result = $this->dbconn->store_result())
                {
                    //create data table passing it the column data
                    $dataTable = new CDataTable($result->fetch_fields());

                    //parse through each row
                    while ($row = $result->fetch_row())
                    {
                        $dataTable->AddRow($row);
                    }
                    $result->free();
                    $dataSet[] = $dataTable;
                }
                if (!$this->dbconn->more_results()) {
                    break;
                }
            } while ($this->dbconn->next_result());
            $this->dbconn->close();
            //return the complete dataset
            return $dataSet;
        }
        else//save the error to member variables
        {
            $this->nErrorNumber = $this->dbconn->errno;
            $this->strErrorDesc = $this->dbconn->error;
        }
    }
    return false;
}

这个可以工作了,需要创建一个CTableData类。请创建一个,它会很好地工作。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接