我有以下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()
返回一行。
但是,尝试绑定params并不能按预期工作:
// 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_ASSOC
更改为PDO::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());
一切正常。我用其他方式找不到问题..
答案 0 :(得分: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
在select语句中不会向您发出查询...它会使用与您选择的信息相同的信息执行COUNT(*)
查询。 ..见http://www.php.net/manual/en/pdostatement.rowcount.php