Jump to content

[SOLVED] Crazy results from php/Sql


wwfc_barmy_army

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/165958-solved-crazy-results-from-phpsql/
Share on other sites

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>");
}
  ?>

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.

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.

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?

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)

 

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.