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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T22:44:51+00:00 2026-06-12T22:44:51+00:00

I have written a database class(es) sometime ago, I use this for most projects

  • 0

I have written a database class(es) sometime ago, I use this for most projects and recently extended the class to work with transactions.

The Transaction class below does not seem to work correctly, the server is MySQL innoDB and I’ve checked that transactions work on my database(with PHPMyAdmin). So this is obviously an error in my code or my misunderstanding.

<?php

/**
 * Description of Database
 *
 * @author http://stackoverflow.com/users/820750/gerve
 */
class Database {

    private $connection;

    public function __construct($dbServer, $dbName, $dbUser, $dbPassword) {
        $this->connection = mysql_connect($dbServer, $dbUser, $dbPassword);
        mysql_select_db($dbName, $this->connection);
    }

    public function query($query, $die = true) {
        return new Query($query, $this->connection, $die);
    }

    public function escape($param) {
        return mysql_real_escape_string($param, $this->connection);
    }

    public function transaction() {
        return new Transaction($this->connection);
    }

}

class Query {

    private $query;
    public $count;
    public $countModified;
    public $queryString;


    public function __construct($query, $link_identifier, $die = true) {
        if($die){
            $this->query = mysql_query($query, $link_identifier) or die(mysql_error());
        }
        else{
            $this->query = mysql_query($query, $link_identifier);
        }

        $this->count = is_resource($this->query) ? mysql_num_rows($this->query) : 0;
        $this->countModified = mysql_affected_rows($link_identifier);

        $this->queryString = $query;
    }

    public function nextRow() {
        return mysql_fetch_object($this->query);
    }

    public function allRows() {
        $array = array();

        while($row = mysql_fetch_object($this->query)){
            $array[] = $row;
        }

        return $row;
    }

}

class Transaction {

    private $connection;
    private $isAlive;

    public function __construct($link_identifier) {
        $this->connection = $link_identifier;
        $this->query("BEGIN");
        $this->isAlive = true;
    }

    public function query($query) {
        if($this->isAlive){
            $q = new Query($query, $this->connection, false);
            if(mysql_error()){
                $this->rollBack();
                return false;
            }
            else{
                return $q;
            }
        }
    }

    public function rollBack() {
        if($this->isAlive){
            $this->query("ROLLBACK");
            $this->isAlive = false;
        }
    }

    public function commit() {
        if($this->isAlive){
            $this->query("COMMIT");
            $this->isAlive = false;
            return true;
        }
        else{
            return false;
        }
    }

}

?>

EDIT – Example usage of classes

$DB = new Database(dbServer, dbName, dbUser, dbPassword);
$transaction = $DB->transaction();

$transaction->query("INSERT INTO myTable `c1`, `c2` VALUES('1', '2')"); //Works
$transaction->query("INSERT INTO jhsdjkag 5dafa 545"); //Fails
$transaction->query("INSERT INTO myTable2 `c1`, `c2` VALUES('3', '4')"); //Works

$transaction->commit();

The above code should not insert any rows into the database, the second query has failed so none should succeed.

My problem is that it doesn’t rollback, always rows are inserted regardless of failing queries.

  • 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-06-12T22:44:53+00:00Added an answer on June 12, 2026 at 10:44 pm

    I Found that there was an error in the code, just in the __construct(). It was pretty simple, I just changed 1 line:

    FROM:

    public function __construct($link_identifier) {
        $this->connection = $link_identifier;
        $this->query("BEGIN");
        $this->isAlive = true;
    }
    

    TO:

    public function __construct($link_identifier) {
        $this->connection = $link_identifier;
        $this->isAlive = true;
        $this->query("BEGIN");
    }
    

    ->isAlive was being set after the first “BEGIN” query, which means BEGIN was never sent.

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

Sidebar

Related Questions

I have written a custom class to handle database queries to a remote and
I have written a function to print database table to an array like this
have written this little class, which generates a UUID every time an object of
So I have this database class in PHP and I only have 1 function
I have written the Database class & trying to insert the values in the
I have written this stored procedure for one my operations in the database, but
I have written a database class but can't access the method from within: class
I have been creating PHP projects for years, and have previously written Database classes,
I have written a very simple file managing database that basicly looks like this:
I have written an O/R database wrapper that generates some wrapper methods for stored

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.