note 75746 added to function.mysql-insert-id
| From: | BenATalfreido dot com at osu1 dot php dot net | Date: | Thu, 14 Jun 2007 03:58:14 +0000 |
| Subject: | note 75746 added to function.mysql-insert-id | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-127467@lists.php.net to get a copy of this message | ||
I realise this isn't the best way of doing it, but if use auto_increment in your tables, which
just increases by one every time you add a row, you can get the last used id simply by quering the
database, selecting the id column and ordering it descendingly and pulling out the first row
returned. The added bonus is you can do it anytime, instead of just after an INSERT query.
<?php
function previous_id($table, $id_column) {
if ($table && $id_column) {
$result = mysql_query("SELECT " . $id_column " FROM " . $table . "
ORDER BY " . $id_column . " DESC
LIMIT 1");
$stuff = mysql_fetch_assoc($result);
return $stuff[$id_column];
} else {
return false;
}
}
?>
Just change $table to the table name, and $id_column to the column which auto increments.
----
Server IP: 203.202.10.60
Probable Submitter: 203.14.53.15
----
Manual Page -- http://www.php.net/manual/en/function.mysql-insert-id.php
Edit -- https://master.php.net/note/edit/75746
Del: integrated -- https://master.php.net/note/delete/75746/integrated
Del: useless -- https://master.php.net/note/delete/75746/useless
Del: bad code -- https://master.php.net/note/delete/75746/bad+code
Del: spam -- https://master.php.net/note/delete/75746/spam
Del: non-english -- https://master.php.net/note/delete/75746/non-english
Del: in docs -- https://master.php.net/note/delete/75746/in+docs
Del: other reasons-- https://master.php.net/note/delete/75746
Reject -- https://master.php.net/note/reject/75746
Search -- https://master.php.net/manage/user-notes.php