【问题标题】:Why do I keep having this error "syntax error, unexpected end of file" [closed]为什么我一直有这个错误“语法错误,文件意外结束”[关闭]
【发布时间】:2015-05-26 10:13:01
【问题描述】:

我一直有这个错误,老实说我无法弄清楚。

    <?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 来获取它。

【问题讨论】:

  • 正确缩进你的代码。
  • 从'if($cnt>0){'=>'中删除'{' if($cnt>0)'

标签: php mysql


【解决方案1】:

您的 if 语句缺少结束 }

【讨论】:

    【解决方案2】:

    你忘记关闭if($cnt&gt;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();
    
    ?>
    

    【讨论】:

      【解决方案3】:

      你缺少 } for if($cnt>0){

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-03-17
        • 1970-01-01
        • 1970-01-01
        • 2013-02-03
        • 2012-11-08
        • 1970-01-01
        • 2016-05-12
        相关资源
        最近更新 更多