dpn68721 2011-07-29 12:30
浏览 5
已采纳

PDO:SQL中的引号

I'm seeing some weirdness when I try to run a query using PDO. The following code shouldn't return results, but it does:

$safe_path = $this->_databaseConnection->quote($unsafe_path);
$sql = "SELECT * FROM routes WHERE path=$safe_path LIMIT 1";
$statement_handle = $this->_databaseConnection->query($sql);
var_dump($statement_handle->fetchAll());

I'm confused because there aren't single quotes around the $safe_path variable as there would be if I were using the mysqli extension - but it's working. If I enclose $safe_path in quotes, no results are returned. This seems strange to me.

  • 写回答

3条回答 默认 最新

  • dongmubi4444 2011-07-29 12:32
    关注

    You are already quoting the $safe_path variable with your first line in the sample:

    $safe_path = $this->_databaseConnection->quote($unsafe_path);
    

    That is why it works as it stands. If you attempt to add quotes yourself in the:

    $sql = "SELECT * FROM routes WHERE path='$safe_path' LIMIT 1";
    

    line then you would be doubling up the quotes and therefore breaking the SQL query.

    Please see the manual page for quote() for more information:

    PDO::quote() places quotes around the input string (if required) and escapes special characters within the input string, using a quoting style appropriate to the underlying driver.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 Android Studio中如何把H5逻辑放在Assets 文件夹中以实现将h5代码打包为apk
  • ¥15 使用小程序wx.createWebAudioContext()开发节拍器
  • ¥15 关于#爬虫#的问题:请问HMDB代谢物爬虫的那个工具可以提供一下吗
  • ¥15 vue3+electron打包获取本地视频属性,文件夹里面有ffprobe.exe 文件还会报错这是什么原因呢?
  • ¥20 用51单片机控制急停。
  • ¥15 孟德尔随机化结果不一致
  • ¥15 在使用pyecharts时出现问题
  • ¥50 怎么判断同步时序逻辑电路和异步时序逻辑电路
  • ¥15 差动电流二次谐波的含量Matlab计算
  • ¥15 Can/caned 总线错误问题,错误显示控制器要发1,结果总线检测到0