Retreiving The Next AutoIndex

I know this is probablly really, really simple, but I can't figure out
how to retreive the next autoindex in a table, so when I want to
obtain it, I've been inserting a new row, then finding the unique
index from some combination of other values that I know will be
unique.

So I would appreciate you're help in this regard:
1. What's the appropriate SQL query to get the next autoindex from a
table (say 'user').
2. How do I obtain that value in PHP (as in, what would I use as the
index in the array returned by mysql_fetch_assoc())

Thanks!
dimo414 [ Mo, 19 März 2007 07:22 ] [ ID #1662102 ]

Re: Retreiving The Next AutoIndex

Post removed (X-No-Archive: yes)
Notifier Deamon [ Mi, 21 März 2007 19:46 ] [ ID #1664375 ]

Re: Retreiving The Next AutoIndex

Michael wrote:
> I know this is probablly really, really simple, but I can't figure out
> how to retreive the next autoindex in a table, so when I want to
> obtain it, I've been inserting a new row, then finding the unique
> index from some combination of other values that I know will be
> unique.
>
> So I would appreciate you're help in this regard:
> 1. What's the appropriate SQL query to get the next autoindex from a
> table (say 'user').
> 2. How do I obtain that value in PHP (as in, what would I use as the
> index in the array returned by mysql_fetch_assoc())
>
> Thanks!
>

Here's a code snippet (from here: http://php.net/mysql_insert_id):

$SQL = "SHOW
TABLE STATUS
FROM
databaseName
LIKE
'tableName'";

$result = mysql_query($SQL);
$row = mysql_fetch_assoc($result);
$nextInsertId = $row['Auto_increment'];



--
Christoph Burschka
Christoph Burschka [ Mo, 26 März 2007 20:25 ] [ ID #1669191 ]

Re: Retreiving The Next AutoIndex

Tom wrote:
> On 18 Mar 2007 23:22:29 -0700, Michael wrote...
>> I know this is probablly really, really simple, but I can't figure out
>> how to retreive the next autoindex in a table, so when I want to
>> obtain it, I've been inserting a new row, then finding the unique
>> index from some combination of other values that I know will be
>> unique.
>>
>> So I would appreciate you're help in this regard:
>> 1. What's the appropriate SQL query to get the next autoindex from a
>> table (say 'user').
>> 2. How do I obtain that value in PHP (as in, what would I use as the
>> index in the array returned by mysql_fetch_assoc())
>>
>> Thanks!
>>
>
> I think the "mysql_insert_id()" function does that...
>
> http://us2.php.net/manual/en/function.mysql-insert-id.php
>
> Tom

It retrieves the *last* id that was inserted - apparently he wants to look ahead
to find the one that the next entry will be inserted with.
Christoph Burschka [ Mo, 26 März 2007 20:27 ] [ ID #1669192 ]

Re: Retreiving The Next AutoIndex

Thanks, mysql_insert_id() is exactly what i'm looking for - better
than what I originally described. But does mysql_insert_id() risk
race conditions?
dimo414 [ Do, 19 April 2007 02:30 ] [ ID #1692450 ]
PHP » alt.php.sql » Retreiving The Next AutoIndex

Vorheriges Thema: Help with Statement
Nächstes Thema: Printing Avery Labels from a web site