I have got the following PHP script connected to a MySQL database which fails:
error_reporting(E_ALL);
ini_set('display_errors', TRUE);
ini_set('display_startup_errors', TRUE);
// array for JSON response
$response = array();
// include db connect class
require_once __DIR__ . '/db_connect.php';
// connecting to db
$db = new DB_CONNECT();
// get a author from authors table
$result = mysql_query("SELECT authorid, name, biography FROM author WHERE authorid = 3");
if (!empty($result)) {
// check for empty result
if (mysql_num_rows($result) > 0) {
// success
$response["success"] = 1;
$result = mysql_fetch_array($result);
$author = array();
$author["authorid"] = $result["authorid"];
$author["name"] = $result["name"];
$author["biography"] = $result["biography"];
// user node
$response["author"] = array();
array_push($response["author"], $author);
// echoing JSON response
echo json_encode($response);
} else {
// no author found
$response["success"] = 0;
$response["message"] = "No author found";
// echo no users JSON
echo json_encode($response);
}
} else {
// no author found
$response["success"] = 0;
$response["message"] = "No author found";
// echo no users JSON
echo json_encode($response);
}
The program get results, but stops at the line array_push($response["author"], $author); (I managed to get it using echo, but the program doesn't actually seem to report any error). If I remove either the biography field (which is a TEXT field) from the query or remove the line $author["biography"] = $result["biography"]; the program returns to work. Therefore I thought that it might be caused by an error/empty biography return but I don't understand why it should return so as if the query is run inside the database a correct result is displayed and biography is not empty.
This is the response returned when the biography field is removed from the query:
{"success":1,"author":[{"authorid":"3","name":"Jane Austen","biography":null}]}
How do you think this problem is created and how could I solve it?
Thank you
EDIT: changed according suggestion of Ben Harris:
$result = mysql_fetch_array($result);
$response["author"] = [
"authorid" => $result["authorid"],
"name" => $result["name"],
"biography" => $result["biography"]
];
var_dump($response);
// echoing JSON response
echo json_encode($response);
Var_dump prints correctly
array(2) {
["success"]=> int(1)
["author"]=> array(3) {
["authorid"]=> string(1) "3"
["name"]=> string(11) "Jane Austen"
["biography"]=> string(41) "Jane Austen (1775 � 1817) was an English "
}
}
However it is still not echoing anything. Any idea why?