Update: Thanks for all the help guys, I’ve decided to try and write my own PHP class for handling MySQL requests.
I’m faced with a problem right now when I’m trying to make the switch from mysql_ to mysqli_ functions, in that I have literally tens of thousands of lines of PHP/MySQL code and queries scattered across hundreds of files, and they’re all using functions such as mysql_query, mysql_num_rows, mysql_fetch_assoc, etc. Going through and changing these line by line (even with find/replace) to mysqli_ functions with prepared statements and all is pain enough, but what if in the future PHP.net decides to release something else that will deprecate the mysqli_ functions?
So right now I’m considering perhaps writing my own MySQL library (is that what you would call it?) that calls the mysqli_ functions and including it in all my files. This way, if I needed to change something else in the future, theoretically I could change the function in my “library” once and changes would be reflected on all pages, instead of manually going through and changing everything by hand.
Would this be a good idea? I found something online called MeekroDB, but first of all I’m not sure if it’s what I’m looking for and second of all they’re charging $50 to use this commercially (which I want to do.) Are there other free solutions, or would I just be wasting my time?
Any comments/feedback is appreciated 🙂
Edit: In case this wasn’t clear, it’s not as if I’m making a connection to my database in every file that I’m making queries to database. I have a functions.php file that I use to connect to the MySQL database and I include that file in all my other files. My question is more related to functions such as mysql_query, mysql_num_rows, and mysql_fetch_assoc.
From looking at your referenced link to
MeekroDB, I don’t think what you’re writing is a MySQL API like themysql_*/mysqli_*methods.What you’re in fact looking to write is a Database Abstraction Layer, despite it being specific to MySQL.
The fact is, unless you’re going to write your own underlying module to communicate with MySQL (of course, you could also do this with difficulty in PHP), you’re probably going to end up using the
mysqli_methods as a backing anyway.There are many of these layers available, but I’d honestly consider using PDO over writing your own, or using
mysqli_*if you’re worried about functions being deprecated. PDO uses its own MySQL driver, and so will be updated if it needs to be. It also supports all what you’d require including easy-to-use transactions, prepared statements and whatnot.If you wanted to create your own easy-access methods such as
DB::Query(..), you can do this by wrapping PDO easy enough.Overall, though, I don’t see how this is going to help with organising your code. You’re probably going to want to look into something like an ORM (Object Relationship Mapper) to your DB where you can define your own classes that map straight into tables, which should alleviate the need of running queries almost entirely, apart from edge cases. The most comprehensive ORM available would probably be Doctrine, although you can quite easily write your own by again, wrapping PDO.
Just as an example of what you could achieve by using an ORM, here’s a snippet of code to demonstrate: