cakephp和PDO限制错误

时间:2013-04-21 17:28:19

标签: mysql cakephp pdo

大家好我在cakephp中开发了一个站点,我在pdo中有一个查询,我想插入一个限制值。 我尝试过这种模式:

$max_result = 10;
$search = "test";
$product_alias = $this->ProductAlias->query(
'SELECT DISTINCT * 
   FROM product_aliases 
   WHERE product_aliases.alias 
   LIKE :search LIMIT :limit_search'
 ,array('search' => '%'.$search.'%','limit_search' => intval(trim($max_result)))
);

我也试过了:

...
WHERE product_aliases.alias 
  LIKE :search 
  LIMIT :limit_search'
,array('search' => '%'.$search.'%','limit_search' => intval($max_result)));

...

WHERE product_aliases.alias 
  LIKE :search 
  LIMIT :limit_search'
,array('search' => '%'.$search.'%','limit_search' => $max_result));

但总是给我这个错误: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '10' at line 1

我已经看到有绑定但我不知道如何应用于这种情况。 任何解决方案?

2 个答案:

答案 0 :(得分:1)

不确定CakePHP API,但你可以试试这个:

$product_alias = $this->ProductAlias->prepare('SELECT DISTINCT * 
    FROM product_aliases 
    WHERE product_aliases.alias LIKE :search 
    LIMIT :limit_search');
$product_alias->bindParam( 'search', '%'.$search.'%', PDO::PARAM_STR );
$product_alias->bindParam( 'limit_search', (int) intval(trim($max_result)), PDO::PARAM_INT );

在检查CakePHP的文档时,他们确实提供了PDOStatementhttp://api.cakephp.org/2.2/class-PDOStatement.html

答案 1 :(得分:0)

必须做这一切。 (你不应该这样做)。手动编写所有查询基本上会使整个框架无效。

阅读手册的这一部分Retrieving your data

要在CakePHP中检索数据,请使用此选项;

$product_alias  = $this->ProductAlias->find('all', array(
    'conditions' => array(
        'ProductAlias.alias LIKE' => '%' . $search . '%',
    ),
    'limit' => $max_result
));