Page 1 of 1

mysql_num_rows() Problem

Posted: Fri Jun 04, 2010 6:17 am
by tito85
Hi

I am getting this Warning when running this code.

Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in C:\wamp\www\movie\moviedetails.php on line 306 where line 306 is:

Code: Select all

if (mysql_num_rows($recommendations) > 0) {
Here is the part of the code

Code: Select all

<?php
                  echo "<div class=\"blog2\"><h4 style=\"margin: 0\">We Recommend this Movie for you:</h4><p />";
                  echo "<hr style=\"border: 1px solid #06C\" />";
                 $query = "SELECT genretypes.*, genres.*, movies.* FROM movies INNER JOIN genres ON genres.MovieID = movies.MovieID INNER JOIN genretypes ON genres.GenreTypeID = genretypes.GenreTypeID WHERE genretypes.GenreTypeID = " . $movies2['GenreID'] . " AND Rating != 0 ORDER BY RAND() LIMIT 0,5";
               $recommendations = mysql_query($query);
                  if (mysql_num_rows($recommendations) > 0) {
                      echo "<table align=\"center\"><tr>";
                      while ($recommendation = mysql_fetch_array($recommendations)) {
                          echo "<td><a href=\"moviedetails.php?id=" . $recommendation['MovieID'] . "\">" . '<img src="Images/Movies/' . $recommendation['Filename'] . '" width="125" height="185" />' . "</a></td>";
                      }
                      echo "</tr></table>";
                  }
                  echo "</div>";
?>

Re: mysql_num_rows() Problem

Posted: Fri Jun 04, 2010 6:23 am
by markusn00b
mysql_query() will return FALSE on failure, and return a resource otherwise.

Check the return of your query. If it fails, investigate the error. Otherwise, do whatever.

Code: Select all

if (!$recommendation) {
    echo mysql_error();
}
else {
    $num_rows = mysql_num_rows($recommendation);
}
Mark.

Re: mysql_num_rows() Problem

Posted: Fri Jun 04, 2010 7:00 am
by tito85
It seems that the problem is in this part of the query:

Code: Select all

" . $movies2['GenreID'] . "
The error says:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'AND Rating != 0 ORDER BY RAND() LIMIT 0,5' at line 1

If I use a fixed number it work but using the variable the error comes out...

Any idea because I cannot figure it out...

Re: mysql_num_rows() Problem

Posted: Fri Jun 04, 2010 8:01 am
by pjcvijay
Hi,
Echo the query you have used in your code, and just run that query on Mysql database, you use, and see what that query returns.
This way you can solve the issue.

Code: Select all

$query = "SELECT genretypes.*, genres.*, movies.* FROM movies INNER JOIN genres ON genres.MovieID = movies.MovieID INNER JOIN genretypes ON genres.GenreTypeID = genretypes.GenreTypeID WHERE genretypes.GenreTypeID = " . $movies2['GenreID'] . " AND Rating != 0 ORDER BY RAND() LIMIT 0,5";

Re: mysql_num_rows() Problem

Posted: Fri Jun 04, 2010 8:20 am
by tito85
pjcvijay wrote:Hi,
Echo the query you have used in your code, and just run that query on Mysql database, you use, and see what that query returns.
This way you can solve the issue.

Code: Select all

$query = "SELECT genretypes.*, genres.*, movies.* FROM movies INNER JOIN genres ON genres.MovieID = movies.MovieID INNER JOIN genretypes ON genres.GenreTypeID = genretypes.GenreTypeID WHERE genretypes.GenreTypeID = " . $movies2['GenreID'] . " AND Rating != 0 ORDER BY RAND() LIMIT 0,5";
I did that in MySQL already but replaced " . $movies2['GenreID'] . " with a fixed number and it works fine. It seems like I have some mistake in the syntax that I cannot figure out...

Re: mysql_num_rows() Problem

Posted: Fri Jun 04, 2010 11:05 am
by AbraCadaver

Code: Select all

echo $movies2['GenreID'];

Re: mysql_num_rows() Problem

Posted: Fri Jun 04, 2010 11:14 am
by tito85
AbraCadaver wrote:

Code: Select all

echo $movies2['GenreID'];
Sorry, but what do you mean?

Re: mysql_num_rows() Problem

Posted: Fri Jun 04, 2010 11:15 am
by AbraCadaver
tito85 wrote:
AbraCadaver wrote:

Code: Select all

echo $movies2['GenreID'];
Sorry, but what do you mean?
I mean, echo that variable and see what it contains! :banghead:

Re: mysql_num_rows() Problem

Posted: Fri Jun 04, 2010 11:16 am
by tito85
AbraCadaver wrote:
tito85 wrote:
AbraCadaver wrote:

Code: Select all

echo $movies2['GenreID'];
Sorry, but what do you mean?
I mean, echo that variable and see what it contains! :banghead:
I did echo that variable. It contains the ID of the Genre that I want to base the select query on. If i write the ID manully instead of using the variable it works.