PDA

View Full Version : LAST_INSERT_ID call


TECK
02-20-2002, 12:54 AM
i want to call in settinggroup the last settinggroupid (for example, 32 that i just created in settinggroup with a NULL). here what i came up with:$settinggroupid=$DB_site->query("SELECT LAST_INSERT_ID() FROM settinggroup");
$DB_site->query("INSERT INTO setting VALUES (NULL,$settinggroupid,'News forum ID','newsforumid','15','The specified forum will contain threads viewed on the main page only.','',0)");it doesnt want to grab it... hmmmm.. i'm upset :noid:

the script returns this:
---------------------------------------
INSERT INTO setting VALUES (NULL,Resource id #6,'News forum ID','newsforumid','15','The specified forum will contain threads viewed on the main page only.','',0)

instead of:
---------------------------------------
INSERT INTO setting VALUES (NULL,32,'News forum ID','newsforumid','15','The specified forum will contain threads viewed on the main page only.','',0)

Scott MacVicar
02-20-2002, 06:32 AM
$settinggroupid=$DB_site->query_first("SELECT LAST_INSERT_ID() FROM settinggroup");

Put the values from the query into an array then it will work

Admin
02-20-2002, 12:18 PM
Just use $DB_site->last_insert().

TECK
02-20-2002, 11:51 PM
is not working PPN.. it returns Array instead of the value.

FireFly, can you post the exact line please? i need to make a query on this. here it is what i try to do:$DB_site->query("INSERT INTO settinggroup (settinggroupid,title,displayorder) VALUES (NULL,'".addslashes($title)."','$displayorder')");
echo "Adding settinggroup data... Done. <font color='#006699'>settinggroup</font> table populated.<br>\n";

$settinggroupid=$DB_site->query_first("SELECT LAST_INSERT_ID() FROM settinggroup");
$DB_site->query("INSERT INTO setting VALUES (NULL,$settinggroupid,'News forum ID','newsforumid','15','The specified forum will contain threads viewed on the main page only.','',0)");
echo "Adding setting data... Done. <font color='#006699'>setting</font> table populated.<p></td>\n";i need to call the last ID from settinggroup and insert it as a value into setting. let me know where i do it wrong.

Scott MacVicar
02-21-2002, 06:25 AM
Yes it returns an array, thats how you get data from the database, or you fetch row or fetch object.

use $settinggroupid[LAST_INSERT_ID()] as you said its an array. If your ever curious to what value an array holds you can use print_r($array);
and it will print out all the values of $array.

Anyway you can use $DB_site->insert_id(); to use the abstraction layer if you want.

its part of the abstraction layer which simply uses mysql_insert_id(); which is a nice trait of mysql as it gets the last value of the auto incrememnt column.

TECK
02-21-2002, 06:40 AM
Originally posted by PPN
Anyway you can use $DB_site->insert_id(); to use the abstraction layer if you want. i'm confused about one thing, how do i call it from settinggroup? if i use:$settinggroupid=$DB_site->insert_id();it will not grab the last id from settinggroup, right? help me understand better this matter.

btw scott, ty for your tips. i learn alot from mysql.com site, but you guys have all kind of tricks.. ;)

Admin
02-21-2002, 10:00 AM
The bit of code you posted is correct nakkid.
mysql_insert_id() returns the last auto-increment ID that was insert into the DATABASE during the current connection.

Scott MacVicar
02-21-2002, 03:04 PM
use this

$DB_site->query("INSERT INTO settinggroup (settinggroupid,title,displayorder) VALUES (NULL,'".addslashes($title)."','$displayorder')");
echo "Adding settinggroup data... Done. <font color='#006699'>settinggroup</font> table populated.<br>\n";

$settinggroupid=$DB_site->insert_id();
$DB_site->query("INSERT INTO setting VALUES (NULL,$settinggroupid,'News forum ID','newsforumid','15','The specified forum will contain threads viewed on the main page only.','',0)");
echo "Adding setting data... Done. <font color='#006699'>setting</font> table populated.<p></td>\n";

TECK
02-21-2002, 11:22 PM
thanks alot PPN... i posted a thread in Website Feedback. let me know if i'm crazy.. read more in my signature why i decide not to post any hacks anymore here.

however, i will give you a copy of the script once completed, so you can use it. i will keep in touch here through pm's and email.

thanks alot for all your support (scott and chen).