我有一个连接类,它成功连接到所需的数据库表。我已经执行了插入查询,我想获取连接的最后插入的ID。但是我收到一个Notice: Undefined property: connection::$insert_id
错误。我在连接对象上var_dump($con)
,下面是我得到的。希望有人可以帮助我解决这个问题。非常感谢您的帮助。
对象(连接)#2(5){[“” db_name“] =>字符串(5)” dbproject“ [” db_user“] =>字符串(4)” root“ [” db_pass“] =>字符串( 0)“” [“ db_host”] =>字符串(9)“ localhost” [“ connection”] => object(mysqli)#3(19){[“ affected_rows”] => int(1)[“ client_info” ] =>字符串(79)“ mysqlnd 5.0.12-dev-20150407-$ Id:241ae00989d1995ffcbbf63d579943635faf9972 $” [“ client_version”] => int(50012)[“ connect_errno”] => int(0)[“ connect_error”] => NULL [“ errno”] => int(0)[“ error”] =>字符串(0)“” [“ error_list”] => array(0){} [“ field_count”] => int(0 )[“ host_info”] =>字符串(20)“通过TCP / IP的本地主机” [“ info”] => NULL [“ insert_id”] => int(26)[“ server_info”] =>字符串(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“] => string(5)” 00000“ [” protocol_version“] => int(10)[” thread_id“] => int(522) [“ warning_count”] => 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;
}
}
}
答案 0 :(得分:-1)
首先在DB
类构造函数中,您应该设置凭据属性(db_host, db_user, db_pass, db_name
)并调用connection
类的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;
}
}
}