aeris130 Posted March 6, 2006 Share Posted March 6, 2006 Suppose I assign a value to $my_line by selecting all data from a certain column in my MySQL table.I'm trying to get a certain output in case this column doesn't contain any data, but I don't know what syntax to use.[code]if ($my_line=="no_data_in_this_column")echo "no data"; elseecho "Current data is: $my_line"; [/code]What should I replace "no_data_in_this_column" with?Also, what is the syntax to simply not do anything at all in case $my_line is empty? Quote Link to comment Share on other sites More sharing options...
mem0ri Posted March 6, 2006 Share Posted March 6, 2006 Try something like:[code]$query="SELECT * FROM table";$qresult = mysql_query($query);if(mysql_num_rows <= 0){--Code to Use if no results--}else{--Code to Use if results--}[/code] Quote Link to comment Share on other sites More sharing options...
aeris130 Posted March 6, 2006 Author Share Posted March 6, 2006 Edit: Solved[!--quoteo(post=352137:date=Mar 6 2006, 01:16 PM:name=mem0ri)--][div class=\'quotetop\']QUOTE(mem0ri @ Mar 6 2006, 01:16 PM) [snapback]352137[/snapback][/div][div class=\'quotemain\'][!--quotec--]Try something like:[code]$query="SELECT * FROM table";$qresult = mysql_query($query);if(mysql_num_rows <= 0){--Code to Use if no results--}else{--Code to Use if results--}[/code][/quote]Can that be used on mulitple columns as well? Suppose I have a row with the following columns:My_table - ID - Name - PhoneAnd I want to display the following on screen:[code]if (ID == empty)echo "no IDS"; elseecho "ID is: $ID <br>"; if (phone == empty)echo "This person doesn't have a phone"; elseecho "$phone<p>"; [/code]How do I check a single column on a single row to see if its empty or not? Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted March 6, 2006 Share Posted March 6, 2006 The first responder's suggestion only checks if there are no rows returned from a particular query, it will not check if individual fields are null or blank.To do that, try:[code]<?php$q="SELECT * FROM table";$rs = mysql_query($query);while ($rw = mysql_fetch_asoc($rs)) { $tmp = array(); foreach($rw as $k => $v) switch ($k) { case 'ID': if ($v == '') $tmp[] = 'No ID entered' else $tmp[] = 'ID is ' . $v; break; case 'Phone': if ($v == '') $tmp[] = "This person doesn't have a phone"; else $tmp[] = 'Phone: ' . $v; break; } echo 'For ' . $rw['name'] . "<br>\n" . implode("<br>\n",$tmp). "<br>\n";}?>[/code]Note: I haven't checked the above for syntax errors.Ken Quote Link to comment Share on other sites More sharing options...
krixham Posted March 6, 2006 Share Posted March 6, 2006 Ken - THANK YOU. This is very similar to what I was trying to do with one of my scripts, but couldn't figure out where to put it. This response put me in the right area.Kathy Quote Link to comment 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.