mysql_num_rows() Problem

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
tito85
Forum Contributor
Posts: 104
Joined: Sat Mar 13, 2010 11:26 am

mysql_num_rows() Problem

Post 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>";
?>
User avatar
markusn00b
Forum Contributor
Posts: 298
Joined: Sat Oct 20, 2007 2:16 pm
Location: York, England

Re: mysql_num_rows() Problem

Post 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.
tito85
Forum Contributor
Posts: 104
Joined: Sat Mar 13, 2010 11:26 am

Re: mysql_num_rows() Problem

Post 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...
pjcvijay
Forum Commoner
Posts: 25
Joined: Tue Sep 09, 2008 6:12 am

Re: mysql_num_rows() Problem

Post 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";
tito85
Forum Contributor
Posts: 104
Joined: Sat Mar 13, 2010 11:26 am

Re: mysql_num_rows() Problem

Post 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...
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: mysql_num_rows() Problem

Post by AbraCadaver »

Code: Select all

echo $movies2['GenreID'];
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
tito85
Forum Contributor
Posts: 104
Joined: Sat Mar 13, 2010 11:26 am

Re: mysql_num_rows() Problem

Post by tito85 »

AbraCadaver wrote:

Code: Select all

echo $movies2['GenreID'];
Sorry, but what do you mean?
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: mysql_num_rows() Problem

Post 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:
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
tito85
Forum Contributor
Posts: 104
Joined: Sat Mar 13, 2010 11:26 am

Re: mysql_num_rows() Problem

Post 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.
Post Reply