Jump to content

Need help with pagination please? PHP / MySQL error


Jax2

Recommended Posts

Hello everyone,

 

I am trying to display a list of games that I have stored in my database. The database only consists of 4 simple fields:

ID - The game ID #, incrimental

IMG - A link to the img file for the game

LINK - A link to the game file itself

DESC - A description of the game

 

The code I am using (Will paste below) results in an error:

 

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /mounted-storage/home7/sub002/sc14077-ZZHR/onlythebestgames.com/holiday/index3.php on line 49

 

I have tried using the same code (altered a bit of course) that I used on a previous website that was working fine, and got the same error on the same function (mysql_num_rows). Here is the new code I am using, can someone PLEASE point out what is causing this error and how I can fix it? I have outlined the problem line (line 49) in *********'s

 

 

<?
// Set Script Variables
$DB_Host="FOOO";
$DB_Name="FOOO";
$DB_User="FOOO";
$DB_Pass="FOOO";
$query="holidays";

// Open MySQL Connection
$Connection=mysql_connect($DB_Host, $DB_User, $DB_Pass);
if (!($limit)){
     $limit = 10;} // Default results per-page.

if (!($page)){
     $page = 0;} // Default page value.

$numresults = mysql_query("SELECT * FROM OTBG WHERE category LIKE '%". $query ."%'"); // the query.

********************************************************************
This is line 49 that is giving me trouble:

$numrows = mysql_num_rows($numresults); // Number of rows returned from above query.


*********************************************************************
if ($numrows == 0){
     echo("No results found matching your query - $query"); // bah, modify the "Not Found" error for your needs.
     exit();}
$pages = intval($numrows/$limit); // Number of results pages.

// $pages now contains int of pages, unless there is a remainder from division.

if ($numrows % $limit) {
$pages++;} // has remainder so add one page

$current = ($page/$limit) + 1; // Current page number.

if (($pages < 1) || ($pages == 0)) {
$total = 1;} // If $pages is less than one or equal to 0, total pages is 1.

else {
$total = $pages;} // Else total pages is $pages value.

$first = $page + 1; // The first result.

if (!((($page + $limit) / $limit) >= $pages) && $pages != 1) {
$last = $page + $limit;} //If not last results page, last result equals $page plus $limit.

else{
$last = $numrows;} // If last results page, last result equals total number of results.
//escape from PHP mode.
?>
<html>
<head>
<title>Search Results for <?=$query?></title>
</head>
<body>
<center><h2>Search Results for <?=$query?></h2></center>
<table width="100%" border="0">
<tr>
  <td width="50%" align="left">
Results <b><?=$first?></b> - <b><?=$last?></b> of <b><?=$numrows?></b>
  </td>
  <td width="50%" align="right">
Page <b><?=$current?></b> of <b><?=$total?></b>
  </td>
</tr>
<tr>
  <td colspan="2" align="right">
 
  </td>
</tr>
<tr>
  <td colspan="2" align="right">
Results per-page: <a href="<?=$PHP_SELF?>?query=<?=$query?>&page=<?=$page?>&limit=5">5</a> | <a href="<?=$PHP_SELF?>?query=<?=$query?>&page=<?=$page?>&limit=10">10</a> | <a href="<?=$PHP_SELF?>?query=<?=$query?>&page=<?=$page?>&limit=20">20</a> | <a href="<?=$PHP_SELF?>?query=<?=$query?>&page=<?=$page?>&limit=50">50</a>
  </td>
</tr>
</table>
<?
// Now we can display results.
$results = mysql_query("SELECT * FROM OTBG WHERE category LIKE '%". $query ."%' ORDER BY id ASC LIMIT $page, $limit");
while ($data = mysql_fetch_array($results))
{
?>
<table width="100%" id="snowline">
<tr>
<td>
<?php print ' '.$img .' '; ?>
</td>
<td>
<?php print ' '.$desc .' '; ?>
</td>
</tr>
<tr>
<td>
<?php print ' '.$link .' '; ?>
</td>
</tr>
</table>
<?php
}
?>
<p align="center">
<?
if ($page != 0) {              // Don't show back link if current page is first page.
$back_page = $page - $limit;
echo("<a href=\"$PHP_SELF?query=$query&page=$back_page&limit=$limit\">back</a>    \n");}

for ($i=1; $i <= $pages; $i++)             // loop through each page and give link to it.
{
$ppage = $limit*($i - 1);
if ($ppage == $page){
echo("<b>$i</b>\n");}            // If current page don't give link, just text.
else{
echo("<a href=\"$PHP_SELF?query=$query&page=$ppage&limit=$limit\">$i</a> \n");}
}

if (!((($page+$limit) / $limit) >= $pages) && $pages != 1) {               // If last page don't give next link.
$next_page = $page + $limit;
echo("    <a href=\"$PHP_SELF?query=$query&page=$next_page&limit=$limit\">next</a>");}
?>

Link to comment
Share on other sites

Ok have done that, and I put an error check after line 65, which reads:

 

$SQL_Result=mysql_db_query($DB_Name, $SQL) or die(mysql_error()); 

 

and now I get this error: Unknown column 'name' in 'order clause' when I run the page.

 

**EDIT***

 

Ahh, never mind, I figured it out, I was trying to sort by "name" instead of "id" and there is no "name" column. Thank you for the help, it's working now :)

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.