tags:

views:

43

answers:

1

At my current job, the standard practice has been to use mysql_query() and friends directly. When constructing large queries, since our standard deployment is on MySQL, table names just get inserted and surrounded by backticks (partial, fake example):

$sql .= "from `$tablename`";

I'm trying to get away from MySQL-ism's, and as part of that, moving toward PDO and/or ADOdb. But, I'm more familiar with Perl than PHP, and I was surprised I couldn't easily find the equivalent of DBI's quote_identifier, which takes either a sole table name, or the whole set of identifying information (catalog, schema, table). Am I overlooking something obvious?

A: 

Unfortunately there's nothing in PHP land that compares to the awesome of DBI. PDO is a worthwhile starting point.

Your best bet is not going to be trying to create DB-specific identifier quoting, but to tell the database to follow the standards. Turn on ANSI quotes, meaning you can use double quotes to identify columns and table names. This standard-specified format is accepted by most other databases, including Postgres and SQLite. Some (like MSSQL) also have similar settings to switch to double quotes from a non-standard default.

As a caveat, it means that you'll always have to use single quotes when quoting a string literal value instead of doubles. Also, most identifiers do not have to be quoted, unless they are a SQL keyword or are otherwise reserved by the database.

There are lots of other steps required to make SQL portable. You might want to look at going one step further and actually using an SQL builder or an ORM.

Charles
Well-aware that SQL-portability is far more involved than getting table quoting correct. Was really just hoping to not have to write something quote_identifier-like. Way too late in the game to turn on ANSI quoting considering the current codebase. I'll accept your answer (that no, there's no obvious equivalent) unless someone else steps in. Thanks.
benizi