Need Help to Display Message When No Results Returned

I have a simple form that allows users to enter a number and get back the complete DB listing for that number. Right now if they enter an invalid number (one that doesn’t exist in the DB) the page shows nothing except the original form. Could anyone help me add a message that says the number doesn’t exist if the number is not in the DB? Here is a snipit of my code where the results are displayed.


$query = "SELECT pin, control, value, exp, type FROM pin_master WHERE pin= '" . $_POST["pin"] . "'";

if ($r = mysql_query ($query)) {

	while ($row = mysql_fetch_array ($r)) {
	
		print "<font  font face='Arial, Helvetica, sans-serif'><table border=5 bordercolor=#5b95c7 cellpadding=5>
						<tr  align='center'><td><b>PIN</b></td>
							<td><b>CONTROL</b></td>
							<td><b>VALUE</b></td>
							<td><b>EXP</b></td>
							<td><b>TYPE</b></td></tr>
						<tr  align='center'><td><b>{$row['pin']}</b></td>
							<td><b>{$row['control']}</b></td>
							<td><b>{$row['value']}</b></td>
							<td><b>{$row['exp']}</b></td>
							<td><b>{$row['type']}</b></td></tr></table></font>
		n";	
	
	}
	
} else {
	die ('<p>Could not retrieve the data because: <b>' . mysql_error() . "</b>.
	The query was $query.</p>");
}

Use another IF statement after running the query.

IF (mysql_num_rows == 0) {

   echo "Error Message or Whatever... HERE";
} else {
   // Put code for successful data retrieval here

}

Thanks for the reply! I tried that and all I get is the error message on the screen all the time and good results will not show. I thought that maybe I should post my entire code, b/c maybe i have the whole thing built screwy. Here it is:

<table width="600" align="center" bgcolor="FFFFFF">
	<tr><td>&nbsp;</td></tr>
	<tr>
		<td align="center"><div align="left">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<img src="images/imagesname.jpg"></div></td>
	</tr>
	
	<tr>			
    <td height="70" align="center" valign="center"><font size="6" face="Arial, Helvetica, sans-serif"><strong>PIN 
      Checker<br>
      </strong></font><font face="Arial, Helvetica, sans-serif"><strong>v2.1b</strong></font></td>
	</tr>
	<tr>
		<td align="center">
			<form action="pin_checker_test.php" method="post">			
       		<p><font size="4" face="Arial, Helvetica, sans-serif">Enter PIN Number</font><br />
			<input type="text" name="pin" size="15" maxlength="15" /></p>
			<input type="submit" name="submit" value="Get PIN Information" />
			</form>
			</td>
	</tr>
	<tr>	
    	<td align="center">
	<?php // Script 12.1 - mysql_connect.php

//if (isset ($_POST['submit'])) {//handle the form


ini_set ('display errors', 1);
error_reporting (E_ALL & ~E_NOTICE);

if ($dbc = mysql_connect ('localhost', 'username', 'password')) {
	;

	if (@mysql_select_db ('pin_check')) {
		;
	} else {
		die ('<p><b>Could not select the database because: <b>' .
		mysql_error() . '</b></p>');
	}

//define querey
$query = "SELECT pin, control, value, exp, type FROM pin_master WHERE pin= '" . $_POST["pin"] . "'";

if ($r = mysql_query ($query)) {

	while ($row = mysql_fetch_array ($r)) {
	
		print "<font  font face='Arial, Helvetica, sans-serif'><table border=5 bordercolor=#5b95c7 cellpadding=5>
						<tr  align='center'><td><b>PIN</b></td>
							<td><b>CONTROL</b></td>
							<td><b>VALUE</b></td>
							<td><b>EXP</b></td>
							<td><b>TYPE</b></td></tr>
						<tr  align='center'><td><b>{$row['pin']}</b></td>
							<td><b>{$row['control']}</b></td>
							<td><b>{$row['value']}</b></td>
							<td><b>{$row['exp']}</b></td>
							<td><b>{$row['type']}</b></td></tr></table></font>
		n";	
	
	}
	
} else {
	die ('<p>Could not retrieve the data because: <b>' . mysql_error() . "</b>.
	The query was $query.</p>");
}	
	}
	mysql_close();

} else {

	print'<p><b><font color=#FF0000 size=+2>ERROR</font><br>
	<font color=#FF0000>Could not connect to MySQL.<br>
	Please contact <a href="mailto:[email protected]"><font color="FF0000">site administrator.</font></a></font></b></p>';
	
}

?>
		</td>
	</tr>
	<tr>	
    	<td><font face="Arial, Helvetica, sans-serif"><hr />
      © MMVIII Compnay</font></td>
	</tr>
</table></fr>

What is the error message returned by MYSQL_ERROR()?

Oh I’m sorry, I meant all I get is the error message that we coded in “Error Message or Whatever… HERE”. So in other words the IF statement is working, but it is ALWAYS there from the start (at the start there should be no error message and no results), and the else isn’t working.

It’s not that the ELSE isn’t working, rather it never GETS to the ELSE because the IF is always true.

Check the MYSQL_ERROR() right after you run the query.

/* ------------------------------------------
*
*          Code to help debug errors
*
 ------------------------------------------ */

// Execute the query
$variable = mysql_query("some query");  

// Echo out the error will be mysql_error() will be empty if successful
echo "<br><b>ERRORS: ".mysql_error() ."</b><br>"; 

// Halt execution at this point because we are "Debugging"
die();  

This will allow you to see if there is an error and what it might be. MySql Errors will NOT stop execution of your script (unless it’s the PHP portion of it). So if you have SELECT fieldA, fieldB, fieldC FROM table WHERE condition= ‘Value’ and there is NO fieldB the query it self will fail and return ZERO rows. mysql_error() will contain the information as to why it failed but unless you check it you won’t know. Your script will just go on. Since the query returned ZERO rows, the IF will always be true.

CHECK THE MYSQL_ERROR() and see if that gives a clue.

I tried looking for SQL errors and I don’t get any.

I changed your secondary IF statement around a little to make it say “IF (mysql_num_rows > 0)” to print my results, otherwise give an error. What I’ve deducted from this, and you probably already know, is that the way I have my page coded, b/c the text field is always blank, the number of rows always = 0. This is even the case when I put in a number that I know give a result. I get no result, only the error b/c the else part of the statement, which is where it is making it to, does not tell it to display the results. So I guess how do I get the IF statement to look at the number of rows returned AFTER I click the button to run the query?

If I put the “print the results table” part of the code in with the “IF (mysql_num_rows = 0)” I get the results, or if I put it in with the ELSE if I go with “IF (mysql_num_rows > 0).”

Sponsor our Newsletter | Privacy Policy | Terms of Service