Monday 3 September 2018

Problems with PHP associative array and MySQL data

I recently added a new column to a MySQL database (featured), then added that column into my associative array. Rather than calling the data for the new column, I simply get 'null' in place of each entry, even after specifically requesting the column to be 'not null'.

I thought I may have been calling the data incorrectly, however, my array seems fine:
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
    array_push($blogs, array('title' => $row['title'], 'content' => $row['content'], 'featured' => $row['featured']));
}

And this is the response I get:
{"blogs":[{"title":"test-title","content":"test-content","featured":null}]}

I'm guessing this must be a MySQL issue, but I've no idea what it could be.

My guess is that the SQL query executed doesn't select this new column, or uses another name as the one you're expecting:
select title, content from ...

or
select title, content, feature as f from ...

0 comments:

Post a Comment