2010-06-10 59 views
0

我完全是新的mysqli,我採取了生成的代碼,並根據我的需要進行了調整。mysqli只返回一行而不是多行

更新:

public function getServeurByName($string) { 

     $stmt = mysqli_prepare($this->connection, "SELECT * FROM $this->tablename where GSP_nom=?"); 
     $this->throwExceptionOnError(); 

     mysqli_stmt_bind_param($stmt, 's', $string);   
     $this->throwExceptionOnError(); 

     mysqli_stmt_execute($stmt); 
     $this->throwExceptionOnError(); 


     $rows = array(); 

     mysqli_stmt_bind_result($stmt, $row->idServ, $row->GSP_nom, $row->IPserv, $row->port, $row->tickrate, $row->membre, $row->nomPays, $row->finContrat, $row->actif, $row->timestamp, $row->type, $row->jeux, $row->slot, $row->ipClient, $row->essai, $row->reussite, $row->echec, $row->valide, $row->email); 

     while (mysqli_stmt_fetch($stmt)) { 
      $row->timestamp = new DateTime($row->timestamp); 
      $rows[] = $row; 
      $row = new stdClass(); 
      mysqli_stmt_bind_result($stmt, $row->idServ, $row->GSP_nom, $row->IPserv, $row->port, $row->tickrate, $row->membre, $row->nomPays, $row->finContrat, $row->actif, $row->timestamp, $row->type, $row->jeux, $row->slot, $row->ipClient, $row->essai, $row->reussite, $row->echec, $row->valide, $row->email); 
     } 

     mysqli_stmt_free_result($stmt); 
     mysqli_close($this->connection); 

     return $rows; 
    } 

問題,這個例子我承擔了回報模板只有一排,而不是所有記錄。

請問如何解決?

EDIT2:

print_r($row) show : 
stdClass Object ([idServ] => 0 [GSP_nom] => [IPserv] => [port] => 0 [tickrate] => 0 [membre] => [nomPays] => [finContrat] => [actif] => 0 [timestamp] => [type] => 0 [jeux] => 0 [slot] => 0 [ipClient] => [essai] => 0 [reussite] => 0 [echec] => 0 [valide] => 0 [email] =>) stdClass Object ([idServ] => 0 [GSP_nom] => [IPserv] => [port] => 0 [tickrate] => 0 [membre] => [nomPays] => [finContrat] => [actif] => 0 [timestamp] => [type] => 0 [jeux] => 0 [slot] => 0 [ipClient] => [essai] => 0 [reussite] => 0 [echec] => 0 [valide] => 0 [email] =>) stdClass Object ([idServ] => 0 [GSP_nom] => [IPserv] => [port] => 0 [tickrate] => 0 [membre] => [nomPays] => [finContrat] => [actif] => 0 [timestamp] => [type] => 0 [jeux] => 0 [slot] => 0 [ipClient] => [essai] => 0 [reussite] => 0 [echec] => 0 [valide] => 0 [email] =>) stdClass Object ([idServ] => 0 [GSP_nom] => [IPserv] => [port] => 0 [tickrate] => 0 [membre] => [nomPays] => [finContrat] => [actif] => 0 [timestamp] => [type] => 0 [jeux] => 0 [slot] => 0 [ipClient] => [essai] => 0 [reussite] => 0 [echec] => 0 [valide] => 0 [email] =>) stdClass Object ([idServ] => 0 [GSP_nom] => [IPserv] => [port] => 0 [tickrate] => 0 [membre] => [nomPays] => [finContrat] => [actif] => 0 [timestamp] => [type] => 0 [jeux] => 0 [slot] => 0 [ipClient] => [essai] => 0 [reussite] => 0 [echec] => 0 [valide] => 0 [email] =>) stdClass Object ([idServ] => 0 [GSP_nom] => [IPserv] => [port] => 0 [tickrate] => 0 [membre] => [nomPays] => [finContrat] => [actif] => 0 [timestamp] => [type] => 0 [jeux] => 0 [slot] => 0 [ipClient] => [essai] => 0 [reussite] => 0 [echec] => 0 [valide] => 0 [email] =>) 

/ 的print_r($行);顯示

Array ([0] => stdClass Object ([idServ] => 190 [GSP_nom] => bc2x [IPserv] => 85.234.212.181 [port] => 27015 [tickrate] => 100 [membre] => [nomPays] => Belgique [finContrat] => 20111231 [actif] => 1 [timestamp] => DateTime Object () [type] => 1 [jeux] => 1 [slot] => 34 [ipClient] => 81.240.221.92 [essai] => 6 [reussite] => 0 [echec] => 0 [valide] => 1 [email] =>) [1] => stdClass Object ([idServ] => 191 [GSP_nom] => bc2x [IPserv] => 85.234.212.185 [port] => 27028 [tickrate] => 100 [membre] => LowReal [nomPays] => Belgique [finContrat] => 20111127 [actif] => 1 [timestamp] => DateTime Object () [type] => 2 [jeux] => 1 [slot] => 11 [ipClient] => 81.240.221.92 [essai] => 6 [reussite] => 0 [echec] => 0 [valide] => 1 [email] =>) [2] => stdClass Object ([idServ] => 192 [GSP_nom] => bc2x [IPserv] => 91.121.27.79 [port] => 27033 [tickrate] => 100 [membre] => LowReal [nomPays] => Belgique [finContrat] => 20110915 [actif] => 1 [timestamp] => DateTime Object () [type] => 2 [jeux] => 1 [slot] => 11 [ipClient] => 81.240.221.92 [essai] => 6 [reussite] => 0 [echec] => 0 [valide] => 1 [email] =>) [3] => stdClass Object ([idServ] => 443 [GSP_nom] => bc2x [IPserv] => 193.105.73.249 [port] => 27069 [tickrate] => 100 [membre] => [nomPays] => France [finContrat] => 20111231 [actif] => 1 [timestamp] => DateTime Object () [type] => 2 [jeux] => 1 [slot] => 12 [ipClient] => 81.241.72.19 [essai] => 6 [reussite] => 2 [echec] => 6 [valide] => 1 [email] => [email protected]) [4] => stdClass Object ([idServ] => 468 [GSP_nom] => bc2x [IPserv] => 213.246.45.17 [port] => 27005 [tickrate] => 100 [membre] => admin [nomPays] => France [finContrat] => 20110302 [actif] => 1 [timestamp] => DateTime Object () [type] => 1 [jeux] => 1 [slot] => 12 [ipClient] => 127.0.0.1 [essai] => 0 [reussite] => 0 [echec] => 0 [valide] => 1 [email] => [email protected]) [5] => stdClass Object ([idServ] => 470 [GSP_nom] => bc2x [IPserv] => 213.246.45.17 [port] => 27005 [tickrate] => 100 [membre] => admin [nomPays] => Allemagne [finContrat] => 20110811 [actif] => 1 [timestamp] => DateTime Object () [type] => 2 [jeux] => 1 [slot] => 12 [ipClient] => 127.0.0.1 [essai] => 0 [reussite] => 0 [echec] => 0 [valide] => 1 [email] => [email protected])) 
+0

是mysqli只返回一行還是你的代碼? – 2010-06-10 00:40:22

+0

是的,他正在返回數據庫的第一行,就是這樣。 – 2010-06-10 00:42:35

回答

1
我沒有用

mysqli的自己,但對我來說,它看起來像這個問題是在這裏:

if(mysqli_stmt_fetch($stmt)) { 
    $row->timestamp = new DateTime($row->timestamp); 
    return $row; 
} else { 
    return null; 
} 

也許是這樣的(不能保證這個肯定會不經修改,它通過返回一個空數組傷了你0結果返回null的合同..按需要調整):

$arr = array(); 
while (mysqli_stmt_fetch($stmt)) { 
    // copy data from $row to $arr 
} 
return $arr; 

如果遇到$ ARR的元素的含量問題,也許討論這裏可以幫助:http://php.net/manual/en/mysqli-stmt.fetch.php

+0

取得一行'0'。任何軌道我需要做什麼改變?謝謝 – 2010-06-10 00:56:57

+0

調試,我會做一個print_r($行);在while循環和print_r($ arr)中;回來之前,並嘗試從那裏走。 – 2010-06-10 01:02:35

+0

我沒有設法調試這個。如果你有一個想法,我卡在這裏。 – 2010-06-10 16:25:58