简单的Mysql选择查询不能在php中工作

时间:2017-03-05 16:59:29

标签: php mysql select mysqli

我是php的初学者,我正在尝试在android中开发一个搜索应用程序。我在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

1 个答案:

答案 0 :(得分:1)

第一个参数应为$con,如果您不在$cousine中使用通配符,则可以使用=like

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