Php $db->;prepare()在执行时给我一个错误

Php $db->;prepare()在执行时给我一个错误,php,Php,我使用时没有任何问题: $db->query(“更新tbl\u订单集订单\u代码=”。$s\u odr\u行['od\u凭证\u后缀]。“'where order\u id=”。“$order\u id.”); 但大家都知道,像这样使用是非常危险的,所以我决定重写如下: $smtp = $db->prepare("UPDATE tbl_orders SET order_code = ? WHERE order_id = ?"); $smtp->bind_para

我使用时没有任何问题:

$db->query(“更新tbl\u订单集订单\u代码=”。$s\u odr\u行['od\u凭证\u后缀]。“'where order\u id=”。“$order\u id.”);
但大家都知道,像这样使用是非常危险的,所以我决定重写如下:

$smtp = $db->prepare("UPDATE tbl_orders SET order_code = ? WHERE order_id = ?");
$smtp->bind_param("ss", $s_odr_row['od_voucher_suffixes'], $order_id);
$smtp->execute();
Database Object (
    [con:Database:private] => SingletonMysqli Object (
        [affected_rows] => 1
        [client_info] => mysqlnd 5.0.12-dev - 20150407 - $Id: 7cc7cc96e675f6d72e5cf0f267f48e167c2abb23 $
        [client_version] => 50012
        [connect_errno] => 0
        [connect_error] =>
        [errno] => 0
        [error] =>
        [error_list] => Array ( )
        [field_count] => 1
        [host_info] => Localhost via UNIX socket
        [info] =>
        [insert_id] => 0
        [server_info] => 8.0.23-0ubuntu0.20.04.1
        [server_version] => 80023
        [stat] => Uptime: 4805192 Threads: 6 Questions: 44797749 Slow queries: 0 Opens: 8612 Flush tables: 3 Open tables: 3839 Queries per second avg: 9.322
        [sqlstate] => 00000
        [protocol_version] => 10
        [thread_id] => 641720
        [warning_count] => 0
    )
    [error:Database:private] =>
    [queries_to_log:Database:private] =>
    [query_log_file:Database:private] =>
)
但我在使用预处理语句时出错:

致命错误:未捕获错误:在第340行的/home/deals/public_html/success.php中调用未定义的方法Database::prepare():340堆栈跟踪:#0{main}抛出/home/deals/public_html/success.php`

我尝试回显$db,结果如下:

$smtp = $db->prepare("UPDATE tbl_orders SET order_code = ? WHERE order_id = ?");
$smtp->bind_param("ss", $s_odr_row['od_voucher_suffixes'], $order_id);
$smtp->execute();
Database Object (
    [con:Database:private] => SingletonMysqli Object (
        [affected_rows] => 1
        [client_info] => mysqlnd 5.0.12-dev - 20150407 - $Id: 7cc7cc96e675f6d72e5cf0f267f48e167c2abb23 $
        [client_version] => 50012
        [connect_errno] => 0
        [connect_error] =>
        [errno] => 0
        [error] =>
        [error_list] => Array ( )
        [field_count] => 1
        [host_info] => Localhost via UNIX socket
        [info] =>
        [insert_id] => 0
        [server_info] => 8.0.23-0ubuntu0.20.04.1
        [server_version] => 80023
        [stat] => Uptime: 4805192 Threads: 6 Questions: 44797749 Slow queries: 0 Opens: 8612 Flush tables: 3 Open tables: 3839 Queries per second avg: 9.322
        [sqlstate] => 00000
        [protocol_version] => 10
        [thread_id] => 641720
        [warning_count] => 0
    )
    [error:Database:private] =>
    [queries_to_log:Database:private] =>
    [query_log_file:Database:private] =>
)
$db变量的设置如下所示:

$db = new Database(CONF_DB_SERVER, CONF_DB_USER, CONF_DB_PASS, CONF_DB_NAME);
$db_config = array('server' => CONF_DB_SERVER, 'user' => CONF_DB_USER, 'pass' => CONF_DB_PASS, 'db' => CONF_DB_NAME);
下面是数据库类:

class Database
{
    private $con = NULL;
    private $error = "";
    private $queries_to_log = NULL;
    private $query_log_file = NULL;

    public function __construct($server = "", $user = "", $pass = "", $dbname = "", $new_connection = false)
    {
        if( $server == "" && $user == "" && $pass == "" && $dbname == "" ) 
        {
            global $db_config;
            $server = $db_config["server"];
            $user = $db_config["user"];
            $pass = $db_config["pass"];
            $dbname = $db_config["db"];
        }

        if( $new_connection ) 
        {
            $this->con = new mysqli($server, $user, $pass, $dbname);
        }
        else
        {
            $this->con = SingletonMysqli::getInstance($server, $user, $pass, $dbname);
        }

        if( !$this->con ) 
        {
            $this->error = "Could not Connect to database Server";
            exit();
        }

        $this->queries_to_log = false;
        $this->query_log_file = "";
    }

    public function query($qry)
    {
        $result = $this->con->query($qry);
        if( $result !== false ) 
        {
            $this->logQuery($qry);
            return $result;
        }

        $this->error = "Mysql Response: \n" . $this->con->error . "\nExecuted Query: \n" . $qry;
        $this->raiseError("Database Query Error", $this->error);
        return false;
    }

    public function prepareStatement($qry)
    {
        return $this->con->prepare($qry);
    }

    public function fetch($rs)
    {
        if( $row = $rs->fetch_assoc() ) 
        {
            return $row;
        }

        return false;
    }

    public function fetch_all($rs, $key_fld = "")
    {
        $arr = array(  );
        while( $row = $rs->fetch_assoc() ) 
        {
            if( $key_fld == "" ) 
            {
                $arr[] = $row;
            }
            else
            {
                $arr[$row[$key_fld]] = $row;
            }

        }
        return $arr;
    }

    public function fetch_all_assoc($rs)
    {
        $arr = array(  );
        while( $row = $rs->fetch_array() ) 
        {
            $arr[$row[0]] = $row[1];
        }
        return $arr;
    }

    public function startTransaction()
    {
        if( !$this->con->autocommit(false) ) 
        {
            $this->error = "Could not turn autocommit off." . $this->con->error;
            return false;
        }

        return $this->con->begin_transaction();
    }

    public function commitTransaction()
    {
        return $this->con->commit();
    }

    public function rollbackTransaction()
    {
        return $this->con->rollback();
    }

    public function insert_id()
    {
        return $this->con->insert_id;
    }

    public function insert_from_array($tbl, $arr, $execute_mysql_functions = false, $insert_options = array(  ), $flds_update_on_duplicate = array(  ))
    {
        $vals = "";
        foreach( $arr as $key => $val ) 
        {
            if( $vals != "" ) 
            {
                $vals .= ", ";
            }

            if( $execute_mysql_functions && substr($val, 0, 11) == "mysql_func_" ) 
            {
                $vals .= "`" . $key . "` = " . substr($val, 11);
            }
            else
            {
                $vals .= "`" . $key . "` = '" . $this->con->real_escape_string($val) . "'";
            }

        }
        $qry = "INSERT ";
        foreach( $insert_options as $opt ) 
        {
            $qry .= $opt . " ";
        }
        $qry .= "INTO " . $tbl . " SET " . $vals;
        if( 0 < count($flds_update_on_duplicate) ) 
        {
            $qry .= " ON DUPLICATE KEY UPDATE ";
            $update_vals = "";
            foreach( $flds_update_on_duplicate as $fld => $val ) 
            {
                if( $update_vals != "" ) 
                {
                    $update_vals .= ", ";
                }

                if( $execute_mysql_functions && substr($val, 0, 11) == "mysql_func_" ) 
                {
                    $update_vals .= "`" . $fld . "` = " . substr($val, 11);
                }
                else
                {
                    $update_vals .= "`" . $fld . "` = '" . $this->con->real_escape_string($val) . "'";
                }

            }
            $qry .= $update_vals;
        }

        if( $this->query($qry) === false ) 
        {
            return false;
        }

        return true;
    }

    public function update_from_array($tbl, $arr, $whr, $execute_mysql_functions = false, $update_options = array(  ), $order_by = "", $limit = 0)
    {
        $limit = intval($limit);
        $vals = "";
        foreach( $arr as $key => $val ) 
        {
            if( $vals != "" ) 
            {
                $vals .= ", ";
            }

            if( $execute_mysql_functions && substr($val, 0, 11) == "mysql_func_" ) 
            {
                $vals .= "`" . $key . "` = " . substr($val, 11);
            }
            else
            {
                $vals .= "`" . $key . "` = '" . $this->con->real_escape_string($val) . "'";
            }

        }
        if( is_array($whr) ) 
        {
            $whr = $this->convertStatementToString($whr, $execute_mysql_functions);
            if( $whr === false ) 
            {
                return false;
            }

        }

        $qry = "UPDATE ";
        foreach( $update_options as $val ) 
        {
            $qry .= $val . " ";
        }
        $qry .= $tbl;
        $qry .= " SET " . $vals . " WHERE " . $whr;
        if( $order_by != "" ) 
        {
            $qry .= " ORDER BY " . $order_by;
        }

        if( 0 < $limit ) 
        {
            $qry .= " LIMIT " . $limit;
        }

        if( $this->query($qry) ) 
        {
            return true;
        }

        return false;
    }

    public function deleteRecords($tbl, $whr, $delete_options = array(  ), $order_by = "", $limit = 0)
    {
        $limit = intval($limit);
        $whr = $this->convertStatementToString($whr);
        if( $whr === false ) 
        {
            return false;
        }

        $qry = "DELETE ";
        foreach( $delete_options as $val ) 
        {
            $qry .= $val . " ";
        }
        $qry .= "FROM " . $tbl . " WHERE " . $whr;
        if( $order_by != "" ) 
        {
            $qry .= " ORDER BY " . $order_by;
        }

        if( 0 < $limit ) 
        {
            $qry .= " LIMIT " . $limit;
        }

        return $this->query($qry);
    }

    public function convertStatementToString($arr)
    {
        if( !isset($arr["smt"]) || !is_array($arr["vals"]) ) 
        {
            $this->error = "Invalid array for where statement";
            return false;
        }

        $arr_smt = explode("?", $arr["smt"]);
        if( count($arr_smt) != count($arr["vals"]) + 1 ) 
        {
            $this->error = "Number of placeholders and number of elements for where statement do not match";
            return false;
        }

        if( !isset($arr["execute_mysql_functions"]) ) 
        {
            $arr["execute_mysql_functions"] = false;
        }

        if( !is_bool($arr["execute_mysql_functions"]) ) 
        {
            $arr["execute_mysql_functions"] = false;
        }

        $execute_mysql_functions = $arr["\$execute_mysql_functions"];
        $str = $arr_smt[0];
        for( $i = 0; $i < count($arr["vals"]); $i++ ) 
        {
            if( $execute_mysql_functions && substr($arr["vals"][$i], 0, 11) == "mysql_func_" ) 
            {
                $str .= substr($arr["vals"][$i], 11) . $arr_smt[$i + 1];
            }
            else
            {
                $str .= "'" . $this->con->real_escape_string($arr["vals"][$i]) . "'" . $arr_smt[$i + 1];
            }

        }
        return $str;
    }

    public function total_records($rs)
    {
        return $rs->num_rows;
    }

    public function getFieldsArray($rs)
    {
        $arr = array(  );
        while( $finfo = $rs->fetch_field() ) 
        {
            $arr[] = $finfo->name;
        }
        return $arr;
    }

    public function rows_affected()
    {
        return $this->con->affected_rows;
    }

    public function getError()
    {
        return $this->error;
    }

    public function logError($errno, $errstr, $errfile, $errline, $errcontext)
    {
        if( mbsErrorHandler($errno, $errstr, $errfile, $errline, $errcontext) ) 
        {
            $custom_message = "Error occured and logged. It can be viewed from error log.";
            $errorlogged = true;
        }
        else
        {
            $custom_message = "Error occured. Could not log error.";
        }

        if( in_array($errno, array( 1, 4, 256 )) ) 
        {
            exit();
        }

        return true;
    }

    public function raiseError($errormsg, $errordetails, $shouldHaltExec = true)
    {
        if( !CONF_DEVELOPMENT_MODE && CONF_LIB_HALDLE_ERROR_IN_PRODUCTION ) 
        {
            $errno = ($shouldHaltExec ? 256 : 2);
            $errstr = $errormsg;
            $errfile = $_SERVER["SCRIPT_NAME"];
            $errline = 0;
            $errcontext = array( "Global Data" => $GLOBALS, "Custom" => $errordetails );
            $this->logError($errno, $errstr, $errfile, $errline, $errcontext);
        }
        else
        {
            trigger_error($errormsg . " " . $errordetails, ($shouldHaltExec ? 256 : 512));
        }

    }

    private function logQuery($qry)
    {
        if( !$this->queries_to_log ) 
        {
            return NULL;
        }

        if( $this->query_log_file == "" ) 
        {
            exit( "Database class error! Query logging enabled but file path not set" );
        }

        if( !($fp = fopen($this->query_log_file, "a+")) ) 
        {
            exit( "Database class error! Could not open file to log queries" );
        }

        fwrite($fp, "\r\n" . date("Y-m-d H:i:s") . " " . $qry);
        fclose($fp);
    }

    public function logQueries($should_log, $file = "")
    {
        $this->queries_to_log = $should_log;
        $this->query_log_file = $file;
    }

    public function clearQueryLog()
    {
        if( !$this->queries_to_log ) 
        {
            return NULL;
        }

        if( $this->query_log_file == "" ) 
        {
            exit( "Database class error! Query logging enabled but file path not set" );
        }

        if( !($fp = fopen($this->query_log_file, "w+")) ) 
        {
            exit( "Database class error! Could not open file to log queries" );
        }

        fwrite($fp, "");
        fclose($fp);
    }

    public function quoteVariable($str)
    {
        return "'" . $this->con->real_escape_string($str) . "'";
    }

    public function getQueryLog()
    {
        if( !$this->queries_to_log ) 
        {
            return NULL;
        }

        if( $this->query_log_file == "" ) 
        {
            exit( "Database class error! Query logging enabled but file path not set" );
        }

        if( !($fp = fopen($this->query_log_file, "r")) ) 
        {
            exit( "Database class error! Could not open file to log queries" );
        }

        return file_get_contents($this->query_log_file);
    }

}
类数据库
{
private$con=NULL;
私人$error=“”;
private$querys\u to\u log=NULL;
私有$query\u log\u file=NULL;
公共函数u构造($server=“”、$user=“”、$pass=“”、$dbname=“”、$new\u connection=false)
{
如果($server==”“&&&$user==”“&&&$pass==”“&&&&$dbname==”“)
{
全局$db_配置;
$server=$db_config[“服务器”];
$user=$db_config[“user”];
$pass=$db_config[“pass”];
$dbname=$db_config[“db”];
}
如果($new_连接)
{
$this->con=newmysqli($server、$user、$pass、$dbname);
}
其他的
{
$this->con=SingletonMysqli::getInstance($server、$user、$pass、$dbname);
}
如果(!$this->con)
{
$this->error=“无法连接到数据库服务器”;
退出();
}
$this->querys\u to\u log=false;
$this->query_log_file=“”;
}
公共函数查询($qry)
{
$result=$this->con->query($qry);
如果($result!==false)
{
$this->logQuery($qry);
返回$result;
}
$this->error=“Mysql响应:\n”。$this->con->error。“\n执行的查询:\n”。$qry;
$this->raiseError(“数据库查询错误”,$this->Error);
返回false;
}
公共职能准备声明($qry)
{
返回$this->con->prepare($qry);
}
公共函数获取($rs)
{
如果($row=$rs->fetch_assoc())
{
返回$row;
}
返回false;
}
公共函数fetch_all($rs,$key_fld=”“)
{
$arr=array();
而($row=$rs->fetch_assoc())
{
如果($key\u fld==“”)
{
$arr[]=$row;
}
其他的
{
$arr[$row[$key\U fld]]=$row;
}
}
返回$arr;
}
公共函数获取所有关联($rs)
{
$arr=array();
而($row=$rs->fetch_array())
{
$arr[$row[0]]=$row[1];
}
返回$arr;
}
公共函数startTransaction()
{
如果(!$this->con->autocommit(false))
{
$this->error=“无法关闭自动提交。”。$this->con->error;
返回false;
}
返回$this->con->begin_transaction();
}
公共职能委员会事务()
{
返回$this->con->commit();
}
公共函数rollbackTransaction()
{
返回$this->con->rollback();
}
公共函数insert_id()
{
返回$this->con->insert\u id;
}
公共函数insert_from_array($tbl,$arr,$execute_mysql_functions=false,$insert_options=array(),$flds_update_on_duplicate=array())
{
$vals=“”;
foreach($arr作为$key=>$val)
{
如果($VAL!=“”)
{
$VAL.=“,”;
}
if($execute_mysql_functions&&substr($val,0,11)=“mysql_func”)
{
$vals.=“`.$key.`=“.substr($val,11);
}
其他的
{
$vals.=“`.$key.`='”$this->con->real\u escape\u string($val)。“;
}
}
$qry=“插入”;
foreach($opt插入选项)
{
$qry.=$opt.“;
}
$qry.=”转换为“$tbl.”设置“$VAL;
如果(0<计数($flds\U更新\U上的\U重复))
{
$qry.=“在重复密钥更新时”;
$update_vals=“”;
foreach($fld\u update\u on\u复制为$fld=>$val)
{
如果($update\u vals!=“”)
{
$update_vals.=“,”;
}
if($execute_mysql_functions&&substr($val,0,11)=“mysql_func”)
{
$update_vals.=“`.$fld.”`=“.substr($val,11);
}
其他的
{
$update\u vals.=“`.$fld.`='”$this->con->real\u escape\u string($val)。“;
}
}
$qry.=$update\U VAL;
}
if($this->query($qry)==false)
{
返回false;
}
返回true;
}
公共函数更新来自数组($tbl、$arr、$whr、$execute\U mysql\U functions=false、$update\U options=array(),$order\U by=“”,$limit=0)
{
$limit=intval($limit);
$vals=“”;
foreach($arr作为$key=>$val)
{
如果($VAL!=“”)
{
$VAL.=“,”;
}
if($execute_mysql_functions&&substr($val,0,11)=“mysql_func”)
{
$vals.=“`.$key.`=“.substr($val,11);
}
其他的
{
$vals.=“`.$key.`='”$this->con->real\u escape\u string($val)。“;
}
}
if(is_数组($whr))
{
$whr=$this->convertStatementToString($whr$execute\u mysql\u functions);
如果($whr)