mysql - mysqli_insert_id is not working on php OOP method -


i testing function of getting last auto_increment_id using mysqli_insert_id . feel quite confuse when find out if use 2 different methods, results 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 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 working can 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 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 doesn't work... result 0 . know did go wrong?

only initialize ->conn() method once, reuse it. every invocation creates new one:

$db = new db('localhost','bs','root',''); $connection = $db->conn(); // initialize once $sql = "insert 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 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(); 

Comments

Popular posts from this blog

amazon web services - S3 Pre-signed POST validate file type? -

c# - Check Keyboard Input Winforms -