quantomworks quantomworks - 1 year ago 82
JSON Question

Some table values returning null?

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";

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

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download