2012-08-28 78 views
0
mysql_query($query,$con); 

     $query = "SELECT * FROM NW_WORLD;"; 
     $result = mysql_query($query, $con); 

     $i = 0; 
     $counter = count($result); 
     while($result) 
     { 
      $village = mysql_result($result, $i, "village"); 
      $player = mysql_result($result, $i, "player"); 

      echo "village: $village "; 
      echo "player: $player<br>"; 
      $i++; 
     } 

我想打印從Select語句中獲得的所有數據。此代碼正在工作,但在表格結束後,我收到了一些錯誤的數據。使用While循環打印數據直到表結尾

Warning: mysql_result() [function.mysql-result]: Unable to jump to row 6788 on MySQL result index 4 in C:\xampp\htdocs\debal\nw_check_exec.php on line 41
village: player:
Warning: mysql_result() [function.mysql-result]: Unable to jump to row 6789 on MySQL result index 4 in C:\xampp\htdocs\debal\nw_check_exec.php on line 40
Warning: mysql_result() [function.mysql-result]: Unable to jump to row 6789 on MySQL result index 4 in C:\xampp\htdocs\debal\nw_check_exec.php on line 41
village: player:
Warning: mysql_result() [function.mysql-result]: Unable to jump to row 6790 on MySQL result index 4 in C:\xampp\htdocs\debal\nw_check_exec.php on line 40
Warning: mysql_result() [function.mysql-result]: Unable to jump to row 6790 on MySQL result index 4 in C:\xampp\htdocs\debal\nw_check_exec.php on line 41
village: player:

請你能幫助我嗎?

+1

請不要使用'mysql_ *'函數編寫新代碼。他們不再被維護,社區已經開始貶低過程。相反,你應該瞭解準備語句和使用是'PDO'或'MySQLi'。 – alfasin

+0

$計數器=計數($結果);通過在資源上使用計數,您只能得到1的值。我在代碼中看不到這一點。 – c3cris

回答

2

你嘗試過:

mysql_query($query,$con); 

     $query = "SELECT * FROM NW_WORLD;"; 
     $result = mysql_query($query, $con); 


     while($data = mysql_fetch_assoc($result)**)** 
     { 

      echo "village: $data[village]"; 
      echo "player: $data[player]<br>"; 
     } 
+0

解析錯誤:語法錯誤,意外的T_ENCAPSED_AND_WHITESPACE,期待T_STRING或T_VARIABLE或T_NUM_STRING – debal

+0

我忘記了a)它被**包圍的那個**)也將它們取出。 – c3cris

+0

是的,我已經檢查過,但我'得到錯誤... – debal

1

一般情況下,我只用mysql_result如果我從一列一列(1×1),結果,抽取的字段。如果你有多個列和/或多行,我會建議你使用mysql_fetch_assoc來代替。

+0

hes的權利,就像我發佈的例子。 – c3cris