Darkwoods Posted February 15, 2010 Share Posted February 15, 2010 im trying to echo data from mysql but i don't want to echo it if it is null/empty anyone have a good idea how to do it? something like this if $var != null echo '$var'; lol sorry i know im a tottaly noob thx Quote Link to comment https://forums.phpfreaks.com/topic/192189-if-mysql_field-not-null-echo-data/ Share on other sites More sharing options...
Darkwoods Posted February 15, 2010 Author Share Posted February 15, 2010 i tried this but still something is wrong it is not working <?PHP if($wineprice2 != NULL){ echo '<div class="price"> $' . $wineprice2 .'</div>'; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/192189-if-mysql_field-not-null-echo-data/#findComment-1012825 Share on other sites More sharing options...
premiso Posted February 15, 2010 Share Posted February 15, 2010 is_null or you can filter the SQL to not pull null data if that is an option: SELECT * FROM table WHERE col1 IS NOT NULL Quote Link to comment https://forums.phpfreaks.com/topic/192189-if-mysql_field-not-null-echo-data/#findComment-1012828 Share on other sites More sharing options...
Darkwoods Posted February 15, 2010 Author Share Posted February 15, 2010 is_null or you can filter the SQL to not pull null data if that is an option: SELECT * FROM table WHERE col1 IS NOT NULL well the thing is im trying to avoid some html if it is null Quote Link to comment https://forums.phpfreaks.com/topic/192189-if-mysql_field-not-null-echo-data/#findComment-1012834 Share on other sites More sharing options...
premiso Posted February 15, 2010 Share Posted February 15, 2010 Then use the is_null function on the variable... <?php if(!is_null($wineprice2)) { echo '<div class="price"> $' . $wineprice2 .'</div>'; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/192189-if-mysql_field-not-null-echo-data/#findComment-1012836 Share on other sites More sharing options...
Darkwoods Posted February 15, 2010 Author Share Posted February 15, 2010 this did the trick for me thanks for your help premiso <?PHP if($wineinfo == NULL){ echo '<br />'; } elseif($wineinfo != NULL){ echo '<p>(' . $wineinfo .')</p><br />'; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/192189-if-mysql_field-not-null-echo-data/#findComment-1012857 Share on other sites More sharing options...
Aro Posted February 16, 2010 Share Posted February 16, 2010 You don't need to do any of that stuff. Make less work for yourself and just edit if from the the query. premiso has the best idea. $query = mysql_query("SELECT * FROM table_name WHERE column1 IS NOT NULL"); Quote Link to comment https://forums.phpfreaks.com/topic/192189-if-mysql_field-not-null-echo-data/#findComment-1012920 Share on other sites More sharing options...
premiso Posted February 16, 2010 Share Posted February 16, 2010 You don't need to do any of that stuff. Make less work for yourself and just edit if from the the query. premiso has the best idea. $query = mysql_query("SELECT * FROM table_name WHERE column1 IS NOT NULL"); Aro, you may be partially correct, but say he wants to pull data from column1 and column2 and he wants to display column2 whether column1 is null or not (as it may not be a required field). The query would not allow for that, however testing if it is null would. Which is why I provided both solutions. Quote Link to comment https://forums.phpfreaks.com/topic/192189-if-mysql_field-not-null-echo-data/#findComment-1013215 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.