2011-12-21 53 views
3
<?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(); 

?>的mysqli和multi_query不工作

它不工作..它不進入第一個,如果條件標識,如果它是一個multiquery .. 我有其他的問題,..why是multi_query()是有用..,

UPDATE:

嚴格的標準:mysqli的:: next_result()[mysqli.next-結果]:有 沒有下一個結果集。請叫 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(); 

?>

+0

是什麼在運行這些查詢2一起的地步? – 2011-12-21 14:53:39

+2

mysqli_multi_query - >執行一個或多個用分號連接的查詢。 您需要在查詢之間添加分號。我個人從來沒有使用multi_query所以我不能勸你過去那種 – JoshStrange 2011-12-21 14:54:40

+1

這是從multi_query PHP文檔頁面,http://php.net/manual/en/mysqli.multi-query.php,與基本複製麪食查詢結構的細微變化。 – 2011-12-21 14:55:30

回答

0

您在第一次查詢的結尾需要一個分號。

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

mysqli::multi_query

+0

嚴格標準:mysqli :: next_result()[mysqli.next-result]:沒有下一個結果集。請調用mysqli_more_results()/ mysqli :: more_results()來檢查是否在第42行的C:\ xampp \ htdocs \ PoliticalForum2 \ test.php中調用此函數/方法 – 2011-12-21 15:02:36

+0

該嚴格標準是什麼?!? – 2011-12-21 15:02:46

+0

如果用「嚴格標準」表示所需的分號,請記住您只是將兩個字符串放在一起。當你的代碼運行時,$ query看起來像*「SELECT query_title FROM administratorSELECT thread_id FROM threads」*。把分號放在那裏告訴SQL你已經完成了一條語句,然後開始另一條語句。據它知道,你的意思是*「administratorSELECT」*是表的名稱;-) – FakeRainBrigand 2011-12-21 16:11:58

0

爲什麼你得到這樣的警告的原因,完全是因爲你用做... while循環運行命令塊之後計算該條件。因此,當沒有更多結果時,循環的內容會再運行一次,產生該警告。

使用一段時間($ mysql-> next_result())... DO循環應該解決這個問題。 (在一般注意事項:使用後測試循環像你這樣是在數據庫編程相當少見)

如果代碼是詩,我想是莎士比亞!

-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。請製作一個,它會很好地工作 。

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

請用英語寫 – 2013-11-03 18:56:02