Using a class for a connection with a MYSQL DB

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
dmonteiro3
Forum Newbie
Posts: 1
Joined: Mon Jun 21, 2010 7:20 pm

Using a class for a connection with a MYSQL DB

Post by dmonteiro3 »

First of all hello to everybody.
This is my first post here on the forum. I always looked around the forums and sites for questions I had, but I never joined a community. Well, my time has come, and I hope to get helped, and help anyone who needs a hand.

My thing is that I am starting a project. I was discussing with the other programmer involved in the project which type of connection to use with MYSQL. We were thinking about creating our own class, the mysqli class, and the pdo class. We choose the pdo. But please fell free to comment about the pros and cons of those options. Anyway, we found that in the pdo class exists three functions to handle errors. the beginTransaction() commit() and rollback(). I look at the php.net documentation, and tried a few different codes and saw that if I use the beginTransaction() and don't use the commit() the SQL code is not saved in the database.
My question is, how can I use those 3 functions to make sure if a query is run at the server, and any error happens during the process, no data is loss. Maybe a try catch?

This is the class I am using it to create the PDO object --> http://www.phpclasses.org/package/2855- ... l#download
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Using a class for a connection with a MYSQL DB

Post by requinix »

Those three functions are for transactions. If you don't know what they do then you don't need them ;)

(It basically lets you run a bunch of queries one after another while giving you a chance to undo them all if you discover there's a problem and want to abort the process.)


Ugh. Never been a fan of phpclasses.org... If you ask me, the mysqli class is plenty good and easy for those familiar with the mysql extension to get on board with.
Phoenixheart
Forum Contributor
Posts: 123
Joined: Tue Nov 16, 2004 7:46 am
Contact:

Re: Using a class for a connection with a MYSQL DB

Post by Phoenixheart »

Isn't it how a transaction should be? If you begin() a transaction without commit(), the data shouldn't be saved. Same when any errors happen.
A try... catch is often used afaik. Like this (pseudo code):

Code: Select all

try
{
    beginTransaction;
    runQueries;
    commitTransaction;
}
catch (DatabaseException e)
{
    rollbackTransaction;
    log e;
}
Post Reply