2015-07-20 88 views
-1

警告:mysqli_fetch_array()預計參數1被mysqli_result,串給出錯誤mysqli_fetch_array()預計參數1被mysqli_result,串給出

這是我的代碼誰能告訴我什麼是錯?

$result ="SELECT * FROM report" ; 
if(mysqli_query($cons, $result)) { 
echo(" 
<div class='sc'> 
<table id='to1' width='90%' border='0' cellspaceing='1' cellpadding='8' align='center'> 
<tr bgcolor='#9B7272'> 
<td > ID </td> 
<td > first_name </td> 
<td > last_name </td> 
<td > phone </td> 
<td > address </td> 
<td > email </td> 
<td > birthdate </td> 
<td > gender </td> 
<td > city </td> 
<td > dr_name </td> 

</tr> 

"); 
while($row = mysqli_fetch_array($result)) 
{ 
$ID   =$row['ID']; 
$first_name =$row['first_name']; 
$last_name =$row['last_name']; 
$phone  =$row['phone']; 
$address =$row['address']; 
$email  =$row['email']; 
$birthdate =$row['birthdate']; 
$gender  =$row['gender']; 
$city  =$row['city']; 
$dr_name =$row['dr_name']; 

echo " <tr bgcolor='#C7B8B8'> 
+4

你應該通過the_result_ mysqli_query的'()''到mysqli_fetch_array()',而不是查詢本身... – Sirko

回答

3

問題

你缺少如何將參數傳遞給mysqli_fetch_array()

解決方案

因此,該行:

if(mysqli_query($cons, $result)) { 

應該

if($res = mysqli_query($cons, $result)) { // assign the return value of mysqli_query to $res 

(FWIW,我會跟 $res = mysqli_query($cons, $result);然後做 if($res) {去。)

然後做

while($row = mysqli_fetch_array($res)) // pass $res to mysqli_fetch_array instead of the query itself 

爲什麼?

您給mysqli_fetch_array() - 作爲參數 - 包含您的查詢的string。這不是它的工作原理。您應該傳遞mysqli_query()的返回值。因此,你也可以寫下:while($row = mysqli_fetch_array(mysqli_query($cons, $result))) {}(但不建議,它只是向你展示它是如何工作的)。

1

正如評論中提到的那樣,您需要從查詢中設置一個$result,然後在循環中使用它。

$qry ="SELECT * FROM report"; 
$result = mysqli_query($cons, $qry); 
if ($result){ 
    while($row = mysqli_fetch_array($result)){ 
    } 
} 
1

,你可以這樣寫: -

$query = mysqli_query($cons,"SELECT * FROM items WHERE id = '$id'"); 
if (mysqli_num_rows($query) > 0) 
{ 
while($row = mysqli_fetch_assoc($query)) 
{ 
    $result=$row; 
} 
} 
相關問題