2016-01-19 94 views
-2
成員函數mysql_fetch_assoc()對資源

誰能幫我,我對PHP的新生,我有model.php呼叫在

public function get_array_of_users(){ 
     $query = "SELECT login,email FROM users"; 
     $result = mysql_query($query); 
     if(!$result){ 
      exit(mysql_error()); 
     } 

     $row = array(); 
     for($i=0; $i<mysql_num_rows($result);$i++){ 
      $row[] = mysql_fetch_array($result,MYSQL_ASSOC); 
     } 

     return $row; 
    } 

功能,當我調用這個函數

$result = $m->get_array_of_users(); 
    while($row = $result->mysql_fetch_assoc()) { 
      ... 
     } 

提示錯誤 致命錯誤:調用一個成員函數mysql_fetch_assoc()對數組中

+0

foreach($ result as $ row){// your stuff} – devpro

+2

停止使用已棄用的mysql_ *函數。它們從PHP7中刪除。遷移到PDO並開始使用準備好的語句。如果您是PHP新手,那更是一個原因。 –

+0

非常感謝你 – DoctorDo

回答

1

您可以用foreach以此爲:

$result = $m->get_array_of_users(); 
foreach($result as $row){ 
    //your stuff 
} 

,如果你想使用關聯數組比使用:

mysql_fetch_assoc($result); // note that it's deprecated function. 

側面說明:

請使用mysqli_*PDO監守mysql_*已被棄用,在無法使用PHP 7.

+0

+1爲備註給他一個選擇,我還會在這一方面加上說明,他在將來處理用戶輸入時需要使用準備好的語句 – BRoebie

+1

非常感謝! – DoctorDo