dix.selken Posted August 8, 2010 Share Posted August 8, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/210099-check-null-values-with-php-or-like-variables-is-null-doesnt-help/ Share on other sites More sharing options...
ldb358 Posted August 8, 2010 Share Posted August 8, 2010 try: $my_query = str_replace("''",'null',"select * from fruits where flavor = '$flavor'"); Quote Link to comment https://forums.phpfreaks.com/topic/210099-check-null-values-with-php-or-like-variables-is-null-doesnt-help/#findComment-1096497 Share on other sites More sharing options...
fenway Posted August 8, 2010 Share Posted August 8, 2010 You'll have to check both conditions. Quote Link to comment https://forums.phpfreaks.com/topic/210099-check-null-values-with-php-or-like-variables-is-null-doesnt-help/#findComment-1096654 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.