php mysql code won't echo
854
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 class="page_speed_936190246"><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?
Answer
Solution:
That's wrong conditional structure, an
elseif
must come after anif
block or anotherelseif
.How can that code not generate a parse error for you?
Also, as others mentioned, simply check for row count being 0
Answer
Solution:
Try that :
Answer
Solution:
You need to use
to check if there are 0 rows or more than that.
Answer
Solution:
Checking
empty($row)
doesn't do what you think it does. What you want to check ismysql_num_rows($result) == 0
.A sidenote -
mysql
is deprecated in favor of.
Answer
Solution:
You could modify your code as below, also try to use mysqli instead of mysql