Page 1 of 1

MySQL Class or MySQLi?

Posted: Sat Jul 19, 2008 4:56 am
by Starcraftmazter
Sup

For a long while now, I've used a single mysql class to handle all the database operations in my scripts and projects, building it up over time.

Now I wish to rewrite it or some of it, to make it a bit cleaner and rename most of the functions to have more meaningful and consistent names. I'm wondering however, whether I should be looking into mysqli. I have no problem with the fact that it's limited to php5 and mysql 4.1.

So I have a few questions:
1. Are there any performance increases in mysqli over mysql?
2. Does mysqli provide anything which is unachievable with regular mysql functions? (And if so, what)

My class does everything I need right now, so I don't know whether it's of any use to use mysqli or create a database abstraction class using mysqli instead of mysql, and I'm hoping someone can help steer me into the right direction.

My current class.


Thanks
PS. Hope this is in the right place.

Re: MySQL Class or MySQLi?

Posted: Sat Jul 19, 2008 6:27 pm
by alex.barylski
Go with MySQLi -- "i" stands for Improved...

The API is more extensive and it's for newer versions which support rollback/commit and such.

Secondly, why write your own database abstraction class?

Yes, you will kill performance. You didn't really write a abstraction class, so much as simply wrap the existing mysql API up in a class -- big difference.

An abstraction class lets you swap RDBMS by changing one line of code...yours would require updating each call to the mysql_* functions.

If you need abstraction, use a class like AdoDB (lite) or better yet just use PDO and something like phrasebook to keep SQL separate from code inorder to quickly hammer out SQL specific to a RDBMS.

If you don't need (or anticipate) any RDBMS other than MySQL just use the API directly inside your models.

Re: MySQL Class or MySQLi?

Posted: Mon Jul 21, 2008 3:25 am
by Starcraftmazter
But transactions are simple SQL commands, what do you mean mysqli supports it?

The reason why I wrote my own class, it because it saves a lot of work and a lot of code. It saves me hundreds of lines of code in each individual .php file I make for any project, so it's far more convenient.

Currently, I'm only anticipating MySQL, that may change, but it's not a factor for me at the moment. I just wish to know whether there are any distinct advantages in using mysqli over mysql.

Re: MySQL Class or MySQLi?

Posted: Mon Jul 21, 2008 4:24 am
by alex.barylski
Currently, I'm only anticipating MySQL, that may change, but it's not a factor for me at the moment. I just wish to know whether there are any distinct advantages in using mysqli over mysql.
The API is improved...and the older one will eventually become deprecated I would assume.

It's proabably best to switch now before your code becomes much larger and less managable.

From php.net:
The mysqli extension allows you to access the functionality provided by MySQL 4.1 and above
It looks like MySQLi provides a class to work with so it would offer you the advantages of OOP as well. You could simply extend the MySQLi class or pass it in via injection.

When something newer and better comes along it's usually good practice to adopt immediately. Especially when the company who develops both products tells you to.

The only disadvantage I can see is that some shared hosting companies might not have upgraded to mysqli or have it installed which is why database abstraction layers are handy.

Re: MySQL Class or MySQLi?

Posted: Wed Jul 23, 2008 12:47 pm
by ghurtado
When something newer and better comes along it's usually good practice to adopt immediately
That could be some dangerous advice to be giving as a blanket statement. Ask the millions of corporate users that jumped on the Vista bandwagon too early and their problems with unsupported drivers and applications.

Other than that, use MySQLi, of course.

Re: MySQL Class or MySQLi?

Posted: Wed Jul 23, 2008 1:56 pm
by alex.barylski
In all fareness, the context of this disscussion was about an API not the entire application.

When a newer API is made public and declared stable it's probably safe to assume it's the successor of the previous.

Re: MySQL Class or MySQLi?

Posted: Fri Aug 29, 2008 6:00 pm
by koen.h
Hockey wrote:Go with MySQLi -- "i" stands for Improved...

Yes, you will kill performance. You didn't really write a abstraction class, so much as simply wrap the existing mysql API up in a class -- big difference.

An abstraction class lets you swap RDBMS by changing one line of code...yours would require updating each call to the mysql_* functions.
Let's say each of the methods in this mysql class are part of an interface. Next to that the class is chosen by means of a simple factory. Wouldn't that count as abstraction?