简体   繁体   English

函数内的php函数

[英]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: 我已经创建了一个简单的项目来帮助我掌握php和mysql,但是遇到了一个小问题,我有一个可行的解决方案,但想了解为什么我无法以这种方式成功运行此代码,请病假解释:

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. 这将获取我数据库中的所有电影,然后我希望获取每部电影的演员人数,因此我传入了get_actors($ id)函数,该函数获取了影片ID,然后给出了实现的演员人数看电影。

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 警告:mysqli_fetch_array()期望参数1为mysqli_result,在第287行的/Applications/MAMP/htdocs/movie_db/includes/functions.inc.php中给出的布尔值

could anyone help me understand why? 谁能帮我理解为什么?

many thanks. 非常感谢。

mysqli_query failed to run your query: mysqli_query无法运行您的查询:

Returns FALSE on failure. 失败时返回FALSE。 For successful SELECT, SHOW, DESCRIBE or EXPLAIN queries mysqli_query() will return a result object. 对于成功的SELECT,SHOW,DESCRIBE或EXPLAIN查询,mysqli_query()将返回结果对象。 For other successful queries mysqli_query() will return TRUE. 对于其他成功的查询,mysqli_query()将返回TRUE。

Before running mysqli_fetch_array test $result... Something like: 在运行mysqli_fetch_array之前,测试$ result ...类似:

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

Seems like a variable scope issue within your SQL statement. 似乎是SQL语句中的变量作用域问题。 Outputting the SQL should show you the "true" error. 输出SQL将显示“ true”错误。

In case of an error mysqli_query will return false. 如果发生错误,mysqli_query将返回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. 请注意,终止(通过执行die())通常不是对错误做出反应,记录错误,为用户提供anice错误页面等的好方法。向用户提供低级错误消息也不是一种好习惯,这可能会激发用户尝试利用可能的安全性问题。

Last remark: you were writing 最后一句话:你在写

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

you should properly escape the movieId there. 您应该在那里正确地避开movieId。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM