Hidde
Hidde

Reputation: 11931

Null value returned by mysql_fetch_assoc()

I am calling mysql_fetch_assoc($res) on a result of a query. The query is good, there are other results returned in the same row. The last column is always null. Why is this?

Code (has been abbreviated, so no comments on the style please):

$qy = "SELECT * FROM `entries` WHERE `dag`=".$_POST['day'];
        $res = mysql_query($qy, $sql);


        $d = '';
        $row = mysql_fetch_assoc($res);
        $d .= $row['text'];
        $e = json_encode($row);

        echo json_encode(array("status" => "success", "data" => $d, "error" => $e));

$d will equal null and $e will equal {"status":"success","data":null,"error":"{\"dag\":\"DATA\",\"afstand\":\"DATA\",\"tijd\":\"DATA\",\"max\":\"DATA\",\"tottijd\":\"DATA\",\"odo\":\"DATA\",\"van\":\"DATA\",\"naar\":\"DATA\",\"weer\":\"DATA\",\"text\":null}"} where DATA is the correct data. The text column is null.

The text column has the VARCHAR type, with a maximum of 5000 characters. The text in the field I am trying to retrieve is 1800 characters long.

Upvotes: 1

Views: 1791

Answers (1)

Hidde
Hidde

Reputation: 11931

I have found an answer to my own question of two years ago.

The problem was encoding: The text column contains text, which was not UTF-8 encoded. When I wrapped the code to fetch the text in a utf8_encode(), everything worked as expected.

Upvotes: 1

Related Questions