0

I have a mysql table where the id column is set to AUTOINCREMENT and a PRIMARY KEY is associated with it. If my table has some records in it already then to get the last id I query like this

$sql = $db->prepare("SELECT `id` FROM `table` ORDER BY `id` DESC LIMIT 1");
$sql->execute();

Now considering my last record id is say about 224, then all the rows get deleted due to some function maybe. Then I want the last id for another querying purpose in which I want to add 1 with it.

$tobeinserted = $lastid + 1;

But since all the rows get deleted, I cannot query like the previous one. So how do I get it?

nick
  • 67
  • 2
  • 10
  • 2
    You do not need to do it. If not truncated the auto-increment will not be deleted. So the next insert will insert id = 225 – Marco Mura Dec 18 '14 at 14:07
  • No. I don't want to insert into that table. Before inserting into that table I need the id that is to be inserted for the purpose of doing some other task. If this task is accomplished then I will insert – nick Dec 18 '14 at 14:08
  • 1
    read the auto_increment value from table ddl – Marco Mura Dec 18 '14 at 14:09
  • Even I get the value how do I print it? like I print things in this way echo $row['id'] – nick Dec 18 '14 at 14:12
  • select * from `information_schema`.`tables` where table_name like 'afs_tipo_immagine' yes – Marco Mura Dec 18 '14 at 14:15

0 Answers0