2016-02-23 49 views
0

我知道在論壇上有幾千個帖子,但沒有一個似乎幫助我解決問題。這裏是PHP代碼:PHP警告:mysqli_fetch_assoc()期望參數1是mysqli_result,字符串在......中用SELECT MAX給出

$countsql = "SELECT MAX(id) as count FROM art"; 
    $count = mysqli_query($link, $countsql); 
    if(!$count){ 
    echo "Error\n"; 
    echo "Code: ". mysqli_error($link); 
    exit; 
    } 

while ($row = mysqli_fetch_assoc($count)){ $count = $row["count"]; } 

    $ppp = 2; 
    $start = $count - (($page-1) * $ppp); 
    $end = $count - (($page-1) * 2 * $ppp); 

該問題出現在與mysqli_fetch_assoc一行。 任何幫助將不勝感激。

+0

事情是,它不會拋出任何東西。或者至少沒有任何東西出現在屏幕上 – aln447

+4

這可能在這種情況下沒有什麼區別,因爲你只返回一行,但是'while($ row = mysqli_fetch_assoc($ count)){$ count = $ row [ 「計數」]; }'當你迭代結果集時,重複使用同一個變量對你的結果集和你存儲結果集中的記錄通常是一個壞主意 –

+1

儘管當$ count不再是結果集,而是檢查第二次迭代時,字符串可能會導致一些問題 –

回答

5

你的問題是在這裏:

while ($row = mysqli_fetch_assoc($count)){ 
    $count = $row["count"]; 
} 

第二次迭代設置$count是一個字符串,使mysqli_fetch_assoc呼叫失敗。

因爲你只需要在你的結果集一排,你可以改變whileif

if ($row = mysqli_fetch_assoc($count)){ 
    $count = $row["count"]; 
} 

,或者甚至更好,不要使用相同的變量有兩個(截然不同)的目的。

$count_result = mysqli_query($link, $countsql); 
... 
if ($row = mysqli_fetch_assoc($count_result)){ 
    $count = $row["count"]; 
} 
+0

工作。謝謝! – aln447

相關問題