2015-12-12 81 views
-4

我的sql查詢有什麼問題嗎?我總是得到這個錯誤「警告:mysqli_fetch_assoc()期望參數1是mysqli_result,布爾給定」。希望您能夠幫助我。MYSQL錯誤:警告:mysqli_fetch_assoc()期望參數1是mysqli_result,布爾給定

<?php 
include("connection.php"); 
$mysqli->query("SET NAMES 'utf8'"); 
$filter = "All"; 

if ($filter == "All"){ 
    $sql="SELECT * FROM city "; 
}else if($filter == "Alphabetically"){ 
    $sql="SELECT * FROM city order by cityName ASC"; 
}else if ($filter == "Region"){ 
    $sql="SELECT * FROM city order by region"; 
}else{ 
    echo "Error sql"; 
} 
$result=$mysqli->query($sql); 
while($e=mysqli_fetch_assoc($result)){ 
    $output[]=$e; 
} 

print(json_encode($output)); 
$mysqli->close(); 
    ?> 
+0

顯示你的'connection.php'的內容 – RiggsFolly

回答

-1

如果您的查詢有任何sql錯誤或其返回0行。 因此檢查第一個num_row計數

$result=$mysqli->query($sql); 
if(mysqli_num_rows($result) > 0){  
    while($e=mysqli_fetch_assoc($result)){ 
        $output[]=$e; 
       } 
     } 
     print(json_encode($output)); 
     $mysqli->close(); 

對於任何mysql錯誤。

$mysqli->error; 
4

我假設你正在使用的mysqli作爲二OO機制連接到數據庫是你所使用的幾乎所有的mysqli命令

但這裏

while($e=mysqli_fetch_assoc($result)){ 

你已經改變到處理函數調用。

而是使用

while($e = $result->fetch_assoc()){ 
相關問題