简体   繁体   中英

select last id, without insert in PDO

Tried this but i still cant make it work.

Edited:

I want to get the id's for each of my Fruits without insert in PDO.

//apple
//orange

foreach($_POST['fruits'] as $fruits) {

     $a = recordFruit($fruits); 

      if(!empty($a)) {
         $id = id_of_this_fruit(); //get ID of apple, then orange. so on..
         echo $id[0]."<br />";
      }
}


function id_of_this_fruit(){
    ...
    $query = "SELECT * FROM fruits ORDER BY id DESC LIMIT 1;
    ...
    $row = $sql->fetchALL();
    return $row;
}

Edited: after some changes in the query. my code returns value. but its not returning the recently added data. it shows the old ID prior to the insertion of new fruits.

You can get last id by querying with order by and limit if ID is auto increment.

SELECT * 
FROM fruits 
ORDER BY ID DESC
LIMIT 1

This will only work if you are using a table. If you are using the alias with multiple tables in the WHERE clause, will fail SQL Error (1221): Incorrect usage of UPDATE and ORDER BY

SELECT * FROM fruits  ORDER BY id DESC LIMIT 1;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM