jalen Posted May 29, 2009 Share Posted May 29, 2009 how do you do an if statement if a field in mySQL is NULL do this if not do that? Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/160096-field-in-mysql-is-null/ Share on other sites More sharing options...
BobcatM Posted May 29, 2009 Share Posted May 29, 2009 <?php if($value = "") echo "Value is Null"; else echo "There is something in there"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/160096-field-in-mysql-is-null/#findComment-844669 Share on other sites More sharing options...
jalen Posted May 29, 2009 Author Share Posted May 29, 2009 I put that code you gave and it doesn't work. it is a name field that is created when created the table, but nothing is input (therefor NULL) to it when someone clicks on the submit button. Then I have another PHP files that is link with this first file, that is using the same table, but in this file it has the name field and is inserted when someone click the submitted button. now How do I use a if statement if the name field is null do this otherwise do that? Quote Link to comment https://forums.phpfreaks.com/topic/160096-field-in-mysql-is-null/#findComment-844683 Share on other sites More sharing options...
BobcatM Posted May 29, 2009 Share Posted May 29, 2009 Post Code I do not understand you. Quote Link to comment https://forums.phpfreaks.com/topic/160096-field-in-mysql-is-null/#findComment-844684 Share on other sites More sharing options...
BobcatM Posted May 29, 2009 Share Posted May 29, 2009 <?php $null = null; if($null == null){ echo "Yes, it is equivilent to null\n"; } if($null === null){ echo "Yes, it is null\n"; } if(isset($null)){ echo "Yes, it is set\n"; } if(empty($null)){ echo "Yes, it is empty\n"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/160096-field-in-mysql-is-null/#findComment-844687 Share on other sites More sharing options...
jalen Posted May 29, 2009 Author Share Posted May 29, 2009 yea is a long story. OK I have two php file both created the same table with the same field. one of the php file never insert the name field (for my purpose) when the submitted button is press there fore created a null field in mysql table. the other insert the name field so no null, etc. the purpose is when someone click submit with one php file, it creates a null field and when using the other it doesn't. So I wanted to distinguish with an if statement if is null do this else do that. I want to be able to do this within a while loop, which I already have the while loop set up: <input type="text" name="name" size="35"> while($info1 = mysql_fetch_array($comments_data)) { if($name = "") echo "Value is Null"; } Quote Link to comment https://forums.phpfreaks.com/topic/160096-field-in-mysql-is-null/#findComment-844690 Share on other sites More sharing options...
jalen Posted May 29, 2009 Author Share Posted May 29, 2009 could you do that if statement example using my name variable as I have: <input type="text" name="name" size="35"> Quote Link to comment https://forums.phpfreaks.com/topic/160096-field-in-mysql-is-null/#findComment-844693 Share on other sites More sharing options...
Ken2k7 Posted May 29, 2009 Share Posted May 29, 2009 Just do: <?php if (empty($name)) echo 'Value is null'; Quote Link to comment https://forums.phpfreaks.com/topic/160096-field-in-mysql-is-null/#findComment-844702 Share on other sites More sharing options...
Philip Posted May 29, 2009 Share Posted May 29, 2009 Or, done in a mysql query: SELECT IF(`field` IS NULL, 'Default Null Value', `field`) as `field` FROM `table` Would print the following: bob joe Default Null Value phil EDIT: I just realized for inserting data, not extracting it To insert with the if, check on PHP side Quote Link to comment https://forums.phpfreaks.com/topic/160096-field-in-mysql-is-null/#findComment-844706 Share on other sites More sharing options...
jalen Posted May 29, 2009 Author Share Posted May 29, 2009 I only got one part so far, the other part isn't printing out the name of whoever enter the name in the name field when press submit. while($info1 = mysql_fetch_array($comments_data)) { print" <table border=1 width=100%><td> <i>subject: </i><b> $info1[subject]</b><br> <font size=2 color=black>$info1[id]. $info1[COMMENTS] </font><br> <font size=2 color=gray> Posted by </font><font color=red><b><i>"; if (empty($name)){ echo 'Value is null'; }else{ echo "$info1[name]"; } print "</i></b></font><font size=2 color=gray> on $info1[date] </font> </td></table>"; } Quote Link to comment https://forums.phpfreaks.com/topic/160096-field-in-mysql-is-null/#findComment-844708 Share on other sites More sharing options...
Ken2k7 Posted May 29, 2009 Share Posted May 29, 2009 Change: if (empty($name)) { To: if (empty($info['name'])) { Quote Link to comment https://forums.phpfreaks.com/topic/160096-field-in-mysql-is-null/#findComment-844712 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.