0

I have a database in phpmyadmin panel. And I want to see this databases as json format. When I paste my link to http://jsonlint.com/. It says Null. What's wrong with my php script?

<?php

$host = "**";
$user = "**";
$password = "**";
$db = "**";

$sql = "select * from product_info;";
$con = mysqli_connect($host,$user,$password,$db);

$result = mysqli_query($con,$sql);
$response = array();

while($row = mysqli_fetch_array($result)){
    array_push($response,array("name"=>$row[0],"email"=>$row[1],"mobile"=>$row[2]));
}

echo json_encode(array("server_response"=>$respose));
mysqli_close($con);

?>

2 Answers 2

0

You are missing a letter in the word "response" on the echo line. That's why you are getting NULL.

Replace

echo json_encode(array("server_response"=>$respose));

By this

echo json_encode(array("server_response"=>$response));

Also, you should set the header to application/json

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

Final code should be:

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

echo json_encode(array("server_response"=>$response));
Sign up to request clarification or add additional context in comments.

1 Comment

It's working now. Thank you very much for your attention. I have a question. Is there any PHP compiler for this spelling errors?
0

You have a typo ($respose) in the line

echo json_encode(array("server_response"=>$respose));

which should be

echo json_encode(array("server_response"=>$response));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.