Click to See Complete Forum and Search --> : a little thing


gruetztian
12-04-2003, 03:05 AM
-if i send a sqlquery like this

$query = "insert into usertable (uname,ufamilyname,ubd,ustreet,ucity) values ("hans","wurst","1979-04-19","dorfstrasse","stralsund");";

-with the php function

mysql_query($query,$conn);//the connection to the database is established

-to a database(table) like this

UID int(8) not null primary key auto_increment, //primary key
uname varchar(255) not null,
ufamilyname varchar(255) not null,
udb date not null,
street varchar(255) not null,
city varchar(255) not null,

-is it then possible for php, after sending the query, to get the UID that was generated by the databaseserver for this "record" without sending a new query?

-i think i saw anywhere that that is possible but i cant remind where ive saw it.Help!!!

Khalid Ali
12-04-2003, 08:25 PM
I don't think its possible,( I could be wrong as well)
However, php / MySQL is pretty fast,so I am sure a second query will matter much

chadypu
12-06-2003, 10:43 AM
well you could run a query grabbing everything from 'usertable' and then sort it by ID from greatest to least and add one

something like...


$sql = "SELECT * from usertable ORDER BY UID DESC";
$exe = mysql_query($sql,$conn);
$lastid = mysql_fetch_array($exe);
$lastUID = $lastid[UID] + 1;
echo $lastUID; //that would echo the uesrid it would be making assuming you run it before the other query


this is untested so im not sure how well it would work, and i suck at php :p

edit-oh you dont want to run a second query and you want to do it after... well then that wouldnt really work

you may need to do a second query like this after your first query

i hope that works for you...

$sql = "SELECT * from usertable ORDER BY UID DESC";
$exe = mysql_query($sql,$conn);
$lastid = mysql_fetch_array($exe);
echo $lastid[UID]; //that would echo the UID created if im not mistaken