[ Curiosity,Experimentation ]

Random stuff from the parallel universe of Ones and Zeroes

An abstraction for MySQL transactions in PHP

Posted by appusajeev on January 16, 2010

PHP comes with intrinsic support for MySQL. A handful of functions exist for performing various database transactions. But when the number of pages containing database interactions increases,it becomes a pain to handle the connections and query processing on each page.This also affects the portability of the app since you have to make quite a lot modifications to up the app in the  new server environment. So i thought of creating a class ,which besides providing some abstraction ,can be reused.By reusing the class,ease of portability may be achieved since the environment dependent area has been isolated to a singe module/class(software engineering principles,he he).

The class exports  methods like connect(), selectdb(),query(),get_entry(),count_result() which are nothing but  a higher level abstractions over functions like mysql_connect(),mysql_query() while the other parameters like link identifier ,result resource etc remains hidden from the user.
The result of the query(SELECT query) becomes available in the array row with field name as the key.I think the rest is all self explanatory.
The script file containing the class can be included in the required page using include() PHP function.

Download Source

An abstraction for PHP MySQL  transactions

An abstraction for PHP MySQL transactions


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: