There's two methods I use, depending on the situations I need.

What it sounds like you want is this:

$sql = "SELECT id FROM test ORDER BY id DESC LIMIT 1";

Often when I need to know the next avaliable ID prior to making an insert,
I'll do this--

$sql ="SHOW TABLE STATUS LIKE 'test'";
$array = $this->sql->getRow($sql);
echo $array['Auto_increment'];

Either way will get you a number you can work with, the first gets you the
last inserted number and the second way gets you the number of what the next
entry will be.

--Nick Pappas

-----Original Message-----
From: Patrik Fomin [mailto:[EMAIL PROTECTED] 
Sent: Tuesday, July 01, 2003 9:04 PM
Subject: [PHP-DB] Hi, simple question


im adding a post to the database, is there any way to retrive back the ID
for the post that i just created?

the id is auto_increment and primary key.

$sql = "insert into test (namn, www) values('test', '')";

$row = mysql_get_post("id");

the only way i could think of was to create a tempfield in the database and
each time you post you give the temp field a unique value, like:

$sql = "insert into test (namn, www, unique) values('test',
'','25323324')"; mysql_query($sql);

$sql = "select id from test where unique = '25323324'";
$res = mysql_query($sql);


PHP Database Mailing List (
To unsubscribe, visit:

PHP Database Mailing List (
To unsubscribe, visit:

Reply via email to