0

I'm new to PHP.I have been trying to get data from Database and convert them to json . However when I open the chrome console. It says 500 : Internal Server Error. Here is my PHP Code

<?php

function get_data(){
$link = mysqli_connect("localhost:8889","root2","root","EMPLOYEE");

if (!$link) {
    echo "Error: Unable to connect to MySQL." . PHP_EOL;
    echo "Debugging errno: " . mysqli_connect_errno() . PHP_EOL;
    echo "Debugging error: " . mysqli_connect_error() . PHP_EOL;
    exit;
}

echo "Success: A proper connection to MySQL was made! The my_db database is great." . PHP_EOL;
echo "Host information: " . mysqli_get_host_info($link) . PHP_EOL;

$sql = "SELECT * FROM people";

$result = mysqli_query($link,$sql);

$json_array = array();

while($row = mysql_fetch_array($result))
{
    $json_array[] = array(
        'id' => $row["id"],
        'name' => $row["name"]
    );
}
$json_array['name'] = "sarath";
$json_array['age'] = 19;
#echo $json_array;
return json_encode($json_array);
}
echo '<pre>';
print_r(get_data());
echo '</pre>';
?>

I get the following output at http://localhost:8888/peo.php. The json_array was not getting printed.

<pre>Success: A proper connection to MySQL was made! The my_db database is great.
Host information: localhost:8889 via TCP/IP

THE DATABASE IMAGE FROM PHPMyAdmin has been linked here

Dharman
  • 30,962
  • 25
  • 85
  • 135

1 Answers1

0

1) You open the connection using mysqli_connect but later use mysql_fetch_array. Change it to mysqli_fetch_array: those are different drivers.

2) Are you sure that you have got json addon installed? You can check it using phpinfo(); or just try to var dump: var_dump(json_encode(['something']));

Bartosz Pachołek
  • 1,278
  • 1
  • 8
  • 17