sonny
08-07-2009, 07:19 PM
Hi I set up a MsQL search form that has a field choice and text box, the search result part
works fine on the first page.
The problem: is when I click a next page link I receive the following error
Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource LINE 14
My Question: is how do you carry over the results from page to page,
I thought php self target just does that
any Idea what I did below wrong? also did I make it safe enough to use publicly?
Thanks
Sonny
<?
require "./connect.php";
//-------------------------------------
//This checks to see if there is a page number. If not, it will set it to page 1
if (!(isset($pagenum)))
{
$pagenum = 1;
}
//Edit $data to be your query (I Don't understand this part I have two values Field and search string)
$results = mysql_query("SELECT * FROM visits WHERE upper($field) LIKE'%$find%'");
$rows = mysql_num_rows($results);
//This is the number of results displayed per page
$page_rows = 4;
//This tells us the page number of our last page
$last = ceil($rows/$page_rows);
//this makes sure the page number isn't below one, or more than our maximum pages
if ($pagenum < 1)
{
$pagenum = 1;
}
elseif ($pagenum > $last)
{
$pagenum = $last;
}
//This sets the range to display in our query
$max = 'limit ' .($pagenum - 1) * $page_rows .',' .$page_rows;
// This shows the user what page they are on, and the total number of pages
echo " --Page $pagenum of $last-- <p>";
// First we check if we are on page one. If we are then we don't need a link to the previous page or the first page so we do nothing. If we aren't then we generate links to the first page, and to the previous page.
if ($pagenum == 1)
{
}
else
{
echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=1'> <<-First</a> ";
echo " ";
$previous = $pagenum-1;
echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$previous'> <-Previous</a> ";
}
//just a spacer
echo " ---- ";
//This does the same as above, only checking if we are on the last page, and then generating the Next and Last links
if ($pagenum == $last)
{
}
else {
$next = $pagenum+1;
echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$next'>Next -></a> ";
echo " ";
echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$last'>Last ->></a> ";
}
?>
<html>
<head>
<title>Search Results - <?=$find?></title>
<style>
A { font-family: Arial; color: #008080; font-size: 10pt;}
A:hover { color: #C0C0C0; text-decoration: none;}
td { font-family: Arial; color: #000080; font-size: 10pt;}
p { font-family: Arial; color: #000080; font-size: 10pt;}
</style>
</head>
<body>
<div><center><table width="600" border="0"><tr><td colspan=5>
<form name="search" method="post" action="<?=$PHP_SELF?>">
Seached for:
<input type="text" name="find" value="" /> in field
<Select NAME="field">
<Option VALUE="city">City</option>
<Option VALUE="region">Region</option>
<Option VALUE="country">Country</option>
</Select>
<input type="hidden" name="searching" value="yes" />
<input type="submit" name="search" value="Search" />
</form></td></tr>
</table>
</center>
</div>
<div><center><table width="600" border="0"><tr>
<?
//This is only displayed if they have submitted the form
if ($searching =="yes")
{
//If they did not enter a search term we give them an error
if ($find == "")
{
echo "<p>You forgot to enter a search term";
exit;
}
// We preform a bit of filtering
$find= strtoupper($find);
$find = strip_tags($find);
$find = trim ($find);
//Now we search for our search term, in the field the user specified
$results = mysql_query("SELECT * FROM visits WHERE upper($field) LIKE'%$find%' $max");
//And we display the results
while($row = mysql_fetch_assoc($results))
{
$city=$row['city'];
$region=$row['region'];
$country=$row['country'];
echo "<tr><td>$city</td>
<td><b>$region</b></td>
<td><b>($country)</b></td></tr>";
}
//This counts the number or results - and if there wasn't any it gives them a little message explaining that
$anymatches=mysql_num_rows($results);
if ($anymatches == 0)
{
echo "Sorry, but we can not find an entry to match your query<br><br>";
}
//And we remind them what they searched for
echo "<b>Searched For:</b><b><font color=ff0000> .$find";
echo "</font></b>";
}
?>
</td></tr>
</table></center></div>
</body>
</html>
works fine on the first page.
The problem: is when I click a next page link I receive the following error
Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource LINE 14
My Question: is how do you carry over the results from page to page,
I thought php self target just does that
any Idea what I did below wrong? also did I make it safe enough to use publicly?
Thanks
Sonny
<?
require "./connect.php";
//-------------------------------------
//This checks to see if there is a page number. If not, it will set it to page 1
if (!(isset($pagenum)))
{
$pagenum = 1;
}
//Edit $data to be your query (I Don't understand this part I have two values Field and search string)
$results = mysql_query("SELECT * FROM visits WHERE upper($field) LIKE'%$find%'");
$rows = mysql_num_rows($results);
//This is the number of results displayed per page
$page_rows = 4;
//This tells us the page number of our last page
$last = ceil($rows/$page_rows);
//this makes sure the page number isn't below one, or more than our maximum pages
if ($pagenum < 1)
{
$pagenum = 1;
}
elseif ($pagenum > $last)
{
$pagenum = $last;
}
//This sets the range to display in our query
$max = 'limit ' .($pagenum - 1) * $page_rows .',' .$page_rows;
// This shows the user what page they are on, and the total number of pages
echo " --Page $pagenum of $last-- <p>";
// First we check if we are on page one. If we are then we don't need a link to the previous page or the first page so we do nothing. If we aren't then we generate links to the first page, and to the previous page.
if ($pagenum == 1)
{
}
else
{
echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=1'> <<-First</a> ";
echo " ";
$previous = $pagenum-1;
echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$previous'> <-Previous</a> ";
}
//just a spacer
echo " ---- ";
//This does the same as above, only checking if we are on the last page, and then generating the Next and Last links
if ($pagenum == $last)
{
}
else {
$next = $pagenum+1;
echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$next'>Next -></a> ";
echo " ";
echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$last'>Last ->></a> ";
}
?>
<html>
<head>
<title>Search Results - <?=$find?></title>
<style>
A { font-family: Arial; color: #008080; font-size: 10pt;}
A:hover { color: #C0C0C0; text-decoration: none;}
td { font-family: Arial; color: #000080; font-size: 10pt;}
p { font-family: Arial; color: #000080; font-size: 10pt;}
</style>
</head>
<body>
<div><center><table width="600" border="0"><tr><td colspan=5>
<form name="search" method="post" action="<?=$PHP_SELF?>">
Seached for:
<input type="text" name="find" value="" /> in field
<Select NAME="field">
<Option VALUE="city">City</option>
<Option VALUE="region">Region</option>
<Option VALUE="country">Country</option>
</Select>
<input type="hidden" name="searching" value="yes" />
<input type="submit" name="search" value="Search" />
</form></td></tr>
</table>
</center>
</div>
<div><center><table width="600" border="0"><tr>
<?
//This is only displayed if they have submitted the form
if ($searching =="yes")
{
//If they did not enter a search term we give them an error
if ($find == "")
{
echo "<p>You forgot to enter a search term";
exit;
}
// We preform a bit of filtering
$find= strtoupper($find);
$find = strip_tags($find);
$find = trim ($find);
//Now we search for our search term, in the field the user specified
$results = mysql_query("SELECT * FROM visits WHERE upper($field) LIKE'%$find%' $max");
//And we display the results
while($row = mysql_fetch_assoc($results))
{
$city=$row['city'];
$region=$row['region'];
$country=$row['country'];
echo "<tr><td>$city</td>
<td><b>$region</b></td>
<td><b>($country)</b></td></tr>";
}
//This counts the number or results - and if there wasn't any it gives them a little message explaining that
$anymatches=mysql_num_rows($results);
if ($anymatches == 0)
{
echo "Sorry, but we can not find an entry to match your query<br><br>";
}
//And we remind them what they searched for
echo "<b>Searched For:</b><b><font color=ff0000> .$find";
echo "</font></b>";
}
?>
</td></tr>
</table></center></div>
</body>
</html>