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

  • SEARCH
  • Home
  • 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 589969
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T15:28:46+00:00 2026-05-13T15:28:46+00:00

MY PLATFORM: PHP & mySQL MY SITUATION: I am trying to implement transactions within

  • 0

MY PLATFORM:

PHP & mySQL

MY SITUATION:

I am trying to implement transactions within my code. I tried to follow examples, but it’s not much help. I am running 3 queries and I wanted to write a transaction in such a way so that if any of the query(ies) fail, the whole transaction should roll back. I would really appreciate a simple, efficient and non-object oriented PHP code to achieve this goal. Thank you in advance.

MY PHP CODE:

//db_res calls a custom function that performs a mysql_query on the query
$res1 = db_res("SELECT c1, c2 FROM t1 WHERE c5 = 3");
$res2 = db_res("UPDATE t2 SET c1 = 5 WHERE c2 = 10");
$res3 = db_res("DELETE FROM t3 WHERE c1 = 20");

if( $res1 && $res2 && $res3 )
{
 //commit --- but how?
}
else
{
 //rollback --- but how?
}
  • 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-13T15:28:46+00:00Added an answer on May 13, 2026 at 3:28 pm

    You need to use the mysqli extension to use this functionality.

    See: autocommit(), commit(), and rollback()

    <?php
    $link = mysqli_connect("localhost", "my_user", "my_password", "world");
    
    /* check connection */
    if (mysqli_connect_errno()) {
        printf("Connect failed: %s\n", mysqli_connect_error());
        exit();
    }
    
    /* disable autocommit */
    mysqli_autocommit($link, FALSE);
    
    mysqli_query($link, "CREATE TABLE myCity LIKE City");
    mysqli_query($link, "ALTER TABLE myCity Type=InnoDB");
    mysqli_query($link, "INSERT INTO myCity SELECT * FROM City LIMIT 50");
    
    /* commit insert */
    mysqli_commit($link);
    
    /* delete all rows */
    mysqli_query($link, "DELETE FROM myCity");
    
    if ($result = mysqli_query($link, "SELECT COUNT(*) FROM myCity")) {
        $row = mysqli_fetch_row($result);
        printf("%d rows in table myCity.\n", $row[0]);
        /* Free result */
        mysqli_free_result($result);
    }
    
    /* Rollback */
    mysqli_rollback($link);
    
    if ($result = mysqli_query($link, "SELECT COUNT(*) FROM myCity")) {
        $row = mysqli_fetch_row($result);
        printf("%d rows in table myCity (after rollback).\n", $row[0]);
        /* Free result */
        mysqli_free_result($result);
    }
    
    /* Drop table myCity */
    mysqli_query($link, "DROP TABLE myCity");
    
    mysqli_close($link);
    ?>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

PLATFORM: PHP, mySQL & jQuery WHAT I HAVE: I have a Database table. Within
PLATFORM: PHP & mySQL For my experimentation purposes, I have tried out few of
MY PLATFORM: PHP & mySQL MY SITUATION: I came across a situation where I
MY PLATFORM: PHP & mySQL MY SITUATION: I am building an app. where users
I am building a platform on php/mysql that allows users to but for a
PLATFORM: PHP & mySQL I am storing the date+time in database in the following
MY PLATFORM: PHP & mySQL WHAT I HAVE HERE: I have 4 tables, namely,
This is a question about Paypal Mass Pay IPN. My platform is PHP &
I need to run Linux-Apache-PHP-MySQL application (Moodle e-learning platform) for a large number of
I installed PHP with Web Platform Installer. Now I need to install PEAR but

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.