mysqli和multi_query无法正常工作

时间:2011-12-21 14:52:22

标签: php mysqli

 <?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();

&GT;

它不起作用..它没有去第一个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();

&GT;

4 个答案:

答案 0 :(得分:0)

在第一个查询结束时需要一个分号。

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

mysqli::multi_query

答案 1 :(得分:0)

您收到此警告的原因仅仅是因为您使用了do ... while循环来运行命令块后评估条件。因此,当没有更多结果时,循环的内容再运行一次,产生该警告。

使用while($ mysql-&gt; next_result())... do循环应该解决这个问题。 (总的来说:在数据库编程中使用像你一样的测试后循环非常罕见)

如果代码是诗歌,我正试图成为莎士比亚!

答案 2 :(得分: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);
}

答案 3 :(得分:-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。请制作一个,它会很好用