mysqli_insert_id 不适用于 php OOP 方法 [英] mysqli_insert_id is not working on php OOP method
问题描述
我正在测试使用 mysqli_insert_id 获取最后一个 auto_increment_ID 的功能.当我发现如果我使用两种不同的方法,结果是不同的时,我感到很困惑.
I am testing the function of getting last auto_increment_ID by using mysqli_insert_id . I feel quite confuse when I find out the if i use 2 different methods, the results are different.
方法一
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = "INSERT into item(uid,item_id,item_name,item_price,item_quantity)
VALUES('1','0','hhh','23','23');";
if (mysqli_query($conn, $sql)) {
$last_id = mysqli_insert_id($conn);
echo "New record created successfully. Last inserted ID is: " . $last_id;
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
mysqli_close($conn);
?>
这种程序方式有效,因为我可以获得最后一个 ID.
This Procedural way is working as i can get the last ID.
方法二
db.php
class db{
protected $db_host;
protected $db_name;
protected $db_user_name;
protected $db_pass;
public function __construct($host,$db_n,$user_n,$pass) {
$this->db_host=$host;
$this->db_name=$db_n;
$this->db_user_name=$user_n;
$this->db_pass=$pass;
}
public function conn(){
return mysqli_connect($this->db_host, $this->db_user_name, $this->db_pass, $this->db_name);
}
}
test.php
require "db.php";
$db=new db('localhost','bs','root','');
$sql = "INSERT into item(uid,item_id,item_name,item_price,item_quantity)
VALUES('1','0','hhh','23','23');";
if (mysqli_query($db->conn(), $sql)) {
$last_id = mysqli_insert_id($db->conn());
echo "New record created successfully. Last inserted ID is: " . $last_id;
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($db->conn());
}
mysqli_close($db->conn());
这个方法根本行不通……它会得到结果 0 .有谁知道我哪里出错了?
this method simply doesn't work... It will get the result 0 . Anyone know where did i go wrong?
推荐答案
只初始化一次 ->conn()
方法,然后重复使用它.每次调用都会创建一个新调用:
Only initialize ->conn()
method once, then just reuse it. Every invocation creates a new one:
$db = new db('localhost','bs','root','');
$connection = $db->conn(); // initialize once
$sql = "INSERT into item(uid,item_id,item_name,item_price,item_quantity)
VALUES('1','0','hhh','23','23');";
if (mysqli_query($connection, $sql)) {
$last_id = mysqli_insert_id($connection);
echo "New record created successfully. Last inserted ID is: " . $last_id;
} else {
echo "Error: " . $sql . "<br>" . $connection->error;
}
$connection->close();
或者使用面向对象的接口(->insert_id
属性):
Or using object oriented interface (->insert_id
property):
$db = new db('localhost','bs','root','');
$connection = $db->conn(); // initialize once
$sql = "INSERT into item(uid,item_id,item_name,item_price,item_quantity)
VALUES('1','0','hhh','23','23');";
if($connection->query($sql)) {
$last_id = $connection->insert_id;
echo "New record created successfully. Last inserted ID is: " . $last_id;
} else {
echo "Error: " . $sql . "<br>" . $connection->error;
}
$connection->close();
这篇关于mysqli_insert_id 不适用于 php OOP 方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!