1

I'm trying to make a simple PHP script that fetches a table from my MySQL database and encodes the results in JSON, so I can use them later in Java.

This is my code:

<?php
    $servername = "localhost:3036";
    $username = "example_user";
    $password = "example_password";

    $conn = mysql_connect($servername, $username, $password);

    if(! $conn) {
        die("Could not connect: " . mysql_error());
    }

    $sql = "SELECT * FROM table_name";
    mysql_select_db("database_name");
    $retval = mysql_query($sql, $conn);

    if(! $retval) {
        die("Could not get data: " . mysql_error());
    }

    while($row = mysql_fetch_assoc($retval)) {
        $output[]=$row;
    }

    print(json_encode($output));
    mysql_close($conn);
?>

This just gives a blank page as output (error messages are set to display). However, if I change json_encode($output) to json_encode($output[0]) (or any other number within the array's bounds), the output becomes that one $row array.

This is probably a really stupid question, but after about 3 hours of research I'm at my wit's end. Thank you for any help.

hlh3406
  • 1,382
  • 5
  • 29
  • 46
Sheogorath
  • 31
  • 6
  • 4
    What PHP version are you using? `mysql_` functions do no longer exist. They've been deprecated for a few years and are now officially dead. You should use [mysqli](http://php.net/mysqli) or [PDO](http://php.net/pdo) instead. – Oldskool Aug 25 '16 at 08:25
  • 1
    Check the array variable by using print_r() or var_dump(). – Mayank Pandeyz Aug 25 '16 at 08:26
  • According to phpinfo(), I'm using v. 5.6.23-0+deb8u1. The MySQL part works fine, but I'll try changing it anyways, thanks. – Sheogorath Aug 25 '16 at 08:33
  • 1
    Use json_last_error to check if there were errors in json_encode. http://php.net/manual/en/function.json-last-error.php – Joni Aug 25 '16 at 08:34
  • A "500 Internal Server Error" status code (or a blank page) means that your script is throwing an error but you haven't configured PHP to display error messages. That's something you need to address before you go further because it's hard to code properly without the aid of error messages. The error reporting thumb rule is to show in development and log in production. As a starting point, I suggest you edit the system-wide `php.ini` file in the computer where you develop and tweak the `error_reporting` and `display_errors` directives ([details here](http://stackoverflow.com/a/5680885/13508)). – Álvaro González Aug 25 '16 at 08:34
  • This is what var_dump() returns: array(39) { [0]=> array(3) { ["id"]=> string(1) "1" ["spanish"]=> string(8) "cirujano" ["german"]=> string(7) "Chirurg" } [1]=> array(3) { ["id"]=> string(1) "2" ["spanish"]=> string(6) "recelo" ["german"]=> string(8) "Verdacht ... and so on – Sheogorath Aug 25 '16 at 08:34
  • do var_dump(json_encode($output)); and paste output here. –  Aug 25 '16 at 08:34
  • As I said in my post, error messages are enabled. I confirmed that by making a syntax error and it showed up. – Sheogorath Aug 25 '16 at 08:37
  • Maybe it's an issue with encoding and special characters. Are both, your database and your file encoding using UTF-8? – igorshmigor Aug 25 '16 at 08:39
  • This is the output of var_dump(json_encode($output)); Warning: json_decode() expects parameter 1 to be string, array given in /var/www/html/vocabulary.php on line 24 NULL – Sheogorath Aug 25 '16 at 08:39
  • how is this possible you are using json_encode() and output shows json_decode() ? –  Aug 25 '16 at 08:42
  • Possibly a duplicate of http://stackoverflow.com/q/9098507/318758 – Joni Aug 25 '16 at 08:42
  • I do have special characters in my database table. My file is definitely encoded in UTF-8. Unfortunately I don't know where to check if my database is as well. – Sheogorath Aug 25 '16 at 08:43
  • Thank you so much, I've got it to work. It was indeed a problem with the UTF-8 encoding. Using mysql_set_charset("utf8); resolved the problem. Thanks Joni, can I flag this as an answer or something? Sorry, I'm new to Stackoverflow... – Sheogorath Aug 25 '16 at 08:48
  • @Sheogorath you can also create a new answer to your own question, and mark it as accepted. It'll be easier to find for other people with the same issue. – MSpreij Aug 25 '16 at 08:56
  • @MSpreij Thanks, I've added an answer, but I need to wait 2 days to accept it. – Sheogorath Aug 25 '16 at 09:02

4 Answers4

2

User @Joni led me to the solution.

Adding mysql_set_charset("utf8") fixed my issue.

As mentioned in this post: Why is this PHP call to json_encode silently failing - inability to handle single quotes?.

Community
  • 1
  • 1
Sheogorath
  • 31
  • 6
0

Try

 echo json_encode($output) ;
Nikolay Mihaylov
  • 3,868
  • 8
  • 27
  • 32
Mohinder
  • 19
  • 2
0

It seems you have some utf8 character in your result set

add this statement before running your query

mysql_query('SET CHARACTER SET utf8');
Altaf Hussain
  • 1,048
  • 2
  • 12
  • 25
-1

Update

"mysql"

to

"mysqli"

and add

mysqli_set_charset($variável_de _conexão, 'utf8');

below the connection variable

Rob
  • 26,989
  • 16
  • 82
  • 98