Jump to content

check null values with php or like variables (is null doesn't help)


Recommended Posts

Hello people, sorry for this long post, I want to build a simple query to check some variables... let's have this mysql table (some random data):

 

fruitcolorflavor

appleredsweet

lemongreenbitter

melonyellow{null}

coconut{null}{null}

 

if you query

select * from fruits where flavor is null

you obviously get melon and cocounut, but what if we want to get invariable a fruit regardless of the null/not null condition

 

let's have this php code:

$flavor = 'sweet';
$my_query = "select * from fruits where flavor = '$flavor'";

$flavor = '';
$my_query = "select * from fruits where flavor = '$flavor'";

 

the first example retrieves successfully returns apple, but the second query does not return anything, thats because null is not equal to '' in mysql...

 

do you have any suggestion how to solve this? checking the variable to be null before building the query is not possible since my actual table has many attributes, and any of them can be null, also an ifnull like in

where ifnull(attribute, '') = '' 

 

although a working solution, it could require php code edition if the table is altered.

 

thank you

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.