Log in

View Full Version : Create a message that says "no records found" with php



Perspective
12-28-2007, 02:17 AM
Hello,

I have the following code and it works with a page that has a search form with it. However when a user types in information that is not found in the mysql database I would like the page to return a message that says "no records found, please try your search again" I have tried several different options with no luck, is there anyone who can help me out (let me know what the code might be and where I would place it)?

Thank you

<?php

if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
{
$theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;

$theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

switch ($theType) {
case "text":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "long":
case "int":
$theValue = ($theValue != "") ? intval($theValue) : "NULL";
break;
case "double":
$theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
break;
case "date":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "defined":
$theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
break;
}
return $theValue;
}
}

$maxRows_rsvendor = 10;
$pageNum_rsvendor = 0;
if (isset($_GET['pageNum_rsvendor'])) {
$pageNum_rsvendor = $_GET['pageNum_rsvendor'];
}
$startRow_rsvendor = $pageNum_rsvendor * $maxRows_rsvendor;

$varzip_rsvendor = "1";
if (isset($_POST['Zip'])) {
$varzip_rsvendor = $_POST['Zip'];
}
$varcategory_rsvendor = "1";
if (isset($_POST['Category'])) {
$varcategory_rsvendor = $_POST['Category'];
}
mysql_select_db($database_vendor, $vendor);
$query_rsvendor = sprintf("SELECT commercial.Company, commercial.Street, commercial.City, commercial.Zip, commercial.Category FROM commercial WHERE commercial.Zip=%s AND commercial.Category=%s", GetSQLValueString($varzip_rsvendor, "text"),GetSQLValueString($varcategory_rsvendor, "text"));
$query_limit_rsvendor = sprintf("%s LIMIT %d, %d", $query_rsvendor, $startRow_rsvendor, $maxRows_rsvendor);
$rsvendor = mysql_query($query_limit_rsvendor, $vendor) or die(mysql_error());
$row_rsvendor = mysql_fetch_assoc($rsvendor);

if (isset($_GET['totalRows_rsvendor'])) {

$totalRows_rsvendor = $_GET['totalRows_rsvendor'];

} else {
$all_rsvendor = mysql_query($query_rsvendor);
$totalRows_rsvendor = mysql_num_rows($all_rsvendor);
}

$totalPages_rsvendor = ceil($totalRows_rsvendor/$maxRows_rsvendor)-1;
?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<title>Untitled Document</title>
</head>

<body>
<p>Commercial Vendors</p>
<p>&nbsp;</p>
<table border="0">

<?php do { ?>

<tr>
<td width="157"><?php echo $row_rsvendor['Company']; ?></td></tr>
<tr><td><?php echo $row_rsvendor['Street']; ?></td></tr>
<tr><td><?php echo $row_rsvendor['City']; ?></td></tr>
<tr><td><?php echo $row_rsvendor['Zip']; ?></td></tr>
<tr><td><?php echo $row_rsvendor['Category']; ?></td>
</tr>
<tr>
<td bgcolor="#666666">&nbsp;</td>
</tr>
<?php

} while ($row_rsvendor = mysql_fetch_assoc($rsvendor));
?>
</table>
<?php
mysql_free_result($rsvendor);
?>

codeexploiter
12-28-2007, 03:32 AM
In your case you can send a message back to the browser if there is no record in your recordset using the 'print' language construct in PHP and if there is no record the only thing going to the browser will be this message. The alogorithm is as follows:


if(recordset is empty){
print "<div>There are no records found</div>";
}else{
Your main record processing stuff goes here
}

The second approach is using PHP we send a client-side script segment into the browser, which makes an alert saying that there are no records. The code will look like the following:


print "<script type='text/javascript'>alert('Records not found; Try another search');</script>";

You can use any one of the above mentioned options.

Perspective
12-28-2007, 08:31 PM
Thank you, however I must have some kind of block in my brain because I tried your first option in several different places in the code and continue to get an error. Is there anyway you can copy the sample code I placed above and show me right where this code needs to be?

Thank you so much

tech_support
12-29-2007, 01:52 AM
<?php

if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
{
$theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;

$theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

switch ($theType) {
case "text":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "long":
case "int":
$theValue = ($theValue != "") ? intval($theValue) : "NULL";
break;
case "double":
$theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
break;
case "date":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "defined":
$theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
break;
}
return $theValue;
}
}

$maxRows_rsvendor = 10;
$pageNum_rsvendor = 0;
if (isset($_GET['pageNum_rsvendor'])) {
$pageNum_rsvendor = $_GET['pageNum_rsvendor'];
}
$startRow_rsvendor = $pageNum_rsvendor * $maxRows_rsvendor;

$varzip_rsvendor = "1";
if (isset($_POST['Zip'])) {
$varzip_rsvendor = $_POST['Zip'];
}
$varcategory_rsvendor = "1";
if (isset($_POST['Category'])) {
$varcategory_rsvendor = $_POST['Category'];
}
mysql_select_db($database_vendor, $vendor);
$query_rsvendor = sprintf("SELECT commercial.Company, commercial.Street, commercial.City, commercial.Zip, commercial.Category FROM commercial WHERE commercial.Zip=%s AND commercial.Category=%s", GetSQLValueString($varzip_rsvendor, "text"),GetSQLValueString($varcategory_rsvendor, "text"));
$query_limit_rsvendor = sprintf("%s LIMIT %d, %d", $query_rsvendor, $startRow_rsvendor, $maxRows_rsvendor);
$rsvendor = mysql_query($query_limit_rsvendor, $vendor) or die(mysql_error());
$row_rsvendor = mysql_fetch_assoc($rsvendor);

if (isset($_GET['totalRows_rsvendor'])) {

$totalRows_rsvendor = $_GET['totalRows_rsvendor'];

} else {
$all_rsvendor = mysql_query($query_rsvendor);
$totalRows_rsvendor = mysql_num_rows($all_rsvendor);
}

$totalPages_rsvendor = ceil($totalRows_rsvendor/$maxRows_rsvendor)-1;
?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<title>Untitled Document</title>
</head>

<body>
<p>Commercial Vendors</p>
<p>&nbsp;</p>
<table border="0">

<?php
if (mysql_num_rows($rsvendor) == 0) {
echo '<p>No records found.</p>';
} else {
do { ?>

<tr>
<td width="157"><?php echo $row_rsvendor['Company']; ?></td></tr>
<tr><td><?php echo $row_rsvendor['Street']; ?></td></tr>
<tr><td><?php echo $row_rsvendor['City']; ?></td></tr>
<tr><td><?php echo $row_rsvendor['Zip']; ?></td></tr>
<tr><td><?php echo $row_rsvendor['Category']; ?></td>
</tr>
<tr>
<td bgcolor="#666666">&nbsp;</td>
</tr>
<?php

} while ($row_rsvendor = mysql_fetch_assoc($rsvendor)); }
?>
</table>
<?php
mysql_free_result($rsvendor);
?>

Perspective
12-29-2007, 07:38 PM
Thank you very much

Leafy
01-02-2008, 09:08 AM
Will this work for the first row? It seems that $row_rsvendor is assigned after it gets done the first iteration.

djr33
01-03-2008, 01:37 AM
This line:
$row_rsvendor = mysql_fetch_assoc($rsvendor);
Immediately after the mysql query, though not in the while loop, both gives the impression that there are results if there are not (as it isn't conditional, so that variable is set to something, even if that something is an error), and also offsets the start of the while loop then by one by placing the pointer of that resource one row ahead, meaning one iteration ahead.