Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5942375
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T16:14:40+00:00 2026-05-22T16:14:40+00:00

Under other circumstances I might be tempted to use $result = mssql_query(INSERT INTO table

  • 0

Under other circumstances I might be tempted to use

$result = mssql_query("INSERT INTO table (fields) VALUES (data); 
                       SELECT CAST(scope_identity() AS int)");

but as I will be inserting user-submitted data, I want to continue to use PDO, which returns an empty array.

Unfortunately, I’m running PHP on a Linux server and using dblib to interface with Microsoft SQL Server, which doesn’t support PDO::lastInsertID().

Please help!

Update to include code example

Here’s the code I’m using: col1 is a field of type int identity and col2 is a datetime with a default of getdate().

//  Connect to db with PDO
$pdo = new PDO( 'dblib:host=' . $host . ';dbname=' . $database . ';', $username, $password, array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION) );
//  Connect to db with MSSQL
$sql = mssql_connect( $host, $username, $password );
mssql_select_db( $database, $sql );
//  Create SQL statement
$query = "INSERT INTO [table] ( col3, col4, col5 )
          VALUES ( 'str1', 'str2', 'str3' );
          SELECT SCOPE_IDENTITY() AS theID;";
//  Run with MSSQL
echo "Using MSSQL...\n";
$result = mssql_query( $query );
$the_id = mssql_result( $result, 0, 'theID' );
echo "Query OK. Returned ID is " . $the_id . "\n";
// Run with PDO
echo "\nUsing PDO...\n";
$stmt = $pdo->query( $query );
$result = $stmt->fetchAll( PDO::FETCH_ASSOC );
print_r( $result );

And this is what was displayed:

Using MSSQL...
Query OK. Returned ID is 149

Using PDO...
Array
(
)

I would love to find out that I’d done something stupid, rather than come up against a horrible dead end 🙂

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T16:14:41+00:00Added an answer on May 22, 2026 at 4:14 pm

    You’ve got a few choices:

    SELECT @@IDENTITY - return the last ID created by actions of the current connection, regardless of table/scope
    
    SELECT SCOPE_IDENTITY() - last ID produced by the current connection, in scope, regardless of table
    
    SELECT IDENT_CURRENT('name_of_table'); - last ID produced on that table, regardless of table/scope/connection
    

    Of the three, SCOPE_IDENTITY() is the best candidate.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Under what circumstances might you want to use multiple indirection (that is, a chain
I'm migrating some data into MySQL and the dump from the other database has
In C# is there any real difference (other than syntax) under the hood between:
I'm wondering how other programmers are backing up changes that are not under source
Galileo came out the other day, and even though plugins under Eclipse are, IMO,
Under VS's external tools settings there is a Use Output Window check box that
Under what circumstances - if any - does adding programmers to a team actually
Under what circumstances would this or would this not be safe? I have a
Under what circumstances will the False part of the following code be executed? x
I have a UITableView that is styled as a grouped tableView. Under certain circumstances

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.