Yes, except that in the second case you first will have to do the insertion
of $sql and then assign $lastId to mysql_insert_id();

Cheers,
Maxim Maletsky

-----Original Message-----
From: Jacky@lilst [mailto:[EMAIL PROTECTED]]
Sent: Thursday, January 25, 2001 7:36 AM
To: Maxim Maletsky; [EMAIL PROTECTED]
Subject: Re: [PHP] last_insert_id function


So can I do either way as I show below here?
first could be :
$sql = "insert into user (name,email) values('Jack','[EMAIL PROTECTED]')";
$result = mysql_query($sql);
$sql1 = "insert into userFriend (userId,friendName,)
values(LAST_INSERT_ID(),'John');"
$result1 = mysql_query($sql1);

or second way is:
$sql = "insert into user (name,email) values('Jack','[EMAIL PROTECTED]')";
$lastId = mysql_insert_id();
$result = mysql_query($sql);

$sql1 = "insert into userFriend (userId,friendName,)
values($lastId,'John');"
$result1 = mysql_query($sql1);


Are these correct?

Jack
[EMAIL PROTECTED]
"There is nothing more rewarding than reaching the goal you set for
yourself"
----- Original Message -----
From: Maxim Maletsky <[EMAIL PROTECTED]>
To: 'Jacky@lilst' <[EMAIL PROTECTED]>; <[EMAIL PROTECTED]>
Sent: Wednesday, January 24, 2001 3:25 AM
Subject: RE: [PHP] last_insert_id function


> for example:
>
> $SQL = "INSERT INTO users SET name='Maxim', surname='Maletsky'";
>
> now you have to insert into another table where you need to relate that
user
> to the entry:
>
> $SQL2 = "INSERT INTO questions SET question='how did you sleep?',
> made_by=LAST_INSERT_ID()";
>
> LAST_INSERT_ID() will be here equal to the auto_incremented id of the
first
> $SQL statement.
>
> Hope this helps,
> Maxim Maletsky
>
>
> -----Original Message-----
> From: Jacky@lilst [mailto:[EMAIL PROTECTED]]
> Sent: Thursday, January 25, 2001 7:16 AM
> To: [EMAIL PROTECTED]
> Subject: [PHP] last_insert_id function
>
>
> I got this quote right out of the php manual. My Id field happen to be
type
> BIGINT as it said so I tried using LAST_INSERT_ID();  and turn out to be
> error said "unidentified function". Any clue?
> *******************************************
> mysql_insert_id() converts the return type of the native MySQL C API
> function mysql_insert_id() to a type of long. If your AUTO_INCREMENT
column
> has a column type of BIGINT, the value returned by mysql_insert_id() will
be
> incorrect. Instead, use the internal MySQL SQL function LAST_INSERT_ID().
> ********************************************
> Jack
> [EMAIL PROTECTED]
> "There is nothing more rewarding than reaching the goal you set for
> yourself"
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
> To contact the list administrators, e-mail: [EMAIL PROTECTED]
>

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]

Reply via email to