Adamski
Adamski

Reputation: 6019

php functions within functions

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.

Upvotes: 0

Views: 215

Answers (3)

Kyle
Kyle

Reputation: 1733

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

Upvotes: 0

johannes
johannes

Reputation: 15969

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.

Upvotes: 0

AlexV
AlexV

Reputation: 23088

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.

Upvotes: 1

Related Questions