We help IT Professionals succeed at work.
Get Started

I am getting 0 as result for mysqli_insert_id()

98 Views
Last Modified: 2019-12-26
I have a PHP class that I created for my database connection and queries. Im having an issue with the mysqli_insert_id, I know the reason for this is because after my query function, I close the connection therefore when I call the last_insert_id function it returns 0, can someone guide me on how i can fix this issue.

    public function get_connection(){
        try{
            $this->connection = @mysqli_connect( $this->databaselink, $this->username, $this->password );
            if( $this->connection === false ){           
                throw new Exception( mysql_error() );
            }           
            if( !mysqli_select_db($this->connection, $this->database) ){
                echo "no db selected error";
                throw new Exception( mysql_error() );
            }
        }
        catch(Exception $e){
            echo $e->getMessage();
            //$this->log->logger( "MYSQL", $e, $query );
        }
    }
 
    public function close_connection(){
        mysqli_close( $this->connection );
        $this->connection = NULL;
    }
   
    public function query( $query ){
        $this->get_connection();
       
        try {
            if( empty($query) ){
                
				throw new Exception($query . " query empty");
            }
           
            $this->result = @mysqli_query($this->connection, $query);
            if( !$this->result ){
                throw new Exception( mysqli_error($this->connection) );
            } else {
                return $this->result;
            }
        } catch(Exception $e){
            echo $e->getMessage();
            //$this->log->logger( "MYSQL", $e, $query );
        }
		echo mysqli_info($this->connection); 
     //   $this->close_connection();
    }
	
	public function last_insert_id(){
	//	$this->get_connection();
		return mysqli_insert_id($this->connection);
	//	$this->close_connection();
	}
}

Open in new window

Comment
Watch Question
CERTIFIED EXPERT
Most Valuable Expert 2020
Most Valuable Expert 2018
Commented:
This problem has been solved!
Unlock 1 Answer and 6 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE