Problem with mysqli_insert_id - Class

Asked

Viewed 109 times

0

I have a problem getting the last id inserted in a table, for the same is returning empty. I searched the stack but couldn’t find a solution.

Script for inclusion of information in the BD:

if ( isset( $_GET['create'] ) )
{
    if ( isset( $_POST['dep_nome'] ) )
    {
        $noticia_title = trim( $_POST['dep_nome'] );
        $noticia_content = trim( $_POST['dep_content'] );
        $db->query( "insert into departamentos (dep_nome, dep_content) values ('$dep_nome','$dep_content');" );
        $dep_id = $db->insert_id;
        @header( "Location: departamentos.php?edit=$dep_id" );  
    } 
}

Class with insert_id (COMMENT: CLASS INSERT ID):

public $query;
public $data;
public $result;
public $rows;
protected $config;
protected $host;
protected $port;
protected $user;
protected $pass;
protected $dbname;
protected $con;

public function __construct()
{
    try
    {
        #array com dados do banco
        include 'database.conf.php';
        global $databases;
        $this->config = $databases['local'];
        # Recupera os dados de conexao do config
        $this->dbname = $this->config['dbname'];
        $this->host = $this->config['host'];
        $this->port = $this->config['port'];
        $this->user = $this->config['user'];
        $this->pass = $this->config['password'];
        # instancia e retorna objeto
        $this->con = $this->con = @mysqli_connect( "$this->host", "$this->user", "$this->pass","$this->dbname");
        //@mysql_select_db( "$this->dbname" );
        if ( !$this->con )
        {
            throw new Exception( "Falha na conexão MySql com o banco [$this->dbname] em database.conf.php" );
        }
        else
        {
            return $this->con;
        }
    }
    catch ( Exception $e )
    {
        echo $e->getMessage();
        exit;
    }
    return $this;
}

public function query($query = '' )
{
    try
    {
        if ( $query == '' )
        {
            throw new Exception( 'mysql query: A query deve ser informada como parâmetro do método.' );
        }
        else
        {
            $this->query = $query;
            $this->result = mysqli_query($this->con, $this->query );
    $this->insert_id = mysqli_insert_id($this->con); // CLASS INSERT ID
        }
    }
    catch ( Exception $e )
    {
        echo $e->getMessage();
        exit;
    }
    return $this;
}

public function fetchAll()
{
    $this->data = "";
    $this->rows = 0;
    while ( $row = @mysqli_fetch_array( $this->result, MYSQLI_ASSOC ) )
    {
        $this->data[] = $row;
    }
    if ( isset( $this->data[0] ) )
    {
        $this->rows = count( $this->data );
    }
    return $this->data;
}

Can someone help me? The result in GET has only been "departments.php? Edit=" and has not returned the ID entered through $db->insert_id.

I’ve looked at the documentation and I can’t solve it.

  • 1

    put the whole class!

  • 2

    I put mate!

  • Place insert_id up there like public $insert_id and run the application, seeing only within the scope of the working class, then make this change.!

  • After the query() you need to catch the result, $res = $db->query(...); echo $res->insert_id;

  • Both did not work :( Still working empty.

  • 1

    Next, the problem is local, there may be error in the insertion, but, your classes have problems...

  • I solved my problem, I switched all "protected" to "public" class and solved the problem, was preventing access to BD "protected" in class.

Show 2 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.