PHP文件突然停止工作,显示空白页面

时间:2016-08-16 17:58:36

标签: php json mysqli

前一段时间它工作得很好我得到了JSON响应,这是一个JSON对象数组现在它显示一个空白页面,我不明白为什么!任何人都可以确定这里的问题是什么?   这是php代码:

<?php


$host="localhost"; // Host name 
$username="root"; // Mysql username 
$password=""; // Mysql password 
$db_name="tourist"; // Database name 

// Connect to server and select databse.
$con = mysqli_connect("$host", "$username", "$password","$db_name")or die("cannot connect");


if($con){
    mysqli_set_charset($con ,'utf8');
    $qry = "SELECT * FROM places";
    $query=mysqli_query($con ,$qry);       
    if (!$query) {
         $message  = 'Invalid query: ' . mysql_error() . "\n";
         $message .= 'Whole query: ' . $qry;
         die($message);
    }

    $return_arr = array();
    $row_array = array();

    $num_rows = mysqli_num_rows($query);
    if ($num_rows > 0) {
        while ($r = mysqli_fetch_array($query)) {

            $row_array['place_id'] = $r['place_id'];
            $row_array['place_name'] = $r['place_name'];
            //echo "http://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; 
            //$row_array['image'] =  "http://localhost/tourist/". $r['db_image'];  //.'" alt=\"\" /"';

            $row_array['image'] = "tourist/".$r['db_image'];
            $row_array['des'] = $r['description'];

            header('Content-Type: application/json');

            array_push($return_arr,$row_array);
        }
        return json_encode($return_arr);
    } else { 
        $return_arr['place_name'] = 'ERRO - Place inexistente'; 
    }
    header('Content-Type: application/json');
    echo json_encode($return_arr);
    return json_encode($return_arr);

    mysqli_close($con);
} else { 
    $return_arr['place_name'] = 'ERRO - failure';
    echo json_encode($return_arr);
    return json_encode($return_arr);
}
?>

1 个答案:

答案 0 :(得分:0)

这里有一些错误

首先,您正在混合数据库访问API,但您无法使用mysql_mysqli_来电。当你用mysqli_与那个

连接时

其次,除非您在函数中,否则不要使用return。如果你正常使用它会在正常的代码流中杀死它遇到的第一个脚本,当然不会将数据发送回你的浏览器。

<?php


$host="localhost"; // Host name 
$username="root"; // Mysql username 
$password=""; // Mysql password 
$db_name="tourist"; // Database name 

// Connect to server and select databse.
$con = mysqli_connect("$host", "$username", "$password","$db_name")or die("cannot connect");


if($con){
    mysqli_set_charset($con ,'utf8');
    $qry = "SELECT * FROM places";
    $query=mysqli_query($con ,$qry);       
    if (!$query) {

        //$message  = 'Invalid query: ' . mysql_error() . "\n";

        $message  = 'Invalid query: ' . mysqli_error($con) . "\n";
        $message .= 'Whole query: ' . $qry;
        die($message);
    }

    $return_arr = array();
    $row_array = array();

    $num_rows = mysqli_num_rows($query);
    if ($num_rows > 0) {
        while ($r = mysqli_fetch_array($query)) {

            $row_array['place_id'] = $r['place_id'];
            $row_array['place_name'] = $r['place_name'];
            //echo "http://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; 
            //$row_array['image'] =  "http://localhost/tourist/". $r['db_image'];  //.'" alt=\"\" /"';

            $row_array['image'] = "tourist/".$r['db_image'];
            $row_array['des'] = $r['description'];

            header('Content-Type: application/json');

            array_push($return_arr,$row_array);
        }

        // This will be terminating the script
        //return json_encode($return_arr);

    } else { 
        $return_arr['place_name'] = 'ERRO - Place inexistente'; 
    }
    header('Content-Type: application/json');
    echo json_encode($return_arr);

    // not needed
    //return json_encode($return_arr);

    mysqli_close($con);
} else { 
    $return_arr['place_name'] = 'ERRO - failure';
    echo json_encode($return_arr);

    // Not needed
    //return json_encode($return_arr);
}
?>