Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
commit gives me strange errors
Old 02-14-2009, 07:09 AM commit gives me strange errors
lizciz's Avatar
Webmaster Talker

Posts: 744
Name: Mattias Nordahl
Location: Sweden
Trades: 0
In my new website I'm using transactions when handling my database, and then commit to make changes. It's been working fine, until now... Suddenly I noticed no changes were made, and I finally tracked the error to the part where I commit.

I have a class Database, in which all database stuff are handled, and in it's contruct I set autocommit to false, and then there's a method to commit, like this
PHP Code:
public function commit() {
   if (!
$this->dbLink->commit()) {
      throw new 
SQLException("Could not commit");
   } else {
      return 
true;
   }

Evan though it wasn't working, a exception was never thrown, which means it was always returning true, right? Still, no changes were made, and I put in var_dumps and echoes here and there to check if all data being sent was correct, and it was. The SQL query being run was correct and no errors occured. Then I tried to remove the line where I set autocommit to false, and it started working! So for some reason it just didn't want to commit when I told it to. Im out of ideas...
lizciz is online now
Reply With Quote
View Public Profile Visit lizciz's homepage!
 
 
Register now for full access!
Old 02-14-2009, 11:43 AM Re: commit gives me strange errors
lizciz's Avatar
Webmaster Talker

Posts: 744
Name: Mattias Nordahl
Location: Sweden
Trades: 0
Ah! I figured out the problem, but it gave me a new one :P
See, I have a couple of classes extending the Database class, for handling specific parts of the site. And every instance of the class gets a mysqli object of it's own. In the class where the error occured I called one of the other child classes of Database, and called a method from it. So, when calling the commit only the changes of the current class where committed, and the other child class' changes were discarded.

So, I thaught I'd let them all share the same musqli object, by making it a static variable. But I get warnings in some places telling me "Couldn't fetch mysqli in ...". Perhaps I'm doing it the wrong way. This is a part of my Database class, see anything wrong with it?

PHP Code:
class Database {

   protected 
$dbLink// this is the mysqli object, bad name I know, but I dont wanna change it in a hundred places :P
   
protected $host;
   protected 
$username;
   
// etc...

   
protected static $sharedLink null// the static mysqli object

   
public function __construct() {
      if (!
is_null(Database::$sharedLink)) {
         
$this->dbLink Database::$sharedLink;
      } else {
         
$this->host DATABASE_HOST;
          
$this->username DATABASE_USERNAME;
         
// etc...

         
$this->dbLink = new mysqli($this->host$this->username$this->password$this->database);
         if (
$this->dbLink->connect_error) {
            throw new 
ConnectException($this->dbLink->connect_error);
         }

         
$this->dbLink->autocommit(false);
         
Database::$sharedLink $this->dbLink;
      }
   }


lizciz is online now
Reply With Quote
View Public Profile Visit lizciz's homepage!
 
Old 02-15-2009, 11:17 AM Re: commit gives me strange errors
lizciz's Avatar
Webmaster Talker

Posts: 744
Name: Mattias Nordahl
Location: Sweden
Trades: 0
I don't know what I did, nothing I think, but it's working now
I changed the reference to the static variable from Database::$sharedLink to self::$sharedLink, but both seem to be working any way. The only thing I can think of is that I never actually tested the code above , but some other code I tried before I wrote the one above.

Any how, thanks to all the people who might have awnsered if I would have waited just a little longer!
lizciz is online now
Reply With Quote
View Public Profile Visit lizciz's homepage!
 
Reply     « Reply to commit gives me strange errors
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.11738 seconds with 12 queries