wwfc_barmy_army Posted July 14, 2009 Share Posted July 14, 2009 Hello. I have this piece of code: $result2 = mysql_query("SELECT id FROM mytable WHERE user_id = " . $userid); if(!$result2){ die("Error quering the Database: " . mysql_error());} $total_items = mysql_num_rows($result2); echo "Total Number of records in Database: " . $total_items; It works, but is producing a much larger value than the database is. The value from the code above is showing 80 on page 1 and 85 on page 2. (should be exactly the same) and the table only shows 5 on page 1 and 3 on page 2. They are using virtually the same sql (the table using a wildcard to get all records) Can anyone see what I'm doing wrong? I used a tutorial similar to this to create pagination if that helps: http://mtbud420.com/?p=76 Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/165958-solved-crazy-results-from-phpsql/ Share on other sites More sharing options...
phporcaffeine Posted July 14, 2009 Share Posted July 14, 2009 We need to see all of the code please, not just the snippet you think may be relevant. Quote Link to comment https://forums.phpfreaks.com/topic/165958-solved-crazy-results-from-phpsql/#findComment-875282 Share on other sites More sharing options...
wwfc_barmy_army Posted July 14, 2009 Author Share Posted July 14, 2009 Here we go: <?php $result2 = mysql_query("SELECT * FROM inputs WHERE user_id = " . $userid); if(!$result2){ die("Error quering the Database: " . mysql_error());} $total_items = mysql_num_rows($result2); echo "Total Number of records in Database: " . $total_items; $limit= $_GET['limit']; $page= $_GET['page']; //Set default if: $limit is empty, non numerical, //less than 10, greater than 50 if((!$limit) || (is_numeric($limit) == false) || ($limit < 10) || ($limit > 50)) { $limit = 5; //default } //Set default if: $page is empty, non numerical, //less than zero, greater than total available if((!$page) || (is_numeric($page) == false) || ($page < 0) || ($page > $total_items)) { $page = 1; //default } //calcuate total pages $total_pages = ceil($total_items / $limit); $set_limit = ($page * $limit) - $limit; echo '' . $set_limit . ''; ?> <table width="80%" border="1" class="sortable"> <thead> //////// ////////.. Table Headers ... //////// </thead> <?php $sql = "SELECT * FROM inputs WHERE user_id = " . $userid . " LIMIT " . $set_limit . ", " . $limit; $result = mysql_query($sql) or die(mysql_error()); while($row = mysql_fetch_array($result)){ // //...Some outputs...// ?> <tr> //...Some outputs...// </tr> <?php } echo "</table>"; //prev. page: $prev_page = $page - 1; if($prev_page >= 1) { echo("<a href='?limit=$limit&page=$prev_page'><< Prev</a>"); } //Display middle pages: for($a = 1; $a <= $total_pages; $a++) { if($a == $page) { echo(" $a | "); //no link } else { echo(" <a href='?limit=$limit&page=$a'>$a</a> | "); } } //next page: $next_page = $page + 1; if($next_page <= $total_pages) { echo("<a href='?limit=$limit&page=$next_page'> Next > > </a>"); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/165958-solved-crazy-results-from-phpsql/#findComment-875285 Share on other sites More sharing options...
wildteen88 Posted July 14, 2009 Share Posted July 14, 2009 This code is not needed at all. Just remove it $result2 = mysql_query("SELECT * FROM inputs WHERE user_id = " . $userid); if(!$result2){ die("Error quering the Database: " . mysql_error());} $total_items = mysql_num_rows($result2); echo "Total Number of records in Database: " . $total_items; Everything else is fine. However the code could be tidied up a bit. Quote Link to comment https://forums.phpfreaks.com/topic/165958-solved-crazy-results-from-phpsql/#findComment-875289 Share on other sites More sharing options...
wwfc_barmy_army Posted July 14, 2009 Author Share Posted July 14, 2009 This code is not needed at all. Just remove it $result2 = mysql_query("SELECT * FROM inputs WHERE user_id = " . $userid); if(!$result2){ die("Error quering the Database: " . mysql_error());} $total_items = mysql_num_rows($result2); echo "Total Number of records in Database: " . $total_items; Everything else is fine. However the code could be tidied up a bit. But i need that code there to be able to get the total number of records at the top of the page. Quote Link to comment https://forums.phpfreaks.com/topic/165958-solved-crazy-results-from-phpsql/#findComment-875290 Share on other sites More sharing options...
wildteen88 Posted July 14, 2009 Share Posted July 14, 2009 Oh.. I thought it was just some random code. What is the problem you are having? Quote Link to comment https://forums.phpfreaks.com/topic/165958-solved-crazy-results-from-phpsql/#findComment-875295 Share on other sites More sharing options...
wwfc_barmy_army Posted July 14, 2009 Author Share Posted July 14, 2009 The output from the total records is coming out a lot more than it should be. Was 80 (first page of paginiation) and 85 (second page), but now i've added 1 more record it's gone up to 90 (first page) and 95 (second Page). Should be 8 and 9. Any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/165958-solved-crazy-results-from-phpsql/#findComment-875300 Share on other sites More sharing options...
wwfc_barmy_army Posted July 14, 2009 Author Share Posted July 14, 2009 Ahhh, spotted it: echo '' . $set_limit . ''; was coming after the correct number as there was no line space inbetween. Thanks anyways guys, knew it was something stupid lol. ::edit:: I tend to tidy up my code after i get it working (bad habit lol) Quote Link to comment https://forums.phpfreaks.com/topic/165958-solved-crazy-results-from-phpsql/#findComment-875303 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.