2014-09-04 50 views
0

我在MySQL中有一个简单的存储过程。这是代码。MySQLi multi_query next_result总是返回1

CREATE DEFINER = 'root'@'chl.rontel.ru' PROCEDURE `test`() 
    NOT DETERMINISTIC 
    CONTAINS SQL 
    SQL SECURITY DEFINER 
    COMMENT '' 
begin 
    select * from `mbus_log` where serial_num='12'; 
    select count(*) from mbus_log; 
end; 

在为MySQL返回6行表和行的总数SQL经理 - 293 所以我想从PHP得到这个值。这里是PHP代码:

public $mysqli; 

... 
    $this->mysqli=new mysqli($this->mysql_host,$this->mysql_user,$this->mysql_password,$this->mysql_db); 
    if ($this->mysqli->connect_error) 
    { 
     die('Connect Error (' . $mysqli->connect_errno . ') ' . $mysqli->connect_error); 
    } 
    $this->mysqli->query("SET NAMES UTF8"); 
... 
     $result= $this->mysqli->multi_query("call test();"); 
     $getReadingsByAddress=$this->mysqli->store_result(); 
     if ($this->mysqli->errno) 
     { 
      die('Select Error (' . $mysqli->errno . ') ' . $mysqli->error); 
     } 
     else 
     { 
      while($row = mysqli_fetch_object($getReadingsByAddress)) 
      { 
       $res.="<tr><td><a class='serialLink' href='./'>".$row->serial_num."</a></td><td>".$row->value."</td></tr>"; 
      } 
     } 
     $getReadingsByAddress->close(); 
     $res.=$this->mysqli->next_result(); 

$ mysqli-> store_result()工作正常,并返回正确的价值观,但next_result方法每次都返回1。我也试着这样做:

$result= $this->mysqli->multi_query("call test(); SELECT COUNT(*) from mbus_log;"); 

下一个结果也返回1。此外,当我试图var_dump($this->mysqli->next_result());它返回布尔(假),但more_results方法返回true,这是什么问题?

回答

0

我唯一的想法是尝试给count(*)列名IE count(*)AS MyCount。

+0

相同的结果 – 2014-09-04 09:56:45

0

解决:问题是我不明白mysqli-> next的工作原理。 这是正确的方式,因为它是在文档中:

$result= $this->mysqli->multi_query("call test();"); 
    if ($result) 
    { 
     do 
     { 
      $getReadingsByAddress=$this->mysqli->store_result(); 
      var_dump($getReadingsByAddress); 
      while($row = mysqli_fetch_object($getReadingsByAddress)) 
      { 
       var_dump($row); 
      } 

     if (!$this->mysqli->more_results()) 
     { 
      break; 
     } 
     if (!$this->mysqli->next_result()) 
     { 
      // report error 
      break; 
     } 
     } while (true); 
    }