简体   繁体   English

PHP MYSQL表仅显示一行结果

[英]PHP MYSQL Table only display one row of results

I have a page where I am using a parameter in the URL as a filter for my SQL query. 我有一个页面,其中使用URL中的参数作为SQL查询的过滤器。 I created a variable from the URL parameter: 我从URL参数创建了一个变量:

$station = htmlspecialchars($_GET["station"]);

Then set up a conditional query depending on whether or not the URL parameter is set: 然后根据是否设置了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);

Then I display the results in a table: 然后,我在表中显示结果:

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>";

The query works find when the ELSE command uses the query, where I'm not relying on the parameter in my SQL, but the problem I am seeing when the URL parameter ISSET is only one row gets displayed from the query when there is more than one row that matches the criteria in the actual database. 当ELSE命令使用查询时,查询工作会找到,我不依赖SQL中的参数,但是当查询到的URL参数ISSET只有一行时,我看到的问题是从查询中显示一行符合实际数据库中条件的一行。 Does anybody know why this is happening? 有人知道为什么会这样吗?

Thank you 谢谢

In this line, you appear to be consuming the first row of data while attempting to get the number of rows found: 在此行中,您似乎在消耗第一行数据时试图获取找到的行数:

$num_rows = mysql_fetch_row($result);

This removes the first row from your result cursor. 这将从结果游标中删除第一行。

Instead, you probably meant to do the following: 相反,您可能打算执行以下操作:

$num_rows = mysql_num_rows($result);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM