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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T21:44:53+00:00 2026-05-26T21:44:53+00:00

I have 2 sql queries to execute, but I want it to execute all

  • 0

I have 2 sql queries to execute, but I want it to execute all or if error in one query then dont execute any. I’m using php. I used to use try and catch in .NET but I’m new to php.

Below is the code which i was trying to do:

function Registration($UserFirstname,$UserLastname){
$sql="INSERT INTO table1 (fieldname1,fieldname2) VALUES ('$UserFirstname','$UserLastname')";
$res=mysql_query($sql) or die(mysql_error());
$sql="INSERT INTO table2 (fieldname1,fieldname2) VALUES ('$UserFirstname','$UserLastname')";
$res=mysql_query($sql) or die(mysql_error());}
  • 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-26T21:44:54+00:00Added an answer on May 26, 2026 at 9:44 pm

    The problem you’re probably facing with try...catch is that PHP has two different error handling mechanisms: error reporting and exceptions. You cannot catch exceptions unless the underlying code throws them and good old mysql_query() will trigger warnings rather than throwing exceptions. There’re several workarounds but, if you are interested in writing good object-oriented code, I suggest you switch to PDO.

    In any case, if you want to stick to good old MySQL library, your code should basically work:

    $res=mysql_query($sql) or die(mysql_error());
    

    The explanation:

    • mysql_query() returns FALSE if the query fails (e.g., you get a duplicate key)
    • The right side of the or expression will only execute if the left side is FALSE
    • die() aborts the script, thus preventing the next queries to be executed

    However, I presume that you don’t want to abort in the middle of nowhere. If we add some missing bits (such as proper SQL generation and code indentation) we get this:

    function Registration($UserFirstname,$UserLastname){
        $sql = sprintf("INSERT INTO table1 (fieldname1,fieldname2) VALUES ('%s','%s')";
            mysql_real_escape_string($UserFirstname),
            mysql_real_escape_string($UserLastname)
        );
        $res = mysql_query($sql);
        if( !$res ){
            return FALSE;
        }
    
    
        $sql = sprintf("INSERT INTO table2 (fieldname1,fieldname2) VALUES ('%s','%s')";
            mysql_real_escape_string($UserFirstname),
            mysql_real_escape_string($UserLastname)
        );
        $res = mysql_query($sql);
        if( !$res ){
            return FALSE;
        }
    
    
        return TRUE;
    }
    

    About transactions

    Please note that you still need to use transactions if there’s a chance that the second query fails. Transactions are not particularly difficult to use, the only requirements are:

    1. Define the involved tables as InnoDB
    2. Run a START TRANSACTION query on top of the function
    3. Run a COMMIT query at the end of the function
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a method which I use to execute all my SQL queries (inserts,
I have several sql queries that I simply want to fire at the database.
I have a truckload of xsd with plain SQL queries. I want to migrate
I have a JSP page which accepts SQL queries, performs them then returns the
I always have trouble with complicated SQL queries. This is what I have $query
It looks like #temptables created using dynamic SQL via the EXECUTE string method have
I have a text file that include sql queries. Each query ends with ;.
I have 3 similar SQL queries that each will bring back 1 record with
I have a set of sql - queries: List<String> queries = ... queries[0] =
I have a lot of SQL queries like this: SELECT o.Id, o.attrib1, o.attrib2 FROM

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.