0

ihave created a simple project to help me get to grips with php and mysql, but have run into a minor issue, i have a working solution but would like to understand why i cannot run this code successfully this way, ill explain:

i have a function,

function fetch_all_movies(){
        global $connection;
        $query = 'select distinct * FROM `'.TABLE_MOVIE.'` ORDER BY movieName ASC';
        $stmt = mysqli_prepare($connection,$query);
        mysqli_execute($stmt);
        mysqli_stmt_bind_result($stmt,$id,$name,$genre,$date,$year);
        while(mysqli_stmt_fetch($stmt)){
            $editUrl = "index.php?a=editMovie&movieId=".$id."";
            $delUrl = "index.php?a=delMovie&movieId=".$id."";
            echo "<tr><td>".$id."</td><td>".$name."</td><td>".$date."</td><td>".get_actors($id)."</td><td><a href=\"".$editUrl."\">Edit</a> | <a href=\"".$delUrl."\">Delete</a></td></tr>";    
        }
    }

this fetches all the movies in my db, then i wish to get the count of actors for each film, so i pass in the get_actors($id) function which gets the movie id and then gives me the count of how many actors are realted to a film.

here is the function for that:

function get_actors($movieId){
        global $connection;
        $query = 'SELECT  DISTINCT COUNT(*) FROM `'.TABLE_ACTORS.'` WHERE movieId = "'.$movieId.'"';
        $result = mysqli_query($connection,$query);
        $row = mysqli_fetch_array($result);
        return $row[0];
    }

the functions both work perfect when called separately, i just would like to understand when i pass the function inside a function i get this warning:

Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in /Applications/MAMP/htdocs/movie_db/includes/functions.inc.php on line 287

could anyone help me understand why?

many thanks.

4
  • and how does the code that fails look like? when you "pass the function inside a function" Commented Dec 21, 2010 at 20:50
  • This would be easier to answer if you posted the code that actually caused the error, i.e. in which you pass get_actors into fetch_all_movies... Commented Dec 21, 2010 at 20:51
  • sorry i dont quite understand what you mean, could you please reiterate. Commented Dec 21, 2010 at 20:52
  • this is the code, if you notice the 1st function throws out all the movies in a table, and in one of tables td elements i pass a function to run inside it which returns the count, and the 3rd block is the error. Commented Dec 21, 2010 at 20:54

3 Answers 3

1

mysqli_query failed to run your query:

Returns FALSE on failure. For successful SELECT, SHOW, DESCRIBE or EXPLAIN queries mysqli_query() will return a result object. For other successful queries mysqli_query() will return TRUE.

Before running mysqli_fetch_array test $result... Something like:

if ($result !== false)
    $row = mysqli_fetch_array($result);
else
    return false;

Seems like a variable scope issue within your SQL statement. Outputting the SQL should show you the "true" error.

Sign up to request clarification or add additional context in comments.

Comments

0

You may want to try using classes with your functions, for example:

class getInfo {

function fetch_all_movies(){
        global $connection;
        $query = 'select distinct * FROM `'.TABLE_MOVIE.'` ORDER BY movieName ASC';
        $stmt = mysqli_prepare($connection,$query);
        mysqli_execute($stmt);
        mysqli_stmt_bind_result($stmt,$id,$name,$genre,$date,$year);
        while(mysqli_stmt_fetch($stmt)){
            $editUrl = "index.php?a=editMovie&movieId=".$id."";
            $delUrl = "index.php?a=delMovie&movieId=".$id."";
            echo "<tr><td>".$id."</td><td>".$name."</td><td>".$date."</td><td>".get_actors($id)."</td><td><a href=\"".$editUrl."\">Edit</a> | <a href=\"".$delUrl."\">Delete</a></td></tr>";    
        }
    }

function get_actors($movieId){
        global $connection;
        $query = 'SELECT  DISTINCT COUNT(*) FROM `'.TABLE_ACTORS.'` WHERE movieId = "'.$movieId.'"';
        $result = mysqli_query($connection,$query);
        $row = mysqli_fetch_array($result);
        return $row[0];
    }

}

$showInfo = new getInfo;

//fetch all movies
$showInfo->fetch_all_movies();

//List actors from movie 1
$showInfo->get_actors("1");

Comments

0

In case of an error mysqli_query will return false. You have to handle the error a simple way to do this might be:

$result = mysqli_query($connection,$query);
if (!$result) {
    die(mysqli_error($connection));
}
$row = mysqli_fetch_array($result);

Please note that terminating (by doing die() ) usually is no good way to react on an error, log it, give the user anice error page etc. It's alsonogood practice to give the low level error message toauser, this might motivate users to try to exploit a possile security issue.

Last remark: you were writing

$query = 'SELECT  DISTINCT COUNT(*) FROM `'.TABLE_ACTORS.'` WHERE movieId = "'.$movieId.'"';

you should properly escape the movieId there.

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.