问题
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.
Method 1
<?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);
?>
This Procedural way is working as i can get the last ID.
Method 2
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());
this method simply doesn't work... It will get the result 0 . Anyone know where did i go wrong?
回答1:
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();
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();
回答2:
just because you called $db->conn() twice, you need to assign $db->conn() to a variable.as follows:
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');";
$conn = $db->conn();
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);
来源:https://stackoverflow.com/questions/39197883/mysqli-insert-id-is-not-working-on-php-oop-method