Nukhba Arshad Nukhba Arshad - 3 months ago 10
JSON Question

PHP file suddenly stopped working, shows blank page

It was working fine just a while ago I was getting JSON response, which was an array of JSON objects now it shows a blank page ,I have no idea why! Can anyone identify what's the problem here?
here is the php code:

<?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);
}
?>

Answer

There are a few error in here

First you are mixing database access API's you cannot use mysql_ and mysqli_ calls. as you are connecting with mysqli_ stick with that

Secondly you do not use a return unless you are in a function. Used as you have it is killing the script the first one it comes across in the normal flow of the code, and of course NOT sending the data back to your browser.

<?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);
}
?>
Comments