I bought VPS from Hostgator to download mysqlnd (because I used a lot get_result method) but they said that they will delete libmysql and will install mysqlnd and also it will be my responsibility to maintain mysqlnd (I have no experience to maintaining it yet).
I want to learn what I will lose when they delete libmysql? What are the cons of mysqlnd? Why is it hard to maintain mysqlnd?
what I will lose when they delete libmysql?
Nothing.
What is the cons of mysqlnd?
Didn't you say yourself that you're using get_result all over the site? Is it not enough benefit for you?
Why is it hard to maintain mysqlnd?
It's not. There is nothing to maintain.
I don't use PDO because it is slow a little bit
Oh, that nasty rumor again.
However, if mysqli functions are wrapped in a good helper library, there is nothing wrong with it.
But in case either mysqli or PDO are intended to be used raw, right in the code without any intermediate wrapper, then for a programmer PDO is the only choice. Yet I have to admit that for a regular PHP user mysqli is better because resembles the only way for database interaction they learned once and for all.
Related
In my efforts to update my code from using mysql_*, I have run into a whole heap of pain.
I decided to change my code using mysqli, first in the procedural style. After learning a bit more, I decided that actually the object-oriented style was better. When it came to using prepared statements, I ran into problems when I tried to use get_result(). I wanted to use this because I had lots of fields which needed to bound. So after looking into the issue, it appears you need the mysqlnd driver to use this, which is only available in PHP version 5.4. I was using version 5.3 so decided to upgrade.
After upgrading, I still had the error. After chasing my tail for ages, as this was something definitely outside of my area of knowledge, I discovered that mysqlnd had not been enabled for mysqli.
After speaking to my webhost support, I got the following reply:
It seems that mysqli can only be built against either the old libmysql libraries, or
against mysqlnd. cPanel apparantly defaults to the older libraries since mysqli has been
used for a long time with the older libraries.
We can try to force mysqli to build against mysqlnd ... but I have no
idea what repercussions this might have if any other sites use mysqli
already against the old libraries
This doesn't sound like a good idea, and at this stage (with plenty of time wasted) I'm thinking I should have never bothered and just stuck with mysql_* instead.
PDO has been mentioned but I know nothing about this. It seems similar to mysqli? I really don't want to invest more time in learning another something new, only then run into a similar brick wall further down the line.
There certainly isn't any plans to switch to a different type of database, so doesn't that defeat the object of using PDO? Should I just try to find some code that does the same thing get_result() does but that would work in 5.3?
I think PDO would be a better solution then mysqli in any case. It's a native library of PHP, it works very well and it's pretty easy to use.
Although they have been deprecated, the mysql_* functions are a long way from being removed entirely. However, once you've decided to make the switch from mysql_* you may as well go with the de facto standard, which is now the PDO library. It offers extra features over mysqli (named parameters, prepared statements), it works with more database vendors, security is a bit better, while the performance difference is neglible.
In your case, I would test that PDOs work on your hosting environment. If they don't, then change host.
Would PDO be better?
Yes.
Is it like mysqli?
No.
It runs SQL queries properly, as opposite to the way you were using either old mysql or new mysqli.
Recently the PHP manual started showing the following warning on every mysql function page:
Use of this extension is discouraged. Instead, the MySQLi or
PDO_MySQL extension should be used. See also MySQL: choosing an API
guide and related FAQ for more information...
MySQLi used to be very buggy, but have they improved it so that it's finally worthy of its name? Is that why they're abandoning the MySQL extension and trying to get people to use MySQLi?
Actually, I would like to use MySQLi if it's not buggy anymore. It has more features and it's object oriented.
Any comments on this?
//EDIT: What I want to know is if it's OK to use MySQLi. Or is it still buggy? Should I go with PDO instead?
Yes. Since (very) long. We now have mysqli, or better yet, PDO.
I wouldn't lock myself into mysqli, I'd prefer PDO. Beside the easier migration it offers from one database system to another, it also offers better error handling.
What I want to know is if it's OK to use MySQLi. Or is it still buggy?
MySQLi itself is quite bug-free and it's used in production.
Should I go with PDO instead?
If your only argument for using mysqli is its similarity to mysql, then you'd probably not use mysqli to its full potential anyway. If you want to use mysqli to its full potential, then you'd have to start learning "anew" (it's not terribly much to learn, you know). If you start learning some new tool from "scratch", then why not learn the better alternative - PDO, in the first place?
On the other side, PDO is not perfect either. With PDO, you cannot access MySQL specific APIs (such as post-construct set_charset, infile settings, async queries, OUT params from prepared statements). Also, you should set it to do true prepared statements if you need them.
The PHP MySQLi is an MySQL Improved Extension.
The mysqli extension allows you to access the functionality provided by MySQL 4.1 and above.
You can compare both of them at The MySQLi Extension Function Summary.
If you are searching for a future proof solution, object oriented, the way to go is PHP PDO.
The PHP Data Objects (PDO) extension defines a lightweight, consistent interface for accessing databases in PHP. Each database driver that implements the PDO interface can expose database-specific features as regular extension functions.
...
PDO provides a data-access abstraction layer, which means that, regardless of which database you're using, you use the same functions to issue queries and fetch data.
Yes you can go with MySQLi and what you write is true, the API allows an easy change from the MySQL API.
For new projects it's recommended to not use ext/mysql any longer, but to use ext/mysqli or PDO_MySQL.
As you have not written what was buggy for you back in 2009, it's hard to say if these bugs are gone. I would assume so, but, well, check for yourself.
You might want to also use ext/mysqli with the MySQL native driver instead of the MySQL client server library (libmysql).
It was about time. The mysql API, though easy to use, suffers from many problems. Arguably the worst problem is the complete lack of support for prepared statements, which forces you to piece together bits of SQL through string operations. This is not only slow but also a major source of SQL injection vulnerabilities.
One of the advantages of PDO over MySQLi is that you'll find that you don't have to learn a new API when you decide to use a different DBMS in a future project.
Both seem to try making it simpler using a database in PHP. Both seem to provide an abstraction over different database types like MySQL, SQLite, etc.
What are the differences between both ADOdb and PDO?
PDO is standard in PHP as of version 5.1. (It is also available with a PECL extension in PHP 5.0) Most hosting provides will have it enabled. AdoDB is not a standard extension.
Also, I believe the PDO drivers are "PHP-native": they are built on top of the same libraries that PHP itself was built on, and use the same underlying routines for things like memory management. So potentially, PDO is more lightweight than AdoDB.
According to this benchmark, AdoDB is considerably slower than PDO: (fixed link)
https://gist.github.com/tony-landis/31483
Of course, you should consider whether this is important enough for your use case to prefer PDO or not.
From a technical perspecitve, the most notable difference would be that PDO is a native extension and, from PHP 5 on, always included in PHP in its fast, compiled form. There is an extension for ADODb as well but you have to install it in PHP first. This is a strong argument in favour of PDO because products based on it are likely to run faster in more environments.
ADOdb supports a larger number of databases than PDO.
Well, I think it boils down to preference. ADOdb is more geared towards people who are used to the Microsoft style of Database access (ADO) and PDO is more "PHP" like and also part of the mainstream of PHP versus ADOdb which sort of sits off to the side.
At the end of the day, it would based on what your target DB is (ADOdb supports more) and what sort of language style your prefer. Personally, I like PDO and it suits my needs.
PDO is native and pretty fast.
ADOdb is a richer library and even has things like ORM (Object Relational Mapping).
For me the big downside of PDO is it's horrible to debug when it goes wrong as there's no PHP source for it. When I was debugging some complicated code the only way I could see the exact SQL that was being executed was the subclass the PDO driver itself...
It's all opinion though of course!
What DB extension PHP has (mysqli, PDO etc) is the best for enterprise level application?
The important features that comes to my mind are:
under active maintenance
A lot of documentation and examples.
Probably endorsed by the Mysql people themselves.
Robust
scalable
1. under active maintenance
mysqli and PDO. if you end up using a framework, it will probably be using mysqli anyway.
2. A lot of documentation and examples.
mysqli and PDO.
3. Probably endorsed by the Mysql people themselves.
mysqli. see http://dev.mysql.com/doc/refman/5.0/en/apis-php.html
4. Robust
if i had to point to 1 single PHP extension that answers the question "which one is used most often and has the most number of people scrutinizing it for bugs?", i would have to vote for mysqli. if you have a different definition of robust, you'll have to elaborate.
5. scalable
it depends what you mean by scalable. in the mysqli vs. PDO shootout, mysqli has the tightest codebase and is therefore more scalable simply because the code is lighter. YMMV. IANAL.
but if you want something that helps you to scale (such as distributing load across a couple of mysql servers) you'll need something to wrap mysqli/PDO because nothing at the mysqli/PDO/whatever level will do this for you.
Real enterprise applications require not only a database extension, but ORM. I've heard sth about Doctrine, but can't tell you if it's stable, because I haven't used it.
I've looked at the features that it offers and it seems that this project is there for a while, because it has support for lazy loading, many-to-many relationships, inheritance, which are features that most of new ORMs don't have.
I've been using ADODB for PHP on several projects for quite some time, and I like it for the ease of use and the efficiency.
I've never been too curious about the way that lib accesses data because you know...it just worked :) But today I realized I'm still relying on the legacy MySQL4 ADODB drivers. I'm using MySQL 5.x, and it would probably be better if I started using a recent driver with ADODB.
But there are two drivers I could use :
adodb-mysqli.inc.php
adodb-pdo_mysql.inc.php
From what I read mysqli is pretty similar to the old mysql extension, optimized for MySQL5, while PDO is a layer between PHP and various DB systems (including MySQL of course).
Which one of these driver do you use ? Which one do you think I should use, and more importantly why should I prefer mysqli over PDO_mysql (or the opposite) ?
Answer : After a few days and some deep code reading, I ended up using the "adodb-mysqli.inc.php" driver. On a kinda-trafic-heavy site, I noticed the DB load went slightly down, and the network trafic between the web server and the db server went down by about 6.5%, which is good.
The PDO-mysql driver is probably pretty good too, but as said below, it doesn't make much sense to use ADODB over PDO. So mysqli it is.
All tests point towards PDO being the most efficient and the fastest driver. I do not know, however, if it makes sense to use PDO over AdoDB
I may be wrong but from what I remember of looking at the drivers, binding of variables in statements is emulated in the adodb mysqli driver despite the mysqli extension supporting binding. The pdo_mysql driver does however do the binding using the extension, so if you are using this you may get better performance.
Also might be worth adding that I think if you want to use the pdo drivers with adodb you have to use a different connection syntax and pass a DSN, there was an example in the docs. I struggled to get it to work for a bit because I didn't read this.