0

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?

3
  • You want to echo when no result found? if yes then what you want to print? use mysql_num_rows for checking empty rows. Commented Apr 4, 2014 at 8:47
  • A curly closing bracket is missing - fix that first. Commented Apr 4, 2014 at 8:49
  • 1
    Where's the if for the elseif? Commented Apr 4, 2014 at 8:49

5 Answers 5

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

Comments

0

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

  }

Comments

0

You need to use

mysql_num_rows($result)

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

Comments

0

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.

Comments

0

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

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.