为什么我一直有这个错误“语法错误,意外的文件结束”

时间:2015-05-26 10:13:01

标签: php mysql

我一直有这个错误,说实话,我无法弄明白。

    <?php
$servername="localhost"; //replace with database hostname 
$username="root"; //replace with database username 
$password=""; //replace with database password 
$dbname="mybd"; //replace with database name

//create connection
$conn = new mysqli($servername, $username, $password, $dbname);

    //check connection
    if($conn->connect_error){
        die("Connection failed: ".$conn->$connect_error);
    }


$sql = "SELECT rest.img,rest.rest_name,horario.desc_hor, rest.descp, rest.rest_id FROM rest, type, horario WHERE rest.type_id = type.type_id AND rest.id_hor = horario.id_hor AND rest.type_id=1";
$result = $conn->query($sql);
$cnt = $result->num_rows;

$json = array();

if($cnt>0){
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {

            $row_array['rest_id'] = $row['rest_id'];
            $row_array['type_id'] = $row['type_id'];
            $row_array['rest_name'] = $row['rest_name'];
            $row_array['contacto'] = $row['contacto'];
            $row_array['id_hor'] = $row['id_hor'];
            $row_array['local'] = $row['local'];
            $row_array['descp'] = $row['descp'];
            $row_array['img'] = $row['img'];

            array_push($json_response,$row_array);

        }
    echo json_encode($json_response);
fclose($conn);
$conn->close();

?>

这个PHP基本上可以连接到我的mysql数据库并将表导出到JSON,所以我可以用Android Studio来解它。

3 个答案:

答案 0 :(得分:3)

if语句缺少结束}

答案 1 :(得分:0)

您忘了关闭if($cnt>0){条件

<?php
$servername="localhost"; //replace with database hostname 
$username="root"; //replace with database username 
$password=""; //replace with database password 
$dbname="mybd"; //replace with database name

//create connection
$conn = new mysqli($servername, $username, $password, $dbname);

    //check connection
    if($conn->connect_error){
        die("Connection failed: ".$conn->$connect_error);
    }


$sql = "SELECT rest.img,rest.rest_name,horario.desc_hor, rest.descp, rest.rest_id FROM rest, type, horario WHERE rest.type_id = type.type_id AND rest.id_hor = horario.id_hor AND rest.type_id=1";
$result = $conn->query($sql);
$cnt = $result->num_rows;

$json = array();

if($cnt>0){
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {

            $row_array['rest_id'] = $row['rest_id'];
            $row_array['type_id'] = $row['type_id'];
            $row_array['rest_name'] = $row['rest_name'];
            $row_array['contacto'] = $row['contacto'];
            $row_array['id_hor'] = $row['id_hor'];
            $row_array['local'] = $row['local'];
            $row_array['descp'] = $row['descp'];
            $row_array['img'] = $row['img'];

            array_push($json_response,$row_array);

        }

    echo json_encode($json_response);
}// need to be close
fclose($conn);
$conn->close();

?>

答案 2 :(得分:0)

对于if($ cnt&gt; 0){

你缺少}