947740 Posted June 22, 2009 Share Posted June 22, 2009 How do I access a MySQL function in a query that I process through PHP? Specifically, the LAST_INSERT_ID() function. Example: $query = "<p>Last insert id: LAST_INSERT_ID(-1)"; Link to comment https://forums.phpfreaks.com/topic/163254-solved-mysql-in-php/ Share on other sites More sharing options...
flyhoney Posted June 22, 2009 Share Posted June 22, 2009 It's already built in: <?php $id = mysql_insert_id(); Link to comment https://forums.phpfreaks.com/topic/163254-solved-mysql-in-php/#findComment-861327 Share on other sites More sharing options...
947740 Posted June 22, 2009 Author Share Posted June 22, 2009 Thanks, but that does not work unless i have already done a query that referenced the particular table I am inserting into. I just want one query using the MySQL function. Link to comment https://forums.phpfreaks.com/topic/163254-solved-mysql-in-php/#findComment-861330 Share on other sites More sharing options...
flyhoney Posted June 22, 2009 Share Posted June 22, 2009 Check out this comment on the link I posted: http://us.php.net/manual/en/function.mysql-insert-id.php#83550 They show this example: <?php function get_current_insert_id($table) { $q = "SELECT LAST_INSERT_ID() FROM $table"; return mysql_num_rows(mysql_query($q)) + 1; } ?> Link to comment https://forums.phpfreaks.com/topic/163254-solved-mysql-in-php/#findComment-861341 Share on other sites More sharing options...
947740 Posted June 22, 2009 Author Share Posted June 22, 2009 It worked perfectly. Thanks for the help. Link to comment https://forums.phpfreaks.com/topic/163254-solved-mysql-in-php/#findComment-861343 Share on other sites More sharing options...
947740 Posted June 22, 2009 Author Share Posted June 22, 2009 For reference, I had to change it once I dropped data from the database. This only works if you have an auto_increment column, though. $q = "SELECT MAX(ID) FROM $table" $n = mysqli_fetch_row(mysqli_query($cxn,$q)); $n = $n[0] + 1; Link to comment https://forums.phpfreaks.com/topic/163254-solved-mysql-in-php/#findComment-861365 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.