Mysqli lastinsertid for PDO

Asked

Viewed 26 times

-1

I have an insert page, as soon as I click insert new accommodation, the system should insert some ready data, but on the same page I need to recover the last id, so that I can complete the other information of the form, as if I were editing the information.

In mysqli I managed to, more need to transform to PDO, and I’m having difficulties, follows below the code.

$imagem="produto.png";

$insert=mysqli_query($con,"insert into tbl_acomodacoes (acomodacao_imagem, acomodacao_situacao) values ('$imagem','0')");

$sql_last=mysqli_query($con,"select LAST_INSERT_ID(acomodacao_id) as last from tbl_acomodacoes order by acomodacao_id desc limit 0,1");

$rw=mysqli_fetch_array($sql_last);

$acomodacao_id=intval($rw['last']);

$sql=mysqli_query($con,"select * from tbl_acomodacoes where acomodacao_id='$acomodacao_id' limit 0,1");

$count=mysqli_num_rows($sql);

if ($count==0){

    //header("location: acomodacao.php");

    //exit;

}

$rw=mysqli_fetch_array($sql);

$titulo=$rw['acomodacao_titulo'];

$slug=$rw['acomodacao_slug'];

$link=$rw['acomodacao_link'];

$texto=$rw['acomodacao_texto'];

$quantidade=$rw['acomodacao_quantidade'];

$imagem=$rw['acomodacao_imagem'];

$situacao=intval($rw['acomodacao_situacao']);
  • Dear Samuel, why do you need this in PDO? What’s wrong with mysqli?

1 answer

1

Using PDO, the code would look like this:

$imagem = "produto.png";

$insert = $con->prepare("INSERT INTO tbl_acomodacoes(acomodacao_imagem, acomodacao_situacao) VALUES(?,?);");
$sonuc = $insert->execute([$imagem , '0']);
$last_id = $con->lastInsertId();
  • 2

    This is just part of what he did and it really doesn’t make sense to trade mysqli for PDO, there are people who believe that codes are portable between different banks, which is just a myth invented by people who knew little. There is no reason to trade something good for something more or less, yes, in practice PDO is a little worse than mysqli, may even have parameters named, but the process is on the PHP side, while mysqli is on the mysql server side and mysqli also supports mysqlnd, that for those who understand know that this helps with several related things.

  • Because it is William, I agree that if it is working with Mysqli it is not worth changing. I only posted the answer to answer Samuel’s question. And another, depending on how is his system, it is very difficult to maintain to change the connection driver in the entire application... Thank you!

  • 2

    There is a question that should not have an answer, even more if it answers only half and even worse if the question is a "Helpdesk" (like "do it for me"), I see that the site should teach the mentioned Apis and explain how to deepen, but everywhere I see answers like "try this", which end up serving more as Ctrl+C. This question at most should be commented and should be closed, because the exchange of both Apis has no purpose, it is only a request to "do it for me"

  • @Rodrigotognin thanks for your help !!!

Browser other questions tagged

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