0

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?

RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
  • if you `var_dump($response)`, what do you get? – Liora Haydont Jan 25 '18 at 21:43
  • @LioraHaydont '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 " } }' correcty but is is still not echoing anything – GC Apps Lab Jan 25 '18 at 22:10
  • UTF-8 char may be causing you issues – RiggsFolly Jan 25 '18 at 23:38
  • [More info](https://stackoverflow.com/a/279279/6352706) on UTF-8. If you're not using it, make sure that you are using the same encoding everywhere. – Ben Harris Jan 25 '18 at 23:49
  • Yeah the problem was that the database wasn't using utf8 collation. Thank you very much @RiggsFolly and BenHarris ! – GC Apps Lab Jan 27 '18 at 00:06

2 Answers2

0

You don't need to create intermediate arrays or even use array_push. Try this, and use print_r or var_dump to debug it if you're not seeing what you expect.

$response["author"] = [
    "authorid"  => $result["authorid"],
    "name"      => $result["name"],
    "biography" => $result["biography"]
];
Ben Harris
  • 547
  • 3
  • 10
  • You could simplify that even further to `$response["author"] = $result` Although that does not answer the question either – RiggsFolly Jan 25 '18 at 22:04
0

The problem was that the database wasn't using utf8 collation. Credits to @RiggsFolly and @BenHarris for the suggestion in the comments! Thank you very much.