0

I have a page where I am using a parameter in the URL as a filter for my SQL query. I created a variable from the URL parameter:

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

Then set up a conditional query depending on whether or not the URL parameter is set:

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. Does anybody know why this is happening?

Thank you

8

1 Answer 1

-1

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);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.