Click to See Complete Forum and Search --> : need basic false return snippet
tracy_jb
11-10-2003, 12:09 PM
is there a standard snippet of php or sql that I can place in my script that will tell searchers that there were no results to their query?
for instance... my db allows users to search for business of different categories located in different cities... when there are no particular businesses of the selected type for the city selected the script returns a blank page...
Can someone please show me some snippets that will ad something like :
"sorry, there were no results please try another location" etc. ?
Thanks
Try something like this:
if (mysql_num_rows($results) == 0) {
echo "sorry, there were no results please try another location";
exit();
}
tracy_jb
11-10-2003, 12:39 PM
thanks for the reply but ai get this error:
Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource
Post the code that you tried. Also, I'm assuming it is a MySQL db?
tracy_jb
11-10-2003, 12:53 PM
I know this is a little clunky but it works for me... not sure how to post it either...
<?php <?php
/*
broke this into functions...much easier to see and follow the flow of the program
*/
/* main code block */
//see if the form is submitted
if (!$_POST['submit']){
show_form();
}else{
//form submitted and need to get record(s)
get_data();
}//end if
//end main code block
function show_form()
{
//assume that city and category come from database
mysql_connect("localhost", "######", "######") or die(mysql_error());
mysql_select_db("BUSLIST") or die(mysql_error());
//get data for city and category
$sql = 'SELECT DISTINCT city FROM bus_list';
$r = mysql_query($sql) or die(mysql_error());
while($row = mysql_fetch_object($r))
{
$city[] = $row->city;
}
$sql = 'SELECT DISTINCT category FROM bus_list';
$r = mysql_query($sql) or die(mysql_error());
while($row = mysql_fetch_object($r))
{
$category[] = $row->category;
}
$result_city = mysql_query($sql) or die ("Can't connect to perform query one because ".mysql_error());
$result_category = mysql_query($sql) or die ("Can't connect to perform query two because ".mysql_error());
if ((!result_city)||(!$result_category)){
echo "error retrieving data";
}else{
//place the results into an array
while ($rows=mysql_fetch_array($result_city )){
$city[]=$rows['city'];
}
//while ($rows1=mysql_fetch_array($result_category)){
//$category[]=$rows1['category'];
//}
//show the form headers
echo "
<body>
<form action=\"" . $_SERVER['PHP_SELF'] . "\" method=\"post\">
<table>
<tr>
<td>Select city</td></td>
<td>Select category</td>
</tr>
<tr>
<td><select name=\"city\">";
//show the city data
for ($x=0;$x<count($city);$x++){
echo "<option value=\"".$city[$x]."\">".$city[$x];
}
echo "</select></td><td>
<select name=\"category\">";
//add the category data
for ($x=0;$x<count($category);$x++){
echo "<option value=\"".$category[$x]."\">".$category[$x];
}
echo "</select></td></tr></table>
<input type=\"submit\" name=\"submit\" value=\"search\">
</form></body>";
}
}//end function show_form
function get_data()
{
//get data from form
$city=$_POST['city'];
$category=$_POST['category'];
$sql="select * from bus_list where city='$city' and category='$category'";
mysql_connect("localhost", "######", "######") or die(mysql_error());
mysql_select_db("BUSLIST") or die(mysql_error());
if (mysql_num_rows($results) == 0) {
echo "sorry, there were no results please try another location";
exit();
}
// if the result didn't work, kill the script with an error
die($error_msg);
// count the result rows, do something with the value
$mysql_num_rows = mysql_num_rows($result);
switch($mysql_num_rows)
{
case 0:
die($no_results);
break;
case 1:
$results_str = "Search returned 1 result from the database:<br>\n";
break;
default:
$results_str = "Search returned $mysql_num_rows results from the database:<br>\n";
}
// if there is a result, print it out in a table
echo "<div align=\"center\">\n";
echo $results_str;
echo "<table border=1 cellpadding=2 cellspacing=0 width=\"90%\">\n";
echo "<tr>\n";
echo "<td class='title'>name</td>\n";
echo "<td class='title'>address</td>\n";
echo "<td class='title'>city</td>\n";
echo "<td class='title'>zip</td>\n";
echo "<td class='title'>phone</td>\n";
echo "<td class='title'>www</td>\n";
echo "</tr>\n";
while($row = @mysql_fetch_array($result))
{
extract($row); // grabs the array and splits into vars
echo "<tr>\n";
echo "<td>$name</td>\n";
echo "<td>$address</td>\n";
echo "<td>$city</td>\n";
echo "<td>$zip</td>\n";
echo "<td>$phone</td>\n";
echo "<td>$www</a></td>\n";
echo "</tr>\n";
}
echo "</table>\n";
echo "</div>\n<br><br>\n";
echo $input_form."<br>\n";
//} // close the second if-else statement
//else {
// no submission, print the regular form
echo $input_form;
} // close if statment for No RESULT
// close the first if-else statement
?>
$results should be the variable that contains the results of your mysql_query().
tracy_jb
11-10-2003, 01:02 PM
sorry I am not a coder I have no idea what you are freferring to... I have supplied the entire script.. can you help me out here ?
tracy_jb
11-10-2003, 01:03 PM
<?php
/*
broke this into functions...much easier to see and follow the flow of the program
*/
/* main code block */
//see if the form is submitted
if (!$_POST['submit']){
show_form();
}else{
//form submitted and need to get record(s)
get_data();
}//end if
//end main code block
function show_form()
{
//assume that city and category come from database
mysql_connect("localhost", "######", "######") or die(mysql_error());
mysql_select_db("BUSLIST") or die(mysql_error());
//get data for city and category
$sql = 'SELECT DISTINCT city FROM bus_list';
$r = mysql_query($sql) or die(mysql_error());
while($row = mysql_fetch_object($r))
{
$city[] = $row->city;
}
$sql = 'SELECT DISTINCT category FROM bus_list';
$r = mysql_query($sql) or die(mysql_error());
while($row = mysql_fetch_object($r))
{
$category[] = $row->category;
}
$result_city = mysql_query($sql) or die ("Can't connect to perform query one because ".mysql_error());
$result_category = mysql_query($sql) or die ("Can't connect to perform query two because ".mysql_error());
if ((!result_city)||(!$result_category)){
echo "error retrieving data";
}else{
//place the results into an array
while ($rows=mysql_fetch_array($result_city )){
$city[]=$rows['city'];
}
//while ($rows1=mysql_fetch_array($result_category)){
//$category[]=$rows1['category'];
//}
//show the form headers
echo "
<body>
<form action=\"" . $_SERVER['PHP_SELF'] . "\" method=\"post\">
<table>
<tr>
<td>Select city</td></td>
<td>Select category</td>
</tr>
<tr>
<td><select name=\"city\">";
//show the city data
for ($x=0;$x<count($city);$x++){
echo "<option value=\"".$city[$x]."\">".$city[$x];
}
echo "</select></td><td>
<select name=\"category\">";
//add the category data
for ($x=0;$x<count($category);$x++){
echo "<option value=\"".$category[$x]."\">".$category[$x];
}
echo "</select></td></tr></table>
<input type=\"submit\" name=\"submit\" value=\"search\">
</form></body>";
}
}//end function show_form
function get_data()
{
//get data from form
$city=$_POST['city'];
$category=$_POST['category'];
$sql="select * from bus_list where city='$city' and category='$category'";
mysql_connect("localhost", "######", "######") or die(mysql_error());
mysql_select_db("BUSLIST") or die(mysql_error());
if (mysql_num_rows($results) == 0) {
echo "sorry, there were no results please try another location";
exit();
}
// if the result didn't work, kill the script with an error
die($error_msg);
// count the result rows, do something with the value
$mysql_num_rows = mysql_num_rows($result);
switch($mysql_num_rows)
{
case 0:
die($no_results);
break;
case 1:
$results_str = "Search returned 1 result from the database:<br>\n";
break;
default:
$results_str = "Search returned $mysql_num_rows results from the database:<br>\n";
}
// if there is a result, print it out in a table
echo "<div align=\"center\">\n";
echo $results_str;
echo "<table border=1 cellpadding=2 cellspacing=0 width=\"90%\">\n";
echo "<tr>\n";
echo "<td class='title'>name</td>\n";
echo "<td class='title'>address</td>\n";
echo "<td class='title'>city</td>\n";
echo "<td class='title'>zip</td>\n";
echo "<td class='title'>phone</td>\n";
echo "<td class='title'>www</td>\n";
echo "</tr>\n";
while($row = @mysql_fetch_array($result))
{
extract($row); // grabs the array and splits into vars
echo "<tr>\n";
echo "<td>$name</td>\n";
echo "<td>$address</td>\n";
echo "<td>$city</td>\n";
echo "<td>$zip</td>\n";
echo "<td>$phone</td>\n";
echo "<td>$www</a></td>\n";
echo "</tr>\n";
}
echo "</table>\n";
echo "</div>\n<br><br>\n";
echo $input_form."<br>\n";
//} // close the second if-else statement
//else {
// no submission, print the regular form
echo $input_form;
} // close if statment for No RESULT
// close the first if-else statement
?>
This perhaps:
//get data from form
$city = $_POST['city'];
$category=$_POST['category'];
mysql_connect("localhost", "######", "######") or die(mysql_error());
mysql_select_db("BUSLIST") or die(mysql_error());
$sql="SELECT * FROM `bus_list` WHERE `city`='$city' AND `category`='$category'";
$results = mysql_query($sql);
if (mysql_num_rows($results) == 0) {
echo "sorry, there were no results please try another location";
exit();
}
tracy_jb
11-10-2003, 01:31 PM
well that seems to work when there are no results...BUT where there are supposed to be results it just prints the column names with no results...?
any ideas?
tracy_jb
11-10-2003, 03:38 PM
I got it ! just kept hacking away...
here is what I got to work:
if (!$result = @mysql_query($sql))
// if the result didn't work, kill the script with an error
die($error_msg);
// count the result rows, do something with the value
$mysql_num_rows = mysql_num_rows($result);
switch($mysql_num_rows)
{
case 0:
echo $no_results = "sorry no results:<br>\n";
break;
case 1:
$results_str = "Search returned 1 result from the database:<br>\n";
break;
default:
$results_str = "Search returned $mysql_num_rows results from the database:<br>\n";
You should be able to get rid of case 1, and it will continue to function the same....
tracy_jb
11-10-2003, 04:04 PM
thanks I am still tweaking the script and app.
I am having an MYsql problem with the same script...
the menus populate ok and I have the "category" menu set to "Order by category"... BUT they are only in alphabetical order by the id ...
what I mean is it goes thru the alphabet then repeats with categories that have been added later and have higher (and out of sync) id #'s
hope this is clear?
thanks for your time on the previous problem....
Can you post your SQL, please?
tracy_jb
11-10-2003, 04:24 PM
BUSLIST
CREATE TABLE `bus_list` (
`name` varchar(255) NOT NULL default '',
`address` varchar(255) NOT NULL default '',
`city` varchar(255) NOT NULL default '',
`zip` varchar(15) default NULL,
`phone` varchar(40) default NULL,
`www` varchar(255) default NULL,
`notes` text,
`cat` varchar(255) default NULL,
`id` int(11) unsigned NOT NULL auto_increment,
`subcatid` varchar(11) default NULL,
PRIMARY KEY (`id`)
) TYPE=MyISAM AUTO_INCREMENT=1 ;
same script as above... only I am trying to change this
$sql = 'SELECT DISTINCT category FROM bus_list ORDER BY category';
the problem is the menu populates by "category" but in more than one block it lists all the original records... thenstarts the alphabet over again withcategories that were added later...