/**
* @param string|string[]$identifiers
* @param string $tableName
* @param string $dbName
* @return string[]|string
*/
static public function quoteIdentifiers($identifiers, $tableName='', $dbName=''){
if(is_array($identifiers)){
$result = array();
foreach($identifiers as $identifier){
$result[] = self::quoteIdentifiers($identifier, $tableName, $dbName);
}
}else{
$result = '`'.str_replace('`','``',$identifiers).'`'; // escape backtick with backtick
if($tableName){
$result = '`'.$tableName.'`.'.$result;
}
if($dbName){
$result = '`'.$dbName.'`.'.$result;
}
}
return $result;
}
использование:
$columns = quoteIdentifiers(array('my col1', 'my col2'), 'table');
$sql = 'SELECT '.join(',', $columns);
$sql=.' FROM '.quoteIdentifiers('table');
=> SELECT `table`.`my col1`,`table`.`my col2` FROM `table`
Bonus (смарт-кавычки значения, без подключения требуется!):
/**
* quote a value or values
* @param string|string[]|int|int[] $value
* @return string[]|string
*/
static public function quoteValues($value) {
if(is_array($value)){
$result = array_map(__METHOD__, $value);
}elseif($value===true){
$result = 'TRUE';
}elseif($value===false){
$result = 'FALSE';
}elseif($value===null){
$result = 'NULL';
}elseif(is_int($value) OR is_float($value) OR (is_string($value) AND $value===strval($value*1))){
$result = strval($value); // no quote needed
}else{
$result = "'".str_replace(
array('\\', "\0", "\n", "\r", "'", '"', "\x1a"),
array('\\\\', '\\0', '\\n', '\\r', "\\'", '\\"', '\\Z'),
strval($value)). "'";
}
return $result;
}
Принято ответ на вопрос "Могу ли я с видом что-то очевидное?" - похоже, нет прямого эквивалента в PDO или ADOdb. – benizi