Germaris Posted February 27, 2008 Share Posted February 27, 2008 Hi there! I wish to insert a blank row in a MySQL table and get as result the id of this row. Purpose of this operation is to make later an update of this row with collected data. How should I write the query? Many thanks in advance for your help! Best regards, Gerry Quote Link to comment Share on other sites More sharing options...
Orio Posted February 27, 2008 Share Posted February 27, 2008 The function you're looking for is mysql_insert_id(). <?php //connect to db mysql_query("INSERT INTO tbl_name (...rows...) VALUES (...values...)"); $inserted_id = mysql_insert_id(); ?> Orio. Quote Link to comment Share on other sites More sharing options...
Germaris Posted February 27, 2008 Author Share Posted February 27, 2008 The function you're looking for is mysql_insert_id(). Orio. Thank you very much! While you were posting I found by myself a less elegant solution but... also working! :-) I'm proud! :-) $table = "mytable"; $query = "INSERT INTO $table VALUES()"; $res = mysql_query($query) or die ("&error=".mysql_error()."<br/>".query); $querytwo="SELECT * FROM $table"; $querytwo_res=mysql_query($querytwo) or die("&error=".mysql_error()."<br/>".querytwo); $count=mysql_num_rows($querytwo_res); print "&count=".$count; But I prefer yours, of course! Best regards, Gerry Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted February 27, 2008 Share Posted February 27, 2008 Finding the number of rows in a database is not an accurate method of finding any information about a row id. Deleting one or more rows make the results incorrect. The method that Orio posted is the correct method of getting the id immediately after an INSERT query. Quote Link to comment Share on other sites More sharing options...
Germaris Posted February 27, 2008 Author Share Posted February 27, 2008 Finding the number of rows in a database is not an accurate method of finding any information about a row id. Deleting one or more rows make the results incorrect. The method that Orio posted is the correct method of getting the id immediately after an INSERT query. You are right. Even if my solution is working I recognize Orio's as more secure ! :-) Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.