SEVIZ Posted May 12, 2009 Share Posted May 12, 2009 Here is my simple code. It is a simple query to search a row for data. <?php $con = mysql_connect("--------","-----------","------------"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("----------", $con); $id=$_POST['id']; $result = mysql_query("SELECT * FROM sprint WHERE id LIKE '%$id%'"); echo "<table border='0' CELLPADDING=5 STYLE='font-size:13px'>"; echo "<tr> <td><H3>Tech ID</h3></td> <td><H3>Name</H3></td> <td><H3>Phone Number</H3></td><td><H3>Team</H3></td></tr>"; while($row = mysql_fetch_array($result)) { echo "<tr><td>"; echo $row['id']; echo "</td><td>"; echo $row['name']; echo "</td><td>"; echo $row['num']; echo "</td><td>"; echo $row['sup']; echo "</td></tr>"; } echo "</table>"; ?> <br /><br /> <form action="techdbid.php" method="post"> Tech: <input type="text" name="id"><br /> <input type="Submit"> </form> What can I do so that if there is no results for the query it display an error message? "No ID found". Thank you. Link to comment https://forums.phpfreaks.com/topic/157753-if-no-data-display-error/ Share on other sites More sharing options...
Ken2k7 Posted May 12, 2009 Share Posted May 12, 2009 Well there are a few ways. From what you already did, I'd say mysql_num_rows would be the easiest way. Link to comment https://forums.phpfreaks.com/topic/157753-if-no-data-display-error/#findComment-832067 Share on other sites More sharing options...
SEVIZ Posted May 12, 2009 Author Share Posted May 12, 2009 I looked at that but I do not understand it. Can I just do an if statement like, " if $result = nothing display error" ? Link to comment https://forums.phpfreaks.com/topic/157753-if-no-data-display-error/#findComment-832071 Share on other sites More sharing options...
Ken2k7 Posted May 12, 2009 Share Posted May 12, 2009 Well if you use that function, it returns the number of rows your query returns back to you. If it's 0, then it's empty. Link to comment https://forums.phpfreaks.com/topic/157753-if-no-data-display-error/#findComment-832073 Share on other sites More sharing options...
SEVIZ Posted May 12, 2009 Author Share Posted May 12, 2009 Thats my problem. I must be doing something wrong because the function returns no answer. <?php $con = mysql_connect("-------","---------","---------------"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("------", $con); $id=$_POST['id']; $result = mysql_query("SELECT * FROM sprint WHERE id LIKE '%$id%'"); $num_rows = mysql_num_rows($result); echo "<table border='0' CELLPADDING=5 STYLE='font-size:13px'>"; echo "<tr> <td><H3>Tech ID</h3></td> <td><H3>Name</H3></td> <td><H3>Phone Number</H3></td><td><H3>Team</H3></td></tr>"; while($row = mysql_fetch_array($result)) { echo "<tr><td>"; echo $row['id']; echo "</td><td>"; echo $row['name']; echo "</td><td>"; echo $row['num']; echo "</td><td>"; echo $row['sup']; echo "</td></tr>"; } echo "</table>"; echo $numrows; echo "Rows"; ?> <br /><br /> <form action="techdbid.php" method="post"> Tech: <input type="text" name="id"><br> <input type="Submit"> </form> The page goes through and there is no data there. Its just "Rows". With no number. Link to comment https://forums.phpfreaks.com/topic/157753-if-no-data-display-error/#findComment-832098 Share on other sites More sharing options...
SEVIZ Posted May 12, 2009 Author Share Posted May 12, 2009 Im dumb I already see the error. Forgot the _ in the variable. Link to comment https://forums.phpfreaks.com/topic/157753-if-no-data-display-error/#findComment-832099 Share on other sites More sharing options...
SEVIZ Posted May 12, 2009 Author Share Posted May 12, 2009 Ok heres what I got. I can now see the num rows result but the if statement does not work. If less than 0 it still shows blank. <?php $con = mysql_connect("-----","--------","----------"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("-------------", $con); $id=$_POST['id']; $result = mysql_query("SELECT * FROM sprint WHERE id LIKE '%$id%'"); $num_rows = mysql_num_rows($result); echo "<table border='0' CELLPADDING=5 STYLE='font-size:13px'>"; echo "<tr> <td><H3>Tech ID</h3></td> <td><H3>Name</H3></td> <td><H3>Phone Number</H3></td><td><H3>Team</H3></td></tr>"; while($row = mysql_fetch_array($result)) { if ($num_rows > 0){ echo "<tr><td>"; echo $row['id']; echo "</td><td>"; echo $row['name']; echo "</td><td>"; echo $row['num']; echo "</td><td>"; echo $row['sup']; echo "</td></tr>"; }else{ echo "<tr colspan=4><td>NO DATA</td></tr>"; } } echo "</table>"; echo $num_rows; echo " Rows"; ?> <br /><br /> <form action="techdbid.php" method="post"> Tech: <input type="text" name="id"><br> <input type="Submit"> </form> Link to comment https://forums.phpfreaks.com/topic/157753-if-no-data-display-error/#findComment-832101 Share on other sites More sharing options...
Ken2k7 Posted May 12, 2009 Share Posted May 12, 2009 Don't use $num_rows inside the while loop. There's no need to. Why? Because if the while loop gets executed, you know that $num_rows is greater than 0. So it makes no sense to put the if statement in there. Put it outside the loop. Link to comment https://forums.phpfreaks.com/topic/157753-if-no-data-display-error/#findComment-832146 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.