dq_609586475 2011-01-01 22:02
浏览 27
已采纳

为什么我的Doctrine DBAL查询在引用时没有返回结果?

I'm using the Doctrine DataBase Abstraction Layer (DBAL) to perform some queries. For some reason, when I quote a parameter before passing it to the query, I get back no rows. When I pass it unquoted, it works fine.

Here's the relevant snippet of code I'm using:

  public function get($game)
  {
    load::helper('doctrinehelper');
    $conn = doctrinehelper::getconnection();
    $statement = $conn->prepare('SELECT games.id as id, games.name as name, games.link_url, games.link_text, services.name as service_name, image_url 
                                 FROM games, services 
                                 WHERE games.name = ? 
                                 AND services.key = games.service_key');
    $quotedGame = $conn->quote($game);

    load::helper('loghelper');
    $logger = loghelper::getLogger();
    $logger->debug("Quoted Game: $quotedGame");
    $logger->debug("Unquoted Game: $game");

    $statement->execute(array($quotedGame));
    $resultsArray = $statement->fetchAll();
    $logger->debug("Number of rows returned: " . count($resultsArray));  
    return $resultsArray;
  }

Here's what the log shows:

01/01/11 17:00:13,269 [2112] DEBUG root - Quoted Game: 'Diablo II Lord of Destruction'
01/01/11 17:00:13,269 [2112] DEBUG root - Unquoted Game: Diablo II Lord of Destruction
01/01/11 17:00:13,270 [2112] DEBUG root - Number of rows returned: 0

If I change this line:

$statement->execute(array($quotedGame));

to this:

$statement->execute(array($game));

I get this in the log:

01/01/11 16:51:42,934 [2112] DEBUG root - Quoted Game: 'Diablo II Lord of Destruction'
01/01/11 16:51:42,935 [2112] DEBUG root - Unquoted Game: Diablo II Lord of Destruction
01/01/11 16:51:42,936 [2112] DEBUG root - Number of rows returned: 1

Have I fat-fingered something?

  • 写回答

1条回答 默认 最新

  • dongyunwei8596 2011-01-02 12:51
    关注

    Actually, this is an architectural thing (IMO). Doctrine is based on PHP's PDO. PDO works with Prepared Statements so there is no need to quote, use mysql_real_escapestring or anything. The ORM has has as a base design goal in it's very core and with PDO, it does this fairly secure.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 求快手直播间榜单匿名采集ID用户名简单能学会的
  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历