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 Link to comment https://forums.phpfreaks.com/topic/93336-insert-a-blank-row/ 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. Link to comment https://forums.phpfreaks.com/topic/93336-insert-a-blank-row/#findComment-478059 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 Link to comment https://forums.phpfreaks.com/topic/93336-insert-a-blank-row/#findComment-478094 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. Link to comment https://forums.phpfreaks.com/topic/93336-insert-a-blank-row/#findComment-478103 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 ! :-) Link to comment https://forums.phpfreaks.com/topic/93336-insert-a-blank-row/#findComment-478111 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.