dongxing5525 2017-08-06 20:33
浏览 38
已采纳

mysqli $ stmt - call_user_func_array期望参数1是有效的回调[重复]

This question already has an answer here:

I'm having these problems no matter how much i tried to fix them. I'm working on an Html Visual Editor CMS and everytime i login these errors showup. Hope this post is clear and easy to understand.. if not ask me and i'll try my best to answer you. Here are the errors:

call_user_func_array() expects parameter 1 to be a valid callback, first array member is not a valid class name or object
Call to a member function execute() on boolean

Here's the code:

class database{
    public $root_db = 'cms';
    public $server_name = 'localhost:3306';
    public $root_db_user = 'root';
    public $root_db_pass = '';
    public $connection;
    public $error = false;

    function connect_to_db(){
        $this->connection = new mysqli($this->server_name,$this->root_db_user,$this->root_db_pass,$this->root_db);

        if($this->connection->connect_error){
            $this->error = "Connection Failed: " . $this->connection->connect_error;
        }
    }

    function executeCleanQuery($query,$params){
        $type_string = '';
        $param_arr = array();

        if(!empty($params)){
            for($i = 0; $i < count($params); $i++){
                $type = gettype($params[$i]);
                $type_string .= $type[0];

                $param_arr[$i] = $this->connection->real_escape_string(htmlentities($params[$i]));
            }

            array_unshift($param_arr,$type_string);

            if(!$stmt = $this->connection->prepare($query))
                $error = "Unable to prepare statement";

            call_user_func_array(array($stmt,"bind_param") ,$this->refValues($param_arr));

            if (!$stmt->execute())
                $error = "Unable to execute statement";

            $result = $stmt->get_result();

            if($result && (strstr($query,"SELECT") || strstr($query,"select")))
                return $result->fetch_all(MYSQLI_ASSOC);
            else{
                return $this->connection->insert_id;
            }
        }else if(strstr($query,"SELECT") || strstr($query,"select")){
            $result = $this->connection->query($query);
            return $result->fetch_all(MYSQLI_ASSOC);
        }else{
            return true;
        }
    }

    function refValues($arr){
        if (strnatcmp(phpversion(),'5.3') >= 0) //Reference is required for PHP 5.3+
        {
            $refs = array();
            foreach($arr as $key => $value)
                $refs[$key] = &$arr[$key];
            return $refs;
        }
        return $arr;
}
}

And Thanks in advance.

</div>
  • 写回答

1条回答 默认 最新

  • doutenggu4070 2017-08-06 20:39
    关注

    The error message is clear. You should learn to read error messages, process them, and act on the error message.

    call_user_func_array() expects parameter 1 to be a valid callback, first array member is not a valid class name or object Call to a member function execute() on boolean

    The first part of the error _call_user_func_array()_ tells you exactly where to find the error. Given your code, it can only be on the line indicated below (although error messages also include line numbers, so using the line number would be useful as well):

    if(!$stmt = $this->connection->prepare($query))
        $error = "Unable to prepare statement";
    
    // this is the only "call_user_func_array" in your code, so must be the issue
    call_user_func_array(array($stmt,"bind_param"), $this->refValues($param_arr));
    

    The next part of the error - first array member is not a valid class name or object. Call to a member function execute() on boolean tells you that specifically, the array( $stmt, "bind_param" ) is not a valid function, because $stmt is boolean.

    The reason that's throwing is because although you check if $stmt is set, you don't actually prevent continuing operation if it is not set.

    While a "band-aid" solution to demonstrate how to handle it (a robust solution would include better error handling), you can solve this if you alter your code like below:

    if( ! $stmt = $this->connection->prepare( $query ) )
        $error = "Unable to prepare statement";
        // stop here, $stmt is not set, cannot continue
        die();
    }
    
    call_user_func_array(array($stmt,"bind_param"), $this->refValues($param_arr));
    

    Lastly, to follow through on why this error is happening - almost certainly the value in $query is not a valid query, and is the culprit.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分