Python社区  »  DATABASE

无法访问php mysqli连接对象属性

guitarlass • 1 周前 • 12 次点击  

我有一个连接类,它成功地连接到所需的db表。我已经执行了一个插入查询,我想得到连接的最后一个插入id。但我得到了 Notice: Undefined property: connection::$insert_id 错误。我 var_dump($con) 在连接对象上,下面是我得到的。希望有人能帮我解决这个问题。非常感谢你的帮助。

对象(连接)2(5){[“数据库名称”]=>字符串(5)“数据库项目”[“数据库用户”]=>字符串(4)“根”[“数据库用户”]=>字符串(4)“根”[“数据库用户”]=>字符串(4)“根”[“数据库用户”]=>字符串(0)”“[“数据库主机”]=>字符串(9)“localhost”[“连接”]=>对象(mysqlli)3(19){[“受影响的排数”]=>int(1)[“客户信息”]==>字符串(79)“mysqlnd 5.0.0”数据库用户”]=>字符串(4)“根”[“数据库用户”[“数据库用户”[“数据库用户”]]=>.12-开发-20150407-$ID:24AE00989D199995FFCBFBFBFBBbf635999994343535FAF9972$“[”客户-汽车版“]=>int(50012)[”connect'u errno“]=>int(0)[”connect'u error“]=>null[”errno“]=>int(0)[”error“]=>lt;int(0)[”error'u list“]=>数组(0){}[”fieldu count“]==>int(0)[”主机-主机-主机-主机-主机-主机-主机-主机-主机-主机-主机-主机-主机-主机-主机-主机-主机-主机-主机-主机-主机-主机-主机-gt;字符串(20)“localhost via tcp/ip“[“info”]=>null[“insert_id”]=>int(26)[“server_info”]=>string(21)“5.5.5-10.1.19-mariadb”[“server_version”]=>int(50505)[“stat”]=>string(136)“正常运行时间:302248线程:1问题:9631慢查询:0打开:65刷新表:1打开表:40查询/秒平均值:0.031“[“sqlstate”]=>字符串(5)“00000”[“协议版本”]=>int(10)[“线程id”]=>int(522)[“警告计数”]=>int(0)}}

class connection {

    public $db_name;
    public $db_user;
    public $db_pass;
    public $db_host;

    public $connection;

     public function connection(){
        $this->db_host = HOST;
        $this->db_user = DB_USERNAME;
        $this->db_pass = DB_PASSWORD;
        $this->db_name = DB_NAME;

        $this->connection = new mysqli($this->db_host, $this->db_user, $this->db_pass, $this->db_name);
    }

    function connect($host, $user, $pass, $db_name) {

        $this->connection = new mysqli($host, $user, $pass, $db_name);

        if ( mysqli_connect_errno() ) {
            printf("Connection failed: %s", mysqli_connect_error());
            exit();
        }

        return $this->connection;

    }

    public function query($sql)
        {
        return $this->connection->query($sql);
        }

   }

//--------

require_once("connection.php");

class DB {

    public $con;

    function __construct()
    {
        $this->con = new connection();
    }

    function insertQuery($table, $data_array){ 

        $fields = " (";
        $values = " VALUES (";
        $x = 0;
        $y = 0;

        foreach ($data_array as $key => $value) {

            $fields.= $key;
            $values.= "'".addslashes($value)."'";

            if(sizeof($data_array)-1 != $x){
                $fields.= ",";
                $values.=",";
            }
            else{
                $fields.= ")";
                $values.=")";
            }
            $x++;
        }

        $sql = "insert into ".$table.$fields.$values;

        if (!$this->con->query($sql) === TRUE) {
            return "Error: " . $sql . "<br>" . $this->con->error;
        }
        else {
            $last_id = $this->con->insert_id;
            return $last_id;
        }
    }

}
Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/40273
 
12 次点击  
分享到微博
文章 [ 1 ]  |  最新文章 1 周前
Arjun Singh
Reply   •   1 楼
Arjun Singh    9 月前

DB 类构造函数首先应该设置凭据属性( db_host, db_user, db_pass, db_name 和呼叫 connection 方法 连接

class DB {

    public $con;

    function __construct()
    {
        $this->con = new connection();
        $this->con->db_host = HOST; // replace HOST
        $this->con->db_user = DB_USERNAME; // replace DB_USERNAME
        $this->con->db_pass = DB_PASSWORD; // replace DB_PASSWORD
        $this->con->db_name = DB_NAME; // replace DB_NAME
        $this->con->connection();
    }

    function insertQuery($table, $data_array){ 

        $fields = " (";
        $values = " VALUES (";
        $x = 0;
        $y = 0;

        foreach ($data_array as $key => $value) {

            $fields.= $key;
            $values.= "'".addslashes($value)."'";

            if(sizeof($data_array)-1 != $x){
                $fields.= ",";
                $values.=",";
            }
            else{
                $fields.= ")";
                $values.=")";
            }
            $x++;
        }

        $sql = "insert into ".$table.$fields.$values;

        if (!$this->con->query($sql) === TRUE) {
            return "Error: " . $sql . "<br>" . $this->con->error;
        }
        else {
            $last_id = $this->con->insert_id;
            return $last_id;
        }
    }
 }