Re: mssql: insert a record and return the auto increment ID
| From: | Heiko Mundle | Date: | Tue, 03 Sep 2002 09:52:06 +0000 |
| Subject: | Re: mssql: insert a record and return the auto increment ID | ||
| References: | 1 | Groups: | php.db |
| Request: | Send a blank email to php-db+get-21528@lists.php.net to get a copy of this message | ||
> >
> > To insert the record I use this code:
> > $sSql = "INSERT INTO myTable (time_in ) VALUES ( getdate() )";
>
> try something like
> $sSql = "INSERT INTO myTable (time_in ) VALUES ( getdate() ); SELECT
> @@IDENTITY";
>
> works with the Perl DBI at least.
>
> :-)christian
>
But if I get the ID in a second queries on a very busy web server with less
persistant database connection, it is not reliable. If two clients execute
this call at the same time with the same connection, I would possiblely get
the wrong ID, wouldn't I? Is there a way to do this in one step?
By the way, I can't use a string with more than one query in the
mssql_query() function. I get error like:
Attempt to initiate a new SQL Server operation with results pending
on the next query() call.
Heiko
"Heiko Mundle" <hmundle1@gmx.net> schrieb im Newsbeitrag
news:20020902131801.9234.qmail@pb1.pair.com...
> I insert a new record with PHP into my MS SQL Server database.
> The key is defined with
> auto_id INT IDENTITY(1000,1) NOT NULL,
>
> how do I get the new id to use it in PHP?
>
> To insert the record I use this code:
> $sSql = "INSERT INTO myTable (time_in ) VALUES ( getdate() )";
> $pResult = mssql_query($sSql, $g_pDBServer);
>
>
> Thanks a lot for your help
>
> Heiko
>
>