Reputation: 15
I got the following code:
if(isset($_SESSION['user_id'])) { $query = "SELECT * FROM watchlist,movies WHERE watchlist.page=movies.id AND userid=". $_SESSION['user_id'];
$result = mysql_query($query) or die('Query failed: ' . mysql_error());
while( $row = mysql_fetch_assoc($result) )
{
echo '<div id="watchlist">';
echo '<a href="watch.php?id='.$row['page'].'"><img src="http://nocmmnt.com/posters/'.$row['imdb_id'].'.jpg" height="217" width="154"></a><br>';
echo '<span style="float:right;"><a href="delete.php?wid='.$row['wid'].'"><img src="image/close_delete.png" width="20"></a></span><a href="watch.php?id='.$row['page'].'">'.$row['title'].'</a><br>';
echo "</div>";
}
elseif(empty($row)) {
echo "You dont have any movies in your watchlist!";
}
My problem is that is doesnt echo when its empty how can I fix that?
Upvotes: 0
Views: 107
Reputation: 3038
You could modify your code as below, also try to use mysqli instead of mysql
<?php
if(isset($_SESSION['user_id'])) { $query = "SELECT * FROM watchlist,movies WHERE watchlist.page=movies.id AND userid=". $_SESSION['user_id'];
$result = mysql_query($query) or die('Query failed: ' . mysql_error());
$count = mysql_num_rows($result);
if($count > 0 )
{
while( $row = mysql_fetch_assoc($result) )
{
echo '<div id="watchlist">';
echo '<a href="watch.php?id='.$row['page'].'"><img src="http://nocmmnt.com/posters/'.$row['imdb_id'].'.jpg" height="217" width="154"></a><br>';
echo '<span style="float:right;"><a href="delete.php?wid='.$row['wid'].'"><img src="image/close_delete.png" width="20"></a></span><a href="watch.php?id='.$row['page'].'">'.$row['title'].'</a><br>';
echo "</div>";
}
}
else {
echo "You dont have any movies in your watchlist!";
}
}
?>
Upvotes: 0
Reputation: 46900
elseif(empty($row)) {
That's wrong conditional structure, an elseif
must come after an if
block or another elseif
.
How can that code not generate a parse error for you?
Parse error: syntax error, unexpected T_ELSEIF on line 13
Also, as others mentioned, simply check for row count being 0
if(mysql_num_rows($result)==0)
Upvotes: 1
Reputation: 37233
Try that :
if(mysql_num_rows($result) == 0)
{ echo "You dont have any movies in your watchlist!"; }
else{
while( $row = mysql_fetch_assoc($result) )
{
echo '<div id="watchlist">';
echo '<a href="watch.php?id='.$row['page'].'"><img src="http://nocmmnt.com/posters/'.$row['imdb_id'].'.jpg" height="217" width="154"></a><br>';
echo '<span style="float:right;"><a href="delete.php?wid='.$row['wid'].'"><img src="image/close_delete.png" width="20"></a></span><a href="watch.php?id='.$row['page'].'">'.$row['title'].'</a><br>';
echo "</div>";
}
}
Upvotes: 0
Reputation: 799
Checking empty($row)
doesn't do what you think it does. What you want to check is mysql_num_rows($result) == 0
.
A sidenote - mysql
is deprecated in favor of mysqli
.
Upvotes: 0
Reputation: 44844
You need to use
mysql_num_rows($result)
to check if there are 0 rows or more than that.
Upvotes: 0