/    Sign up×
Community /Pin to ProfileBookmark

need basic false return snippet

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

to post a comment
PHP

14 Comments(s)

Copy linkTweet thisAlerts:
@pyroNov 10.2003 — Try something like this:

[code=php]if (mysql_num_rows($results) == 0) {
echo "sorry, there were no results please try another location";
exit();
}[/code]
Copy linkTweet thisAlerts:
@tracy_jbauthorNov 10.2003 — thanks for the reply but ai get this error:

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource
Copy linkTweet thisAlerts:
@pyroNov 10.2003 — Post the code that you tried. Also, I'm assuming it is a MySQL db?
Copy linkTweet thisAlerts:
@tracy_jbauthorNov 10.2003 — I know this is a little clunky but it works for me... not sure how to post it either...






[code=php]<?php [/code] <?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

?>
Copy linkTweet thisAlerts:
@pyroNov 10.2003 — $results should be the variable that contains the results of your mysql_query().
Copy linkTweet thisAlerts:
@tracy_jbauthorNov 10.2003 — 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 ?
Copy linkTweet thisAlerts:
@tracy_jbauthorNov 10.2003 — [code=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
?> [/code]
Copy linkTweet thisAlerts:
@pyroNov 10.2003 — This perhaps:

[code=php]//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();
}[/code]
Copy linkTweet thisAlerts:
@tracy_jbauthorNov 10.2003 — 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?
Copy linkTweet thisAlerts:
@tracy_jbauthorNov 10.2003 — I got it ! just kept hacking away...

here is what I got to work:

[code=php] 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";[/code]
Copy linkTweet thisAlerts:
@pyroNov 10.2003 — You should be able to get rid of case 1, and it will continue to function the same....
Copy linkTweet thisAlerts:
@tracy_jbauthorNov 10.2003 — 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....
Copy linkTweet thisAlerts:
@pyroNov 10.2003 — Can you post your SQL, please?
Copy linkTweet thisAlerts:
@tracy_jbauthorNov 10.2003 — 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...
×

Success!

Help @tracy_jb spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.29,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...