Jump to content

[SOLVED] Dealing with a NULL value in a SELECT statement...


Recommended Posts

I just cannot do this...I've tried so many things that my fingers hurt from typing so many attempts...

 

I have a MYSQL table like so:

 

row    value

1        blue

2        red

3        NULL

4        purple

5        green

 

This is a simplified version of the code:

 

for ($num_row = 1; $num_row <= 3; $num_row++)
{
         $sql = "SELECT value
                 FROM $table
                 WHERE row = $num_row" ;

         $result = mysql_query($sql) ;
         
         $value = mysql_result($result,0) ;
         
         print $row . " -- " . $value . "<br>" ;
}

 

 

The process throws an error at the NULL value.  I have searched far & wide for the answer, but can't put my hands on it.  I have several books but can't find a reference to this situation.

 

What am I doing wrong?

 

Thanks for your help!!

 

I haven't tested this, but I'm pretty sure there is an answer using "IS NOT NULL" in your MySQL statement.

 

Example:

 

$sql = "SELECT value FROM $table WHERE row = $num_row AND IS NOT NULL" ;

 

I bet it's wrong since I have never had to use it, but at least you get the idea.

 

It should skip that row if the field is NULL.

 

 

[Edit]

Also try this!

$sql = "SELECT value FROM $table WHERE row <=> $num_row" ;

 

The "<=>" is supposedly a "NULL-safe" operator.

Thanks very much to your help, ProjectFear and PseudoEvolution...Very helpful.

 

As the result of implementing your suggestions, I discovered (read: stumbled upon, tripped over) the looping construct "CONTINUE," which was also very helpful.  I have some additional questions, but those are for another post!

 

Thanks again!!

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.