0

I have a database with columns that are allowed to be NULL.

Whenever I run a query against this database using $wpdb->get_results the columns that are NULL are always returned with an actual value; the number zero.

In my database NULL and zero actually have totally different meanings and I would like to be able to get the correct values from the database without Wordpress translating the returned value.

How can I select from the database so that NULL values are maintained?

1 Answer 1

0

There is no specific issue with retrieving null values from the database. They will come back as the PHP NULL value.

WordPress uses either the function mysqli_fetch_object or mysql_fetch_object to retrieve results from the database.

https://php.net/manual/en/function.mysql-fetch-object.php https://php.net/manual/en/mysqli-result.fetch-object.php

According to those respective pages:

Note: This function sets NULL fields to the PHP NULL value.

Are you using is_null() to check for the value? Or are you comparing the result to the NULL constant using the identity check (===)?

2
  • When I'm testing today the NULL values are retrieved just fine and the empty() check on the variables now behave correctly. I don't know what I did last night for it not to work, but it seems to have resolved itself. As it works now I blame working late at night :) Mar 31, 2015 at 9:07
  • Well.. Be careful with empty(). A int of zero is empty. You need to explicitly check for NULL if it's going to be different than zero.
    – Otto
    Mar 31, 2015 at 17:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.