On Thu, 2009-02-26 at 11:49 -0500, PJ wrote: > Ashley Sheridan wrote: > > On Thu, 2009-02-26 at 11:14 -0500, PJ wrote: > > > >> Something is rotten here. Twist it any way you like and it just does not > >> work. > >> I am trying to figure out the working of SELECT LAST_INSERT_ID() and > >> cannot get beyond the first $sql - But it works fine from command-line! > >> > >> <? > >> include ("lib/db1.php"); // Connect to database > >> > >> $sql1 = "INSERT INTO test (name, age) VALUES ('Joe Blow', '69')"; > >> $result1 = mysql_query($sql1,$db); > >> if ( !$result) { > >> echo("<P>Error performing query: " . > >> mysql_error() . "</P>"); > >> exit(); > >> } > >> > >> THIS IS WHERE IT STOPS WITH ERROR.$sql1 just refuses to work. db > >> connection is fine and the INSERT works from xterm on remote puter. > >> Can't figure this out. > >> ============ > >> > >> > >> $sql = "SELECT LAST_INSERT_ID() FROM test"; > >> $result = mysql_query($sql,$db); > >> if (!$result) { > >> echo("<P>Error performing query: " . > >> mysql_error() . "</P>"); > >> exit(); > >> } > >> while ( $row = mysql_fetch_array($result) ) { > >> echo("<P>" . $row["id"] . "</P>"); > >> } > >> ?> > >> > >> What am I missing here? Or how can I trace the error? > >> > >> -- > >> > >> Phil Jourdan --- pj@xxxxxxxxxxxxx > >> http://www.ptahhotep.com > >> http://www.chiccantine.com > >> > >> > >> > > PHP has a built-in function for retrieving the last inserted auto id, > > called mysql_insert_id (http://uk2.php.net/mysql_insert_id) which is a > > lot easier than using another query to do the same. So in your above > > line, you would replace > > > > $sql = "SELECT LAST_INSERT_ID() FROM test"; > > > > with: > > > > $sql = mysql_insert_id($result1); > > > > and then remove the $result line, as you don't need to execute a query > > for it now. > > > Thanks for that... but how do I see what is returned - and what do I do > with it - is it a string a value or what? And how do I recuperate it for > further use? > I have been trying with the little proggie above to see just what is > returned and how to deal with it for further inserts and selects etc. > But I AM NOT GETTING BEYOND THE FIRST QUERY and that is frustrating... > > the value returned from mysql_insert_id() is an integer, so you can use it unquoted in your queries like any other number value. Ash www.ashleysheridan.co.uk -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php