0

I am trying to get pagination working on my website. Everything should be working but this only displays one product, however that should be 8 products. Anybody know what's wrong? Thanks in advance.

else {
$query = "SELECT COUNT(*) FROM products";
$result = mysql_query($query) or die(mysql_error());
$num_rows = mysql_fetch_row($result);
$pages = new Paginator;  
$pages->items_total = $num_rows[0];  
$pages->mid_range = 9;  
$pages->paginate();  

$query = "SELECT serial, name, description, price, picture FROM products WHERE serial != '' ORDER BY serial ASC $pages->limit";
$result = mysql_query($query) or die(mysql_error());
$row = mysql_fetch_row($result);
{
echo '<div style="margin-bottom:10px;display:inline-block;background-color:#E3E3E3;width:190px;height:200px;"><a href="'.$_SERVER['PHP_SELF'].'?serial='.$row[0].'"><img style="padding-top:10px;padding-left:25px;width:150px;height:150px;" src="'.htmlspecialchars($row[4]).'"></a><br><div align="center"><b>'.htmlspecialchars($row[1]).'</b><br><h6>&euro;'.htmlspecialchars($row[3]).'</h6></div></div>';
};
echo '&nbsp;';
echo '<br><br><div style="margin-left:330px;">';
echo $pages->display_pages();
echo '</div>';
}
?>      
Thakkennes
  • 215
  • 2
  • 3
  • 12

4 Answers4

3

mysql_fetch_row() only fetches one row at a time. You need to call it repeatedly to display all rows, like this:

while ($row = mysql_fetch_row($result)) {
    // handle single row
}

I suggest you consider using mysql_fetch_array() instead. Then you will not have to rely on the order of the columns anymore and your code becomes more legible: $row[0] becomes $row["serial"] etc.

Try reading the articles that @Kush linked. See here for a PHP-specific discussion.

Community
  • 1
  • 1
phant0m
  • 16,595
  • 5
  • 50
  • 82
1

You do not seem to be using mysql_fetch_row() properly


The following code is vulnerable to SQL injection because $page->limit does not appear to be sanitized

$query = "SELECT serial, name, description, price, picture FROM products WHERE serial != '' ORDER BY serial ASC $pages->limit";

Stop using mysql_* functions as they're deprecated. Use PHP database objects (PDO) instead because PDO allows parameter binding which protects you from sql injection.

You can read up on using PDO here

Kush
  • 1,512
  • 2
  • 17
  • 32
0

because of this line

$row = mysql_fetch_row($result);

should be

while($row = mysql_fetch_row($result)){...
k102
  • 7,861
  • 7
  • 49
  • 69
0

Change the line

$row = mysql_fetch_row($result); 

to

while ($row = mysql_fetch_row($result))
Anthony
  • 12,177
  • 9
  • 69
  • 105
bkwint
  • 636
  • 4
  • 9