查询计数和功能无法正常工作
我使用下面的代码来选择项目并使用 $result 变量进行计数。如果少于 1 个,则会显示“添加更多”;如果超过 5 个,则会显示“查看全部”。它适用于少于 1 个,但不适用于超过 5 个。我这样做对吗?
//询问
$sql = "SELECT id, name, why, date_time
FROM tabs
WHERE p_id = '$pid'
ORDER BY id
LIMIT 0, 5";
$result = mysql_query($sql);
$query = mysql_query($sql) or die ("Error: ".mysql_error());
if ($result == "") {
echo "";
}
echo "";
$rows = mysql_num_rows($result);
if($rows == 0) {
print("");
} elseif($rows > 0) {
while($row = mysql_fetch_array($query)) {
$name = $row['name'];
$w = nl2br($row['why']);
$y = $row['date_time'];
print("echoing contents here");
}
}
if(mysql_num_rows($result) > 5) {
echo "view all";
}
if(mysql_num_rows($result) < 1) {
echo "add one";
} ?>
I am using the code below to select items and using the $result variable to do a count. if there are less than 1 it will say add more and if there are more than 5 it will say view all. It works for less than 1 but it won't for more than 5. Am i doing it right?
//Query
$sql = "SELECT id, name, why, date_time
FROM tabs
WHERE p_id = '$pid'
ORDER BY id
LIMIT 0, 5";
$result = mysql_query($sql);
$query = mysql_query($sql) or die ("Error: ".mysql_error());
if ($result == "") {
echo "";
}
echo "";
$rows = mysql_num_rows($result);
if($rows == 0) {
print("");
} elseif($rows > 0) {
while($row = mysql_fetch_array($query)) {
$name = $row['name'];
$w = nl2br($row['why']);
$y = $row['date_time'];
print("echoing contents here");
}
}
if(mysql_num_rows($result) > 5) {
echo "view all";
}
if(mysql_num_rows($result) < 1) {
echo "add one";
} ?>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
应该是
因为您用前面的
mysql_fetch_array()
循环耗尽了结果集Should be
Since you exhausted the result set with the previous
mysql_fetch_array()
loop