troubleshooting Question

I am getting 0 as result for mysqli_insert_id()

Avatar of impressionexpress
impressionexpress asked on
DatabasesPHP* mysql connectionsSQLMySQL Server
6 Comments1 Solution101 ViewsLast Modified:
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();
	}
}
ASKER CERTIFIED SOLUTION
Join our community to see this answer!
Unlock 1 Answer and 6 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 6 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros