sudduth66 Posted May 31, 2011 Share Posted May 31, 2011 cant figure out how to get this script to post a message if no results are found. please help to new at this. [attachment deleted by admin] Quote Link to comment https://forums.phpfreaks.com/topic/238015-how-to-get-an-null-echo-reslut/ Share on other sites More sharing options...
coolcam262 Posted May 31, 2011 Share Posted May 31, 2011 You almost had it but instead it is: if (mysql_num_rows($sql) > 0){ // Process hamsters }else{ echo 'There are no hamsters in the field! Feel free to let the cat out.'; } mysql_num_rows() uses the result of the sql. You were just using $term which was the result of a post. Quote Link to comment https://forums.phpfreaks.com/topic/238015-how-to-get-an-null-echo-reslut/#findComment-1223018 Share on other sites More sharing options...
sudduth66 Posted May 31, 2011 Author Share Posted May 31, 2011 it doesn't throw an error now but it still doesn't display anything. Thank you for the input i am much closer. [attachment deleted by admin] Quote Link to comment https://forums.phpfreaks.com/topic/238015-how-to-get-an-null-echo-reslut/#findComment-1223064 Share on other sites More sharing options...
fugix Posted May 31, 2011 Share Posted May 31, 2011 using the code that coolcam provided, the only way that the code wouldn't work is if your num_rows does not equal 0 Quote Link to comment https://forums.phpfreaks.com/topic/238015-how-to-get-an-null-echo-reslut/#findComment-1223065 Share on other sites More sharing options...
wildteen88 Posted May 31, 2011 Share Posted May 31, 2011 You have place the code coolcam262 suggested in wrong place. The if (mysql_num_rows($sql) > 0){ line should go before the while loop Change while ($row = mysql_fetch_array($sql)){ echo 'box_num: '.$row['box_number']; echo '<br/> dept: '.$row['Department']; echo '<br/> company: '.$row['Company']; echo '<br/> status: '.$row['status']; echo '<br/> location: '.$row['location']; echo '<br/> description: '.$row['box_desc']; if (mysql_num_rows($sql) > 0){ } else{ echo 'No Boxes available please use your back button to select a new box.'; } echo '<br/><br/>'; } To if (mysql_num_rows($sql) > 0) { while ($row = mysql_fetch_array($sql)) { echo 'box_num: '.$row['box_number']; echo '<br/> dept: '.$row['Department']; echo '<br/> company: '.$row['Company']; echo '<br/> status: '.$row['status']; echo '<br/> location: '.$row['location']; echo '<br/> description: '.$row['box_desc']; } } else { echo 'No Boxes available please use your back button to select a new box.'; } Quote Link to comment https://forums.phpfreaks.com/topic/238015-how-to-get-an-null-echo-reslut/#findComment-1223068 Share on other sites More sharing options...
phpJoeMo Posted May 31, 2011 Share Posted May 31, 2011 Try this code: <?php mysql_connect ("localhost", "3333","33333333") or die (mysql_error()); mysql_select_db ("Customers"); $term = (isset($_POST['term'])?$_POST['term']:null); $sql = mysql_query("select * from storage where box_number = '$term'"); $row = mysql_fetch_array($sql); if(count($row)){ while ($row){ echo 'box_num: '.$row['box_number']; echo '<br/> dept: '.$row['Department']; echo '<br/> company: '.$row['Company']; echo '<br/> status: '.$row['status']; echo '<br/> location: '.$row['location']; echo '<br/> description: '.$row['box_desc']; } } else { echo 'There are no hamsters in the field! Feel free to let the cat out.'; } echo '<br/><br/>'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/238015-how-to-get-an-null-echo-reslut/#findComment-1223073 Share on other sites More sharing options...
sudduth66 Posted May 31, 2011 Author Share Posted May 31, 2011 Thank You it is all working now. wildteen88 thanks for your fix and thanks to all that replied. I am sure i will be back as i am new to this and been put in charge of building this site. THANK YOU Quote Link to comment https://forums.phpfreaks.com/topic/238015-how-to-get-an-null-echo-reslut/#findComment-1223077 Share on other sites More sharing options...
Psycho Posted May 31, 2011 Share Posted May 31, 2011 You have your logic backwards. You have to check if there were results BEFORE processing the results. Although, I have a hard time believing that you are getting no results and no errors. <?php //Connect to DB mysql_connect ("localhost", "John","Harley08") or die (mysql_error()); mysql_select_db ("Customers"); //Parse user input and run query $term = mysql_real_escape_string(trim($_POST['term'])); $query = "SELECT * FROM storage WHERE box_number = '$term'"; $result = mysql_query($query); //Check results of query if (!$result) { //There was an error running the query echo "There was an error running the query<br><br>\n"; echo "Query:<br>{$query}<br><br>\n"; echo "Error:<br>" . mysql_erro(); } else if(mysql_num_rows($sql) < 1) { //No records returned echo 'No Boxes available please use your back button to select a new box.'; } else { //There were records returned, display them while ($row = mysql_fetch_array($sql)) { echo "box_num: {$row['box_number']}<br>\n"; echo "dept: {$row['Department']}<br>\n"; echo "company: {$row['Company']}<br>\n"; echo "status: {$row['status']}<br>\n"; echo "location: {$row['location']}<br>\n"; echo "description: {$row['box_desc']}<br><br>\n"; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/238015-how-to-get-an-null-echo-reslut/#findComment-1223078 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.