note 71674 deleted from function.mysql-fetch-array by peehaa
| From: | peehaa@php.net | Date: | Mon, 09 Dec 2013 12:28:14 +0000 |
| Subject: | note 71674 deleted from function.mysql-fetch-array by peehaa | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-197883@lists.php.net to get a copy of this message | ||
Note Submitter: joshmish at haanmc dot com
----
A simple function that returns the next auto index. I recommend you use a unique id and not a
relative row index, but if the need arises, here's how to get that id before you INSERT without
the error-prone MAX(id) approach.
<?php
function nextautoindex ($table) {
$res=mysql_query("SHOW TABLE STATUS");
if (@mysql_num_rows($res)>0) { while ($row = @mysql_fetch_array($res, MYSQL_ASSOC)) {
if ($table == $row["name"]) return $row["Auto_increment"];
}}
return false;
}
/* Example usage:
$newID = nextautoindex("user"); // output: 12
// Now you can insert multiple related records without guessing the id
*/
?>