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 6003853
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T01:10:48+00:00 2026-05-23T01:10:48+00:00

(Baring in mind when I wrote this it works for a MSSQL database no

  • 0

(Baring in mind when I wrote this it works for a MSSQL database no problems, and now I’m writing the same thing for MySQL using SELECT LAST_INSERT_ID() instead of @SCOPE_IDENTITY)
I have a piece of code that enters a company and then the client details for that company. The first SELECT LAST_INSERT_ID() works fine for inserting the company_id from the companies table to the clients table but then throws an error to retrieve the client_id.

Here’s the code:
//// Set initial SQLs

$SQLINSERTCOMPANY = "insert into companies(company_name,is_organisation,company_regno,company_url,is_active,entry_date)
            VALUES ('$company_name',$is_organisation,'$company_regno','$company_url',1,'$entry_date');";

$SQLINSERTCOMPANYDETAILS = "insert into clients (client_name,client_tel,client_mobile,client_email,client_usern,client_pwdx,mailing_list,savedsession,salt,company_id)
            VALUES ('$client_name','$client_tel','$client_mobile','$client_email','$client_email','$psw',$sign_up,'$PHPSESSIONID','$salt'";

$SQLUPDATECOMPANY = "update clients 
        set company_name = '$company_name',
        is_organisation = $is_organisation,
        company_regno = '$company_regno',
        company_url = '$company_url',
        is_active = 1,
        entry_date = '$entry_date' ";

$SQLUPDATECOMPANYDETAILS = "update clients 
            set client_name = '$client_name',
            client_tel = '$client_tel',
            client_mobile = '$client_mobile',
            client_email = '$client_email',
            client_usern = '$client_email',
            client_pwdx = '$psw',
            mailing_list = $sign_up,
            savedsession = '$PHPSESSIONID' ";

////get the cart for later use if the cart has items
$cart = $_SESSION['cart'];
if($cart){
    $strSQL = "select *
        from category_items 
        where category_item_id IN ($cart) order by category_items.item_code;";          
    $query_get_value = mysql_query($strSQL) or die ('query failed ' . mysql_error());
}   

///// add a new client here
if($_POST['action'] == "add"){  

    ////// Insert new company or just update the details
    if($no_of_client_entry == 0) {

        ////// update clients
        $strSQL1 = $SQLINSERTCOMPANY;

        ////// Insert Client Details
        $strSQL2 = $SQLINSERTCOMPANYDETAILS . ",(SELECT LAST_INSERT_ID())); SELECT LAST_INSERT_ID() as NEWID;";

        $sent_to = $client_email;
        $headers = "MIME-Version: 1.0" . "\r\n";
        $headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
        $headers .= "FROM: TheImageLounge<no-reply>";
        $subject = "Welcome to The Image Lounge";
        $message = "<html><head><title>Error Report</title>";
        $message .= "<style type='text/css'>
                        body{margin: 0 auto;text-align:left;}
                        h1, h2, h3 { margin:10px;padding:10px;background-color:#ccc;}                   
                     </style>";
        $message .= "</head><body>";

        $message .= "<p>Welcome to Website... TBC/p>";
        $message .= "<p>Your Details Are:</p>";
        $message .= "<p>Your username: $client_email<br />";
        $message .= "<p>Your password: " . $_SESSION['client_psw'] . "</p>";
        $message .= "<p>We hope that you will enjoy using our website.</p>";
        $message .= "<p>Thank You and best regards,<br />";
        $message .= "<p>The Website.</p>";
        $message .= "</body></html>";

        $sentOK = mail($sent_to,$subject,$message,$headers);    

    } else { ///// just update the client details

        $client_id = $clients_rows['client_id'];

        ////// update clients
        $strSQL1 = $SQLUPDATECOMPANY . " where client_id = $client_id;";

        ////// Insert Client Details
        $strSQL2 = $SQLUPDATECOMPANYDETAILS . " where client_id = $client_id; SELECT client_id as NEWID from clients where client_id = $client_id;";            

    } ///// End if
    ///echo $strSQL2;
    ///// Run transactions

    echo $strSQL1 . "<br />";
    echo $strSQL2;

    $query_insert_client = mysql_query($strSQL1) or die('' . mysql_error());
    $query_insert_clients = mysql_query($strSQL2) or die('' . mysql_error());   

    $last_client = mysql_fetch_assoc($query_insert_clients);
    $client_id =  $last_client['NEWID'];
    mysql_free_result($query_insert_clients);   

Now when I output the SQL it looks like this:

insert into companies(company_name,is_organisation,company_regno,company_url,is_active,entry_date) VALUES ('Individual',0,'','',1,'2011-06-08 14:16:33');

insert into clients (client_name,client_tel,client_mobile,client_email,client_usern,client_pwdx,mailing_list,savedsession,salt,company_id) VALUES ('Andis','0777','','andrew@com','andrew@com','6be86ab8355979352cdd28bbe7026be71a196936fc1ab0c315629f68c0d5f9162acd20b201a27d0ced0cfd53a8e880a1e8bf3714833ed9c4cbe6d0fe3bf15ca0',0,'8730dfec5ce01b315e1a31007185c486','SALTYDOG',(SELECT LAST_INSERT_ID())); SELECT LAST_INSERT_ID() as NEWID;

But then I get this error

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '; SELECT LAST_INSERT_ID() as NEWID' at line 2

If I copy and paste the outputted SQL into MySQL it works fine. So I am not sure what why this fails on the second query. This works fine when working with MSSQL database.

Any help would be grateful.

thanks

andi

  • 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-23T01:10:49+00:00Added an answer on May 23, 2026 at 1:10 am

    You can not run 2 queries at the same time with mysql_query()
    You can however do it with PDO.

    To achieve your goal you could completely drop SELECT LAST_INSERT_ID() as NEWID and use mysql_insert_id()

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

Sidebar

Related Questions

Bearing in mind this is for classic asp Which is better, all HTML contained
Bearing in mind that I'll be performing calculations on lat / long pairs, what
I been waiting for sometime now to bring my Asp.net Preview 4 project up
I'm currently converting some legacy code to create PDF files using iTextSharp. We're creating
We've got the following scenario: Central Database (replicated across multiple servers) Client Database 1
I'm heavily basing my code off of this excellent tutorial at Ars Technica, so
I want to bring up a file dialog in Java that defaults to the
How can I bring a console application window to front in C# (especially when
I'm trying to bring myself up to speed on C#, having never developed for
I may be barking up the wrong tree... However, what I have is a

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.