Jump to content

Having a Pagination Problem


SkyRanger

Recommended Posts

[code]
//start Top Pagination Code
if (!(isset($pagenum)))
{
$pagenum = 1;
}

//Here we count the number of results
//Edit $data to be your query
$data = mysql_query("SELECT * FROM quest where auth='1'") or die(mysql_error());
$rows = mysql_num_rows($data);

//This is the number of results displayed per page
$page_rows = 10;

//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 range that we will display in our query
$max = 'limit ' .($pagenum - 1) * $page_rows .',' .$page_rows;

$result = mysql_query( "SELECT * FROM quest where auth='1' order by 'id' desc $max") or die( "Unable to select database");
$wordnum = mysql_num_rows( $result );

while( $row = mysql_fetch_array( $result ) )

{
//End Pagination Code
?><b>Username:</b> <?
echo $row["uname"];
?>&nbsp;<b>|</b>&nbsp;<b>Age:</b> <?
echo $row["age"];
?>&nbsp;<b>|</b>&nbsp;<b>Date:</b> <?
echo $row["pdate"];
?><br><?
echo $row["msgpost"]; ?>
<br><br><hr>
<?
}
?><center>
            <?


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> ";
}
[/code]

The problem is that I am getting a "Unable to select database" unless I go into mysql and manually insert an entry.  Is there a way to fix this so I don't have to do this?
Link to comment
https://forums.phpfreaks.com/topic/24232-having-a-pagination-problem/
Share on other sites

Yes, I just left that garbage out.  Connecting is no problem, pulling is no problem.  Having the script not show ""Unable to select database"" when there is nothing in the table is the problem.

If I manually add something through phpmysql then it works, but if empty I get the error
Ok this is what I did:

[code]
$max = 'limit ' .($pagenum - 1) * $page_rows .',' .$page_rows;

$result = mysql_query( "SELECT * FROM tricks where auth='1' order by 'id' desc $max"); //removed or die
//added
Line:287 if( $result === FALSE ) { print "MySQL Error: "; mysql_error($result); }
Link:288 $wordnum = mysql_num_rows( $result );

Line:290 while( $row = mysql_fetch_array( $result ) )
[/code]

And end up getting:

MySQL Error:
Warning: mysql_error(): supplied argument is not a valid MySQL-Link resource in /public_html/poster.php on line 287

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in public_html/poster.php on line 288

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in public_html/poster.php on line 290
[quote author=rab link=topic=111795.msg453478#msg453478 date=1161131359]
gah, stupid mistake. give mysql_error() the mysql resource link
[/quote]

Not a prob rab, I am a noob, I am surprised I got this far...lol

Not sure if this is right, this time I posted the whole code:

[code]
include "inc/dbinfo.inc.php";
$connection=mysql_connect ("$dblocation", "$dbusername", "$dbpassword") or die ('I cannot connect to the database because: ' . mysql_error());
        mysql_select_db ("$dbname");

//start Top Pagination Code
if (!(isset($pagenum)))
{
$pagenum = 1;
}

//Here we count the number of results
//Edit $data to be your query
$data = mysql_query("SELECT * FROM tricks where auth='1'") or die(mysql_error());
$rows = mysql_num_rows($data);

//This is the number of results displayed per page
$page_rows = 10;

//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 range that we will display in our query
$max = 'limit ' .($pagenum - 1) * $page_rows .',' .$page_rows;

$result = mysql_query( "SELECT * FROM tricks where auth='1' order by 'id' desc $max");
if( $result === FALSE ) { print "MySQL Error: "; mysql_error($connection); }
$wordnum = mysql_num_rows( $result );

while( $row = mysql_fetch_array( $result ) )

{
//End Pagination Code
?><b>Username:</b> <?
echo $row["uname"];
?>&nbsp;<b>|</b>&nbsp;<b>Age:</b> <?
echo $row["age"];
?>&nbsp;<b>|</b>&nbsp;<b>Date:</b> <?
echo $row["pdate"];
?><br><?
echo $row["msgpost"]; ?>
<br><br><hr>
<?
}
?><center>
            <?


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> ";
}
[/code]

If that is right then I am now getting:

MySQL Error:
Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/squirt/public_html/poster.php on line 288

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/squirt/public_html/poster.php on line 290

[code]
$result = mysql_query( "SELECT * FROM tricks where auth='1' order by 'id' desc $max");
if( $result === FALSE ) { print "MySQL Error: "; mysql_error($connection); }
line:288 $wordnum = mysql_num_rows( $result );

line:290 while( $row = mysql_fetch_array( $result ) )
[/code]
Try this, there __may__ be some typos

[code=php:0]
<?php
include "inc/dbinfo.inc.php";

$connection = mysql_connect ($dblocation, $dbusername, $dbpassword);

if( !$connection )  // Error checking
{
die("Error connecting to MySQL Host: ". mysql_error());
}

$db = mysql_select_db ($dbname, $connection); // You NEED the MySQL link ID to select a DB

if( !$db ) // Error Checking
{
die("Error selecting a database: ". mysql_error($connection));
}

// Whats $pagenum? Defined? No
if( !( isset($pagenum) ) )
{
$pagenum = 1;
}

$data = mysql_query("SELECT * FROM tricks where auth='1'");

if( !$data )
{
die("Error selecting: ". mysql_error($connection));
}

$rows = mysql_num_rows($data);

if( $rows === FALSE ) // Regular num rows can return 0 for 0 rows, this checks if it is FALSE
{
die("Error fetching num rows");
}

//This is the number of results displayed per page
$page_rows = 10;

//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;
if( $pagenum > $last )
$pagenum = $last;

$max = 'limit ' .($pagenum - 1) * $page_rows .',' .$page_rows;

$result = mysql_query( "SELECT * FROM tricks where auth='1' order by 'id' desc $max");

if( !$result )
{
die("Error Selecting: ". mysql_error($connection));
}

$wordnum = mysql_num_rows( $result );

if( $wordnum === FALSE )
{
die("Error selecting num rows");
}

while( $row = mysql_fetch_array( $result ) )
{
?>
<b>Username:</b> <?=$row["uname"]; ?>
<b>Age:</b> <?=$row["age"]; ?>
<b>Date:</b> <?=$row["pdate"]; ?>
<b><?=$row["msgpost"]; ?>
<br><br><hr>
<?
}
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 )
{
echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=1'> <<-First</a> ";
echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=".$pagenum-1."'> <-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 )
{
echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=".$pagenum+1."'>Next -></a> ";
echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$last'>Last ->></a> ";
}
[/code]
Thanks rab,

just left with this:

Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING, expecting ',' or ';' in /home/squirt/public_html/tips_tricks.phtml on line 364

[code]echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=".$pagenum-1."'> <-Previous</a> ";[/code]

It may or may not be that, but I will dig though the code and see if something is missing.
Not sure, I copied and pasted exactly what you put, double check to make sure everything was closed, check all the syntax.

reuploaded and got this: Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING, expecting ',' or ';' on line 332


Line 332: echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=".$pagenum-1."'> <-Previous</a> ";

Starting to beat head off keyboard.  I am going to beat this...lol

oh, and that error is the only thing I get on the page, rest of it is white.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.