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.

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

报告相同问题?

悬赏问题

  • ¥15 基于作物生长模型下,有限水资源的最大化粮食产量的资源优化模型建立
  • ¥20 关于变压器的具体案例分析
  • ¥15 生成的QRCode圖片加上下載按鈕
  • ¥15 板材切割优化算法,数学建模,python,lingo
  • ¥15 科来模拟ARP欺骗困惑求解
  • ¥100 iOS开发关于快捷指令截屏后如何将截屏(或从截屏中提取出的文本)回传给本应用并打开指定页面
  • ¥15 unity连接Sqlserver
  • ¥15 图中这种约束条件lingo该怎么表示出来
  • ¥15 VSCode里的Prettier如何实现等式赋值后的对齐效果?
  • ¥20 keepalive配置业务服务双机单活的方法。业务服务一定是要双机单活的方式