Vegeterian
2015-11-14 19:36
有个地方有点问题,大约100行代码那块。那个$fields整个字符串里没找到反引号时,再处理加入反引号。但是有可能是他的sql中,关键词的字段没加反引号,而非关键词的加了反引号。这样的话,还是逃脱了走加反引号的方法哈哈。 好比$sql = 'select `hi`, status from table '; 这个$fields = '`hi`,status';这个status就没加反引号。。
public static function parseFilds($fields)
{
if(!is_array($fields))
{
$fields = explode(',',$fields);
}else{
$fieldsStr = "*";
}
array_walk($fields,array('PdoMysql','addSpecilChar'));
$fieldsStr=implode(',',$fields);
return $fieldsStr;
}
/**
* sql条件格式化 abc->`abc`,tab.cloum->`tab`.`cloum`
* @param [type] &$value [description]
*/
public function addSpecilChar(&$value)
{
if($value === "*") return $value;
preg_match('/^\`(.*)\`$/',$value,$preg);
if(count($preg)>0){return $value;};
preg_match('/(.*)\.$/',$value,$preg);
if(count($preg)>0){return '`'.$value.'`';};
preg_match('/^\.(.*)/',$value,$preg);
if(count($preg)>0){return '`'.$value.'`';};
$arr = explode('.',$value);
$res = Array();
foreach ($arr as $key => $v) {
if($v=='') break;
preg_match('/^\`(.*)\`$/', $v,$preg);
if(count($preg)>0){
$res[] = $v;
}else{
$res[] = '`'.$v.'`';
}
}
$value = implode('.',$res);
$res = null;
$arr = null;
$preg = null;
return $value;
}我把他的升级了一下
PDO—数据库抽象层
30046 学习 · 421 问题
相似问题