【问题标题】:mysqli and multi_query not workingmysqli 和 multi_query 不工作
【发布时间】:2012-01-25 09:02:50
【问题描述】:
 <?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();

?>

【问题讨论】:

  • 同时运行这两个查询有什么意义?
  • mysqli_multi_query -> 执行一个或多个由分号连接的查询。您需要在查询之间添加分号。我个人从未使用过 multi_query,所以我不能建议你过去
  • 这基本上是来自 multi_query PHP 文档页面 php.net/manual/en/mysqli.multi-query.php 的复制粘贴,对查询结构进行了细微更改。
  • true.. 我试着自己做这个例子
  • 你应该也检查过这一行...“执行一个或多个用分号连接的查询。”,同时复制粘贴

标签: php mysqli


【解决方案1】:

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

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

mysqli::multi_query

【讨论】:

  • 严格标准:mysqli::next_result() [mysqli.next-result]:没有下一个结果集。请在第 42 行调用 mysqli_more_results()/mysqli::more_results() 检查是否在 C:\xampp\htdocs\PoliticalForum2\test.php 中调用此函数/方法
  • 这么严格的标准怎么样?!?
  • 如果“严格标准”是指所需的分号,请记住您只是将两个字符串放在一起。当您的代码运行时,$query 看起来像 "SELECT query_title FROM administratorSELECT thread_id FROM threads"。把分号放在那里告诉 SQL 你已经完成了一个语句,并开始另一个。据它所知,您的意思是 "administratorSELECT" 是表的名称 ;-)
【解决方案2】:

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

使用 while ($mysql->next_result())...do 循环应该可以解决这个问题。 (一般来说:在数据库编程中,像你一样使用测试后循环是非常少见的)

如果代码是诗,我想成为莎士比亚!

【讨论】:

    【解决方案3】:

    你可以这样修复它:

    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);
    }
    

    【讨论】:

    • 请用英文写
    【解决方案4】:

    我得到了同样的答案。

    请在下面找到我的功能。

    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 类。请做一个,它会很好用 .

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-09-22
      • 2012-05-06
      • 2019-07-16
      • 2016-08-07
      • 2020-11-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多