So this whole day, I was thinking about my source code and I questioned myself. Why support mysqli_* or PDO? Why not support mysqli_* and PDO. In my older versions of my application, I’ve always stuck to supporting either of those.
If someone wanted to use PDO because they were told it is better than mysqli_* and they happened to not like it and wanted to switch to mysqli_*, they would have to do a clean installation. But what if I supported both mysqli_* and PDO. The support for switching wouldn’t be so hard and the user won’t need to do a clean installation because they’ll just need to define a few things on the index file and they’ll be able to switch rather quickly from PDO to mysqli_* and mysqli_* to PDO.
What do you think of this approach? All functions and tables will be the same for both PDO and mysqli_*.
An additional feature the PHP Framework may have is a Query Builder which is parsed, validated and sent to the chosen database. Any user problems encountered are [SOLVED] in System Security Updates and usually do not affect any of your code.
Well, you are the one who should know best. This can be done but it depends on your requirements if it’s worthwhile:
who is this application intended for?
what would the user/programmer gain by being able to easily switch between one driver and the other?
do you want to distribute your application to wide audience and want to make it as compatible as possible with various set-ups?
You can always abstract your database access with your own or an existing library - but if you do this then it won’t really make sense to choose one driver over the other because “it is better” - you will have to accept the common ground and use the functionality present in both otherwise using the unique strengths of either one would require sometimes elaborate emulations and workarounds for the other.
If someone uses PDO or mysqli then I don’t think there is any real advantage of switching between them under the hood unless one of them is not available on the server. However, I think it’s always a good idea to have some database abstraction - but more for yourself for code reuse rather than for arbitrarily switching in a single project.
For many distributed projects it is beneficial to have a large degree of database independence. But I don’t think there is so much benefit in db driver independence within one db type.
Yes, I am using the MVC pattern, that’s why I am asking because I am passing the database information through the construct of my router. It’s kind of like the information in both your larvel and codeigniter examples.
I already know how MVC works.
Per your bullet questions.
The application is intended for both advance and novice users. Mostly, novice users will typically use the GUI part of the application because they aren’t too familiar with any programming yet. And for advanced users, they can add in their codes to a custom model file.
If an advanced or novice user is use to let’s say mysqli_* and they accidentally chose PDO on the installation, they can easily switch back to mysqli_* without having to do a clean installation. Clean installations might not have their files they have customized.
Yes, I think compatibility both ways would be essential to my project. It could make things really easy if someone learns a new library.
Technically, they aren’t passing the database information through the router, but they are defining the database information. But what does this have to do with my original question?
Nothing. You just seemed to be misunderstanding how routers work that was fundamentally wrong and I wanted to address that misunderstanding.
As for “PDO” or “mysqli”, the database abstraction layer you use or design should use one connection source or the other. As an abstraction, it then takes away the choice, as abstractions should.
I would even go as far as to say that MVC has also nothing to do with your question because MVC says nothing about connecting to a database or how to abstract the connection - it is a completely different thing. Whether you use MVC or any other design pattern the database stuff is (and I believe should be) separate. (As to passing the connection through the router - that looks odd - unless your router uses the database for its functioning.)
I think what he wants is to be able to change the driver under the hood in one place and have the application work the same.
spaceshiptrooper, it looks like you believe you have reasons to implement mysql driver independence - if your main question is if this is valid approach then I would say yes. Many projects intended for wide audience do this for compatibility reasons. You may have a look at Doctrine DBAL, which already appears to do what you want and it also seems to be able to detect the existing db driver on the server automatically (Edit: I’ve never used this feature of automatic detection but I might be wrong because it seems intended for db version detection only). Of course, you can make your own abstraction layer if you think it worthwhile (but I think DBAL is pretty good and fast and I would recommend it for those not wanting to reinvent the wheel).
The only thing I can think of is supporting some servers where only PDO or mysqli is installed. However, nowadays both are installed almost everywhere.
Another advantage might be for developers being able to use the same db connection if one application is run from within another - but this also appears to be a rare case.
That’s my take as well.
I can see problems with using mysqli_ if the user wants to switch to a different database.
PDO wouldn’t have this problem except where MySQL specific features aren’t supported by the other database.
Of course if the uses might be messing with the source code, I suppose some might have a preference for mysqli_ over PDO but if they’re messing with the code perhaps it’s time for them to get more comfortable using PDO anyway?
I guess a lot depends on how advanced the “advanced” users are.
IMHO using OOP isn’t that much of a learning curve, Though writing OOP might be (especially good OOP)
Just the same, considering the number of posts from members still using mysql_ I get the impression that many are intimidated by even only seeing the word OOP even though mysqli_ allows for procedural style if so desired.
Using OOP for the database calls is just a matter of a minor syntax change -
Instead of:
mysqli_something($resource, '...')
you simply use
$resource->something('...')
where something is any of the mysqli calls and ‘…’ is whatever you need to pass to it. Basically replaces mysqli_ with → and moves the resource to the front.
mysqli does not support client side prepared statements for example.
PDO (using mysqlnd) does not support non-blocking asynchronous queries.
PDO also does it support all of the MySQL functionality.