[英]PHP MYSQL Table only display one row of results
我有一个页面,其中使用URL中的参数作为SQL查询的过滤器。 我从URL参数创建了一个变量:
$station = htmlspecialchars($_GET["station"]);
然后根据是否设置了URL参数来设置条件查询:
if(isset($_GET['station'])) {
$query = "SELECT * FROM song_of_the_day WHERE station = '$station'";
}
else {
$query = "SELECT * FROM song_of_the_day WHERE end_date >= CURDATE()";
}
$result = mysql_query($query) or die(mysql_error());
$num_rows = mysql_fetch_row($result);
然后,我在表中显示结果:
echo "<table width='758' border='0' cellpadding='10' cellspacing='0' class='myTable'>";
while($row = mysql_fetch_array( $result )) {
echo '<tr>';
echo '<td align="left" width="48">' . $row['station'] . '</td>';
echo '<td align="left">' . date('M j, Y',strtotime($row['end_date'])) . '</td>';
echo '<td width="24" align="left"><a href="edit.php?id=' . $row['id'] . '"><img src="http://yourligas.yourli.com/ad-inventory/edit.png" border="0"></a></td>';
echo '<td width="24" align="left"><a href="delete.php?id=' . $row['id'] . '" data-confirm="Are you sure you want to delete this entry?" ><img src="http://yourligas.yourli.com/ad-inventory/remove.png" border="0"></a></td>';
echo "</tr>";
echo '</tbody>';
}
echo "</table>";
当ELSE命令使用查询时,查询工作会找到,我不依赖SQL中的参数,但是当查询到的URL参数ISSET只有一行时,我看到的问题是从查询中显示一行符合实际数据库中条件的一行。 有人知道为什么会这样吗?
谢谢
在此行中,您似乎在消耗第一行数据时试图获取找到的行数:
$num_rows = mysql_fetch_row($result);
这将从结果游标中删除第一行。
相反,您可能打算执行以下操作:
$num_rows = mysql_num_rows($result);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.