SkyRanger Posted October 17, 2006 Share Posted October 17, 2006 [code]//start Top Pagination Codeif (!(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"]; ?> <b>|</b> <b>Age:</b> <? echo $row["age"]; ?> <b>|</b> <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 spacerecho " ---- ";//This does the same as above, only checking if we are on the last page, and then generating the Next and Last linksif ($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? Quote Link to comment Share on other sites More sharing options...
SkyRanger Posted October 17, 2006 Author Share Posted October 17, 2006 Does anybody have an idea of why I am getting this error? Quote Link to comment Share on other sites More sharing options...
SkyRanger Posted October 17, 2006 Author Share Posted October 17, 2006 I am getting a feeling by the look of people who have viewed this post is just as lost as I am. Well thanks guys for trying to help, but if you can come up with something please let me know. Quote Link to comment Share on other sites More sharing options...
rab Posted October 17, 2006 Share Posted October 17, 2006 Well are you connected to a mysql database? Quote Link to comment Share on other sites More sharing options...
SkyRanger Posted October 17, 2006 Author Share Posted October 17, 2006 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 Quote Link to comment Share on other sites More sharing options...
rab Posted October 17, 2006 Share Posted October 17, 2006 [code=php:0]$result = mysql_query( "SELECT * FROM quest where auth='1' order by 'id' desc $max");if( $result === FALSE ) { print "MySQL Error: "; mysql_error($result); }[/code]I'm pretty sure that that might be the problem Quote Link to comment Share on other sites More sharing options...
SkyRanger Posted October 18, 2006 Author Share Posted October 18, 2006 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//addedLine: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 287Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in public_html/poster.php on line 288Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in public_html/poster.php on line 290 Quote Link to comment Share on other sites More sharing options...
rab Posted October 18, 2006 Share Posted October 18, 2006 gah, stupid mistake. give mysql_error() the mysql resource link Quote Link to comment Share on other sites More sharing options...
SkyRanger Posted October 18, 2006 Author Share Posted October 18, 2006 [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...lolNot 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 Codeif (!(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"]; ?> <b>|</b> <b>Age:</b> <? echo $row["age"]; ?> <b>|</b> <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 spacerecho " ---- ";//This does the same as above, only checking if we are on the last page, and then generating the Next and Last linksif ($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 288Warning: 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] Quote Link to comment Share on other sites More sharing options...
rab Posted October 18, 2006 Share Posted October 18, 2006 Try this, there __may__ be some typos [code=php:0]<?phpinclude "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 DBif( !$db ) // Error Checking{ die("Error selecting a database: ". mysql_error($connection));}// Whats $pagenum? Defined? Noif( !( 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 spacerecho " ---- ";//This does the same as above, only checking if we are on the last page, and then generating the Next and Last linksif( $pagenum != $last ) { echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=".$pagenum+1."'>Next -></a> "; echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$last'>Last ->></a> ";} [/code] Quote Link to comment Share on other sites More sharing options...
SkyRanger Posted October 18, 2006 Author Share Posted October 18, 2006 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. Quote Link to comment Share on other sites More sharing options...
rab Posted October 18, 2006 Share Posted October 18, 2006 Could be, I take it that the script worked. I think your problem was when you were selecting a database you didnt supply a MySQL link ID. Quote Link to comment Share on other sites More sharing options...
SkyRanger Posted October 18, 2006 Author Share Posted October 18, 2006 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 332Line 332: echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=".$pagenum-1."'> <-Previous</a> ";Starting to beat head off keyboard. I am going to beat this...loloh, and that error is the only thing I get on the page, rest of it is white. Quote Link to comment Share on other sites More sharing options...
xsist10 Posted October 18, 2006 Share Posted October 18, 2006 [code]echo " <a href='". $_SERVER['PHP_SELF'] ."?pagenum=".$pagenum-1."'> <-Previous</a> ";[/code] Quote Link to comment Share on other sites More sharing options...
JasonLewis Posted October 18, 2006 Share Posted October 18, 2006 if you ask me, that is an extremely long pagination code. it could be a lot simplier in the same style. Quote Link to comment Share on other sites More sharing options...
SkyRanger Posted October 19, 2006 Author Share Posted October 19, 2006 Thanks guys, I got it working, with alot smaller script, had alot of useless garbage in there. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.