0

我正在尝试在我的网站上进行分页。一切都应该正常工作,但这仅显示一种产品,但应该是 8 种产品。有人知道出了什么问题吗?提前致谢。

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>';
}
?>      
4

4 回答 4

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.

于 2012-07-24T09:46:48.483 回答
1

您似乎没有正确使用mysql_fetch_row()


以下代码易受SQL 注入攻击,因为 $page->limit 似乎没有被清理

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

停止使用 mysql_* 函数,因为它们已被弃用。请改用PHP 数据库对象 (PDO),因为 PDO 允许参数绑定,从而保护您免受 sql 注入。

您可以在此处阅读有关使用 PDO的信息

于 2012-07-24T09:50:02.793 回答
0

because of this line

$row = mysql_fetch_row($result);

should be

while($row = mysql_fetch_row($result)){...
于 2012-07-24T09:46:48.890 回答
0

换行

$row = mysql_fetch_row($result); 

while ($row = mysql_fetch_row($result))
于 2012-07-24T09:47:39.957 回答