dennismonsewicz Posted December 12, 2008 Share Posted December 12, 2008 Is there a way to create a SQL statement that will insert into a database and the next SQL entry will grab the rowid of the first statement entered? Quote Link to comment https://forums.phpfreaks.com/topic/136703-solved-dynamic-sql-statement/ Share on other sites More sharing options...
gevans Posted December 12, 2008 Share Posted December 12, 2008 $query = "INSERT INTO `table` (`field`) VALUE ('something')"; $result = mysql_query($query) or die(mysql_error()); $insertid = mysql_insert_id(); Quote Link to comment https://forums.phpfreaks.com/topic/136703-solved-dynamic-sql-statement/#findComment-713847 Share on other sites More sharing options...
timmah1 Posted December 12, 2008 Share Posted December 12, 2008 mysql_insert_id() Quote Link to comment https://forums.phpfreaks.com/topic/136703-solved-dynamic-sql-statement/#findComment-713848 Share on other sites More sharing options...
timmah1 Posted December 12, 2008 Share Posted December 12, 2008 oops, sorry gevans Quote Link to comment https://forums.phpfreaks.com/topic/136703-solved-dynamic-sql-statement/#findComment-713849 Share on other sites More sharing options...
dennismonsewicz Posted December 12, 2008 Author Share Posted December 12, 2008 hmm thanks for the help... i am writing a SQL file can you use this in the file? like: INSERT INTO `table` (`field1`) VALUE ('something'); INSERT INTO `table` (`field1`) VALUE ('something') WHERE mysql_insert_id(); Quote Link to comment https://forums.phpfreaks.com/topic/136703-solved-dynamic-sql-statement/#findComment-713864 Share on other sites More sharing options...
gevans Posted December 12, 2008 Share Posted December 12, 2008 You don't need a WHERE clause in an INSERT statement Quote Link to comment https://forums.phpfreaks.com/topic/136703-solved-dynamic-sql-statement/#findComment-713883 Share on other sites More sharing options...
dennismonsewicz Posted December 12, 2008 Author Share Posted December 12, 2008 so my SQL could look like this INSERT INTO `table` (`field1`) VALUE ('something', 'somethingelse'); INSERT INTO `table` (`field1`) VALUE ('something', mysql_insert_id); Quote Link to comment https://forums.phpfreaks.com/topic/136703-solved-dynamic-sql-statement/#findComment-713890 Share on other sites More sharing options...
gevans Posted December 12, 2008 Share Posted December 12, 2008 mysql_insert_id is php, so its easier to write this in php $query = "INSERT INTO `table` (`field1`) VALUE ('something', 'somethingelse')"; $result = mysql_query($query) or die(mysql_error()); $returnid = mysql_insert_id(); $query = "INSERT INTO `table` (`field1`, `field2`) VALUE ('something', '$returnid')"; $result = mysql_query($query) or die(mysql_error()); Quote Link to comment https://forums.phpfreaks.com/topic/136703-solved-dynamic-sql-statement/#findComment-713895 Share on other sites More sharing options...
dennismonsewicz Posted December 12, 2008 Author Share Posted December 12, 2008 ah gotcha... i am writing a Joomla (BOOOO) component and to install the file you have to write a sql file to install of the content and menu structure and its a pain in the butt. And the Joomla Forums suck! You can never get any help! Thanks for your help here man! Quote Link to comment https://forums.phpfreaks.com/topic/136703-solved-dynamic-sql-statement/#findComment-713899 Share on other sites More sharing options...
gevans Posted December 12, 2008 Share Posted December 12, 2008 no worries, hit resolved Quote Link to comment https://forums.phpfreaks.com/topic/136703-solved-dynamic-sql-statement/#findComment-713902 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.