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?

268

Answer

Solution:

elseif(empty($row)) {

That's wrong conditional structure, anelseif must come after anif block or anotherelseif.

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)
205

Answer

Solution:

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

  }
209

Answer

Solution:

You need to use

mysql_num_rows($result)

to check if there are 0 rows or more than that.

944

Answer

Solution:

Checkingempty($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 .

Write your answer




983
votes

Answer

Solution:

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 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>";
        }
    }
    else {
        echo "You dont have any movies in your watchlist!";
    }
}
?>

People are also looking for solutions to the problem: Java to PHP - function for X509EncodedKeySpec

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.