Jump to content

MYSQL NULL INT field returned to PHP as "0" string


Recommended Posts

I have a table with a field defined as "`offer_limit_remaining` INT( 5 ) NULL".

I have rows where mySQL shows that the field contains NULL, however mysql_fetch_assoc() returns an array with the field containing "string: "0".

 

If I execute a SELECT field IS NULL in phpMyAdmin it returns the desired rows, so the table does contain NULLs.

 

How can I retrieve the field using "SELECT *" and detect that it contains NULL?

 

php 5.2.9

mySQL Server version: 5.1.33-community

MySQL client version: 5.0.51a

You will want to use is_null to test if the value is null, that is for PHP.

 

For the SQL check you can easily do:

SELECT * FROM table_name WHERE value IS NULL

 

Or

 

SELECT * FROM table_name WHERE value IS NOT Null

 

If that does not answer your question, let me know.

 

Example of the php code:

<?php
// mysql connect info:

$res = mysql_query("SELECT * FROM table_name");

while ($row = mysql_fetch_assoc($res)) {
if (is_null($row['field_name'])) {
	echo "It is null!";
}
}

?>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.