donglie1994 2013-06-18 21:55
浏览 36

PDO ::引用nulls,bools等等

PDO::quote always seems to slap on two single quotes regardless of the type of value I pass it, or the parameter type I set.

e.g.,

$x = null;
echo $pdo->quote($x,PDO::PARAM_NULL); // ''

Thus I've extended the PDO class with my own function,

public function quote($value, $parameter_type=PDO::PARAM_STR) {
    if(is_null($value)) return 'NULL';
    elseif(is_bool($value)) return $value ? 'TRUE' : 'FALSE';
    elseif(is_int($value)||is_float($value)) return $value;
    return parent::quote($value, $parameter_type);
}

Have I missed any cases? Is there any harm in doing this?

Do the different parameter types ever do anything?

  • 写回答

1条回答 默认 最新

  • douhao9203 2013-06-18 23:08
    关注

    Per the documentation, the results of a call to quote() are dependent on the PDO driver, because different databases escape strings in different ways. So some drivers may require quotes in different places than others. But really, if you know you don't need quotes, then you shouldn't be calling quote().

    As for your function, it depends on what you're trying to do. If you're trying to build a database-agnostic data access layer, then your function will break for some databases. This is because not all DBs have boolean types, e.g. there is no such value as TRUE in SQLite.

    But really, it would be better to just use prepared statements and not touch quote() at all. Even the documentation for quote says so, after all.

    评论

报告相同问题?

悬赏问题

  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据