php mysqli last record

0

I have a page that connects through the mysqli class as follows:

# include da classe

class connectionClass extends mysqli{
    public $host="localhost",$dbname="banco",$dbpass="",$dbuser="susus";
    public $con;

    public function __construct() {
        if($this->connect($this->host, $this->dbuser, $this->dbpass, $this->dbname)){}
        else
    {
        return "<h1>Erro de conexão/h1>";
    }
}
}

page where class calls

require_once 'connectionClass.php';

class webcamClass extends connectionClass{

    $query  =   "INSERT INTO vis_pres_path_foto (fk_id_user_sistema,vis_pres_path_foto_image) ";
    $query  .=  "VALUES('$id_user','$image') ";
    $result =   $this->query($query);
}

How do I do after insert above return last id of table after insert in PDO I use $ultimo = $conn->lastInsertId(); but for that case it does not work.

    
asked by anonymous 23.11.2017 / 20:58

2 answers

1

The class mysqli has int $insert_id , which returns the last id generated by mysql, see how to use it in your situation:

    $query  =   "INSERT INTO vis_pres_path_foto (fk_id_user_sistema,vis_pres_path_foto_image) ";
    $query  .=  "VALUES('$id_user','$image') ";
    $result =   $this->query($query);
    $ultimoId = $this->insert_id;
    
24.11.2017 / 01:20
1

After correcting the syntax of your code you can create a function like this

<?php
/*
 * Description of LastId
 *
 * @author Adriano Back
 */
class LastId {

private $id;

    public function __construct(){
        $conn = mysqli_connect(DBURI,DBUSER,DBPASS,DBNAME);
        if (mysqli_connect_error())
        {
            exit("Falha de Conexão: <br />" . mysqli_connect_error());
        }

        $query = "SELECT id FROM suatabela ORDER BY id DESC LIMIT 1";
        $result = mysqli_query($conn,$query) or 
                die(mysqli_error($conn) . " Falha de Consulta " . $query);


        $id = mysqli_fetch_assoc($result);

        foreach($id as $last_id){
            $this->id = $last_id['id'];
        }    
        mysqli_close($conn);
    }

    public function getLastId(){
        return $this->id;
    }
}

After you do so,

$id = new LastId();
$last_id = $id->getLastId();
    
23.11.2017 / 21:53