0

I have a database that stores a website's news articles. Each row contains a time, title, author, and a content field. Javascript handles the information passed from php and displays it via togglable buttons. For some reason even though I see the data in Mysql Workbench some content fields are returning null. Why would this be?

Here is my code..

    var json = <?php
    $servername = "sfxworks.net"; //Currently pulls from my webserver. Pass sql credentials or write a php file that I can include that wont show on github.
    $username = "foo";
    $password = "bar";
    $dbname = "foodbarmmmmmmmmm";

    $conn = new mysqli($servername, $username, $password, $dbname);

    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    } 

    $rows = array();

    $sql = "SELECT title, author, date, content FROM News";
    $result = $conn->query($sql);
    if ($result->num_rows > 0) {
        while($row = $result->fetch_assoc()) {
            $rows[] = $row;
        }
    } else {
        echo "0 results";
    }
    $conn->close();

    print json_encode($rows);

    ?>;

For some reason, after php processes this with error reporting on, I just get null values at random. In the same places, but random.

null values

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
sfxworks
  • 1,031
  • 8
  • 27

1 Answers1

0

There are some values in the table with interesting apostrophes..

After removing [ ’ ] from the string and replacing it with [ ' ] the value is no longer null.

sfxworks
  • 1,031
  • 8
  • 27
  • Look here: http://stackoverflow.com/questions/15097518/escaping-json-apostrophe-before-phps-json-encode-truncates . Or here: http://php.net/manual/en/function.json-encode.php . You maybe have to escape the special chars with JSON_HEX_APOS or some of the other flags. – zypro May 12 '17 at 07:26
  • 1
    I didn't test it but it might be a better solution to escape all special chars at one by addslashes() or the more specific mysqli_real_escape_string(). These functions will escape the characters that need to be quoted in database queries. – zypro May 12 '17 at 07:31