2017-03-05 61 views
-3

我是一個初學者在PHP和我試圖在Android開發一個搜索應用程序。我正在使用MySQL和php在000webhost上的後端。我正在嘗試使用以下代碼檢索關於提供輸入美食的餐館的信息。簡單的Mysql選擇查詢不工作在PHP

<?php 
$con = mysqli_connect("localhost", "id023", "pass", "id023"); 

$cuisine = $_GET["cuisine"]; 

$response = array(); 

$result = mysqli_query("SELECT id, name, capacity, rate, feedback FROM restaurants WHERE cuisine1 LIKE '{$cuisine}'"); 


if (mysqli_num_rows($result) > 0) { 

    $response["restaurants"] = array(); 


    while ($row = mysqli_fetch_array($result)) { 

    $restaurant = array(); 
    $restaurant["id"] = $row["id"]; 
    $restaurant["name"] = $row["name"]; 
    $restaurant["capacity"] = $row["capacity"]; 
    $restaurant["rate"] = $row["rate"]; 
    $restaurant["feedback"] = $row["feedback"]; 

    array_push($response["restaurants"], $restaurant); 
} 

$response["success"] = 1; 

    echo json_encode($response); 
} 
else { 

$response["success"] = 0; 
$response["message"] = "No products found"; 


echo json_encode($response); 
}?> 

this is the table "restaurant" in the database

+5

使用的程序的方式'mysqli_query'要求第一個參數是鏈接http://php.net/manual/en/mysqli.query.php –

+1

另外,枚舉的列往往表明設計不佳 – Strawberry

+1

你應該是gett關於'mysqli_query()'缺少參數的錯誤。就像「mysqli_query()中的無效參數1,預期資源/鏈接/任何,得到了字符串」。 [你看到錯誤消息](http://stackoverflow.com/questions/1053424/how-do-i-get-php-errors-to-display)? – HPierce

回答

1

第一個參數應該是$con,你可以使用簡單的$cousine(和=,如果你不使用通配符like

$result = mysqli_query($con,"SELECT id, name, capacity, rate, feedback 
      FROM restaurants WHERE cuisine1 LIKE '$cuisine'"); 
+0

它的工作!謝謝! :d –