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.
MySQL Class or MySQLi?
Moderator: General Moderators
-
Starcraftmazter
- Forum Commoner
- Posts: 45
- Joined: Mon Apr 24, 2006 11:36 pm
-
alex.barylski
- DevNet Evangelist
- Posts: 6267
- Joined: Tue Dec 21, 2004 5:00 pm
- Location: Winnipeg
Re: MySQL Class or MySQLi?
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.
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.
-
Starcraftmazter
- Forum Commoner
- Posts: 45
- Joined: Mon Apr 24, 2006 11:36 pm
Re: MySQL Class or MySQLi?
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.
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.
-
alex.barylski
- DevNet Evangelist
- Posts: 6267
- Joined: Tue Dec 21, 2004 5:00 pm
- Location: Winnipeg
Re: MySQL Class or MySQLi?
The API is improved...and the older one will eventually become deprecated I would assume.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.
It's proabably best to switch now before your code becomes much larger and less managable.
From php.net:
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.The mysqli extension allows you to access the functionality provided by MySQL 4.1 and above
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?
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.When something newer and better comes along it's usually good practice to adopt immediately
Other than that, use MySQLi, of course.
-
alex.barylski
- DevNet Evangelist
- Posts: 6267
- Joined: Tue Dec 21, 2004 5:00 pm
- Location: Winnipeg
Re: MySQL Class or MySQLi?
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.
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?
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?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.