0

我有以下 PDO:

// Connect to PDO / $this->link = PDO
$this->statement = $this->link->prepare("
    SELECT p.*, pm.*
      FROM dev_permissions p, dev_permissions_menu pm
     WHERE pm.id = p.permission_menu_id
       AND pm.module = 'account'
       AND pm.controller IN('login', '*')
       AND pm.action IN('index', '*')
");
// Execute and verify number of rows

哪个有效,从 . 返回一行$this->statement->rowCount()

但是,尝试绑定参数不能按预期工作:

// Connect to PDO / $this->link = PDO
$this->statement = $this->link->prepare("
    SELECT p.*, pm.*
      FROM dev_permissions p, dev_permissions_menu pm
     WHERE pm.id = p.permission_menu_id
       AND pm.module = :module
       AND pm.controller IN(:controller, '*')
       AND pm.action IN(:action, '*')
",

$this->statement->bindParam(':module', 'account', PDO::PARAM_STR);
$this->statement->bindParam(':controller', 'login', PDO::PARAM_STR);
$this->statement->bindParam(':action', 'index', PDO::PARAM_STR);
// Execute and verify number of rows

谢谢!

更新:

更改PDO::FETCH_ASSOCPDO::PARAM_STR因为 CTRL + C/CTRL + V 问题,并#__反映正确的数据库前缀。

使用以下代码进行测试:

$pdo = new \PDO('mysql:dbname=database;host=localhost', 'user', 'password');
$statement = $pdo->prepare("
    SELECT p.*, pm.*
      FROM dev_permissions p, dev_permissions_menu pm
     WHERE pm.id = p.permission_menu_id
       AND pm.module = :module
       AND pm.controller IN(:controller, '*')
       AND pm.action IN(:action, '*')
");
$statement->bindParam(':module', $request['module'], \PDO::PARAM_STR);
$statement->bindParam(':controller', $request['controller'], \PDO::PARAM_STR);
$statement->bindParam(':action', $request['action'], \PDO::PARAM_STR);
$statement->execute();

new \Core\Debug($statement->fetchAll());

一切正常。我找不到其他方式的问题..

4

1 回答 1

1

在您的示例中,变量名称有点令人困惑......prepare是 PDO 对象的一种方法,它返回一个 PDOStatement 对象......尝试:

$sth = $this->statement->prepare("
    SELECT p.*, pm.*
      FROM #__permissions p, #__permissions_menu pm
    WHERE pm.id = p.permission_menu_id
     AND pm.module = :module
     AND pm.controller IN(:controller, '*')
     AND pm.action IN(:action, '*')
",

$sth->bindParam(':module', 'account', PDO::PARAM_STR);
$sth->bindParam(':controller', 'login', PDO::PARAM_STR);
$sth->bindParam(':action', 'index', PDO::PARAM_STR);

//execute the prepared statement
$sth->execute();

作为注释...rowCount在选择语句上不会发出您的查询...它COUNT(*)使用与您的选择相同的信息执行查询...请参见http://www.php.net/上的示例 #2手册/en/pdostatement.rowcount.php

于 2012-06-10T01:36:32.007 回答