【发布时间】:2012-11-29 09:21:54
【问题描述】:
我想将数据库中的结果分成每行 3 个结果。这是我的代码:
include("connect.php");
$result = mysql_query("SELECT * FROM movies ORDER BY title");
$num = mysql_num_rows ($result);
if ($num > 0 ) {
$i=0;
while ($i < $num) {
$id = stripslashes(mysql_result($result,$i,"id"));
$title = stripslashes(mysql_result($result,$i,"title"));
$description = stripslashes(mysql_result($result,$i,"description"));
$length = stripslashes(mysql_result($result,$i,"length"));
$link = stripslashes(mysql_result($result,$i,"link"));
$rating = stripslashes(mysql_result($result,$i,"rating"));
$cover = stripslashes(mysql_result($result,$i,"icover"));
$row .= '<tr><td><a href="view.php?id='.$id.'"><img width=130 height=190 src="images/covers/'.$cover.'" /></a></td><td valign="top">'.$title.'<br />'.$rating.'<br />Run Time: '.$length.'</td><td><a href="update.php?id='.$id.'">Update</a></td><td><a href="delete.php?id='.$id.'">Delete</a></td></tr>';
++$i;
}
}
else {
$row = '<tr><td colspan="2" align="center">Nothing found</td></tr>';
}
mysql_close();
【问题讨论】:
-
您能否举一个查询结果的示例以及您希望它的格式?您的消息来源并不太清楚您要做什么。
-
模数运算符是你的朋友。
-
请不要将 mysql_* 函数用于新代码。它们不再维护,社区已经开始deprecation process。看到red box?相反,您应该了解prepared statements 并使用PDO 或MySQLi。如果您不能决定,this article 将帮助您选择。如果你想学习,这里是good PDO tutorial。
-
@JohnConde 它们现在实际上已被正式弃用。 Here's更新的不要使用ext mysql评论
-
@Nick Fury,感谢您的更新!