Ask YC: Those using PHP, what do you use for talking to your database (MySQL)?
Say, for example, you're working with MySQL. Are you using the mysql/mysqli_* functions, PDO's drivers, ADOdb, etc? Or perhaps an ORM like Doctrine or Propel? I'm just curious to see what others are using, as there are a lot of choices out there.
(
35 comments
| 9 points
| on HN
)
I don't wrap the mysql_fetch* functions because that slows them down (lots of string copying). Plus, they don't need it.
I also like:
Very helpful for preventing sql injections, it's so short to type that I always remember to use it. And by including the quote marks right in it I don't have to go nuts escaping the quotes in the outer query.If you use mysqli you don't need this - use bind variables instead.
private function clean_string($string, $strip_tags = true) {
}Edit: You only need to use mysql_real_escape_string if you are inserting binary data or need to use database character sets. mysql_real_escape_string also requires a mysql connection and link identifier.
We use this in the Mantis project to define a list of sequential schema definitions, which then gets applied one-by-one to generate or upgrade an installation's database, and it works on MySQL, PostgreSQL, MS SQL, Oracle, and DB2.
At my current place, I inherited a badly-designed wrapper class around OO-style mysqli (and stored procedures); the mysqli part seems to be pretty good.
http://devzone.zend.com/node/view/id/686
Prior, used a custom object wrapper around the mysql functions — and it would be really simple change to swap out a different DB platform as all DB calls are abstracted and located in one class file...
But am considering switching to Zend or CodeIgniter.
DB_DataObject is working fine for now (it's lightweight, and I had it working pretty quickly). Saves time without getting in the way.
Anyone else using Propel?
Propel has been a bit of a kludge at times, but it does a decent job. We're experimenting with 1.3 and Doctrine to evaluate our migration path however.
Anything that's hit you as particularly inefficient?
I would say it does depend on the size of the project, so far I've only written small/medium projects built from scratch. I haven't had any reason to regret that decision yet, the code is simple, easy to read, maintain, and optimize.
Same is true of databases. There are things that people don't even think about if they've only ever used one database. What's your DB's lock escalation strategy? Are cursors cheap or expensive? What kinds of indexes don't you have, and could any of them ever be useful? And so on...
My comment was based on my own experience having to move a PHP app from SQL Server to Oracle. And it was just about the last app I'd ever expect to have to move from one database to another.
I'm not sure why my original comment was modded down because it's just plain common sense. With all the libraries out there for abstracting database access and ORMs you're just begging for trouble if you still use the database specific stuff. Unfortunately, in my case none of that stuff was available yet.
Still, I like using an abstraction layer because they tend to hide the quirks of the particular database - I use MySQL, Postgres and SQLite all fairly often, and by using PHP's PDO I can not worry about the differences as much as if I were using the database-specific drivers.
Even so, I don't think such a move would be that painful. I've taken great care to isolate all SQL queries from the rest of the code, and to keep them all in one place. I also have queries which I've had to optimize for the specific database I'm using (MySQL). I doubt an out-of-the-box DAL would be able to help with those in the current incarnation, or eliminate the need to rewrite them if the database changes.
The biggest challenge I can foresee is if they grow to the point where I need multiple databases in order to scale. I don't have enough experience yet to know whether existing frameworks would help in such a scenario, or just get in the way. But changing from MySQL to PostgreSQL? I'm fairly confident I can live with that.
But first and foremost, they're small-to-medium projects (in terms of code size), with relatively simple data models, and a single developer. I wouldn't advocate this approach for large projects.
Moving to something like Oracle or ODBC is a major task. You end up having to rewrite quite a bit of code even if you've isolated your queries. I had to learn this the hard way through experience and thought I'd share so someone else could avoid the hassle. The app that I had to move was very small and about the last thing I thought I'd have to spend much time maintaining. Six months later I was wasting half a day rewriting database code. Not sure why I would get modded down for this.
Their version of Active Record isn't the same as those from other frameworks, but we use the built-in one from Code Igniter. There's another floating around that's closer to the RoR style.
its excellent, and the framework itself is pretty snappy.