1

I'm trying this for the first time, so my code comes entirely from tutorials but it just doesn't work. I run PHP 7 with JSON 1.4.0 enabled.

I can print the resulting rows if I don't use json_encode, so my connection and the query are OK.

Error reporting is active but doesn't output anything either.

If I run the php file on my server, I just get a blank page.

Here's my code:

error_reporting(-1);

try {
    $conn=new PDO("mysql:host=server.com;dbname=theDB",username,password);
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $stmt = $conn->prepare('SELECT * FROM table');
    $stmt->execute();

    header('Content-type: application/json');
    echo json_encode($stmt->fetchAll(PDO::FETCH_ASSOC));



} catch(PDOException $e) {
        echo 'ERROR: ' . $e->getMessage();
}
  • 1
    Add `ini_set('display_errors', 1);` to your code or look in your error logs. – Jay Blanchard Apr 26 '16 at 14:23
  • 1
    @bub the `-1` in PHP turns on [all PHP errors](http://php.net/manual/en/function.error-reporting.php), but they may not be displayed. – Jay Blanchard Apr 26 '16 at 14:28
  • 1
    you're setting the exception mode AFTER you attempt the connection. by default pdo doesn't throw exceptions, so if the connection fails, it won't throw an exception, because exceptions haven't been enabled yet. – Marc B Apr 26 '16 at 14:29
  • Uh @MarcB, setting those attributes like that [is correct](http://php.net/manual/en/pdo.error-handling.php), no? Having the query execution code in the same try statement is less than desirable, but if the connection fails it should throw the error. – Jay Blanchard Apr 26 '16 at 14:31
  • 2
    Add [`echo json_last_error_msg();`](http://php.net/manual/en/function.json-last-error-msg.php) at the end. Add `;charset=utf-8` in the PDO connection string. – trincot Apr 26 '16 at 14:34
  • 1
    @JayBlanchard: d'oh. right... I need to go get some coffee. – Marc B Apr 26 '16 at 14:39
  • 1
    @JayBlanchard Thank heavens for that, I just had to go and run a test to prove it. Mark frightened the bejazus out if me – RiggsFolly Apr 26 '16 at 14:40
  • I'm only on my third cup @MarcB! – Jay Blanchard Apr 26 '16 at 14:41
  • Thanks @trincot, now I get this error: "Fatal error: Uncaught PDOException: SQLSTATE[HY000] [2019] Unknown character set in /home/.sites/123/site7948827/web/bedauerlicher_fetcher.php:8 Stack trace: #0 /home/.sites/123/site7948827/web/bedauerlicher_fetcher.php(8): PDO->__construct('mysql:host=mysq...', 'username', 'password') #1 {main} thrown in /home/.sites/123/site7948827/web/bedauerlicher_fetcher.php on line 8 – PectoralisMajor Apr 26 '16 at 14:42
  • It skeered me when I saw it @RiggsFolly! Thank goodness for coffee! – Jay Blanchard Apr 26 '16 at 14:43
  • What did you chnage to get that error? Because it looks like you removed the `try {} catch {}` block – RiggsFolly Apr 26 '16 at 14:46
  • @RiggsFolly I added `echo json_last_error_msg();` at the end of the file and `;charset=utf-8` at the end of the PDO string – PectoralisMajor Apr 26 '16 at 14:49
  • 1
    Add UTF-8 like this `$conn=new PDO("mysql:host=server.com;dbname=theDB;charset=UTF8",username,password);` in the DSN – RiggsFolly Apr 26 '16 at 14:53
  • @RiggsFolly YES! A GOD AMONGST CODERS! `charset=UTF8` instead of `charset=utf-8` did the trick! I'm getting my JSON object now! :) Thanks everyone! – PectoralisMajor Apr 26 '16 at 14:56
  • Do you hear that everyone, Just goes to show how wrong one developer can be about another! hahhhaha I can beat my chest and Holla like Tarzan too, on a good day. – RiggsFolly Apr 26 '16 at 14:58

2 Answers2

2

SOLUTION: (Thanks @trincot & @RiggsFolly) I had to change this line

$conn=new PDO("mysql:host=server.com;dbname=theDB",username,password);

to

$conn=new PDO("mysql:host=server.com;dbname=theDB;charset=UTF8",username,password);`
RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
1

json_encode() will not throw an exception on error, you have to verify the result yourself:

if (json_last_error()!==JSON_ERROR_NONE) {
    echo json_last_error_msg();
    exit;
}

Almost everytime this question comes here it's JSON_ERROR_UTF8:

Malformed UTF-8 characters, possibly incorrectly encoded

RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
Álvaro González
  • 142,137
  • 41
  • 261
  • 360