我正在尝试以这种方式使用 Zend Framework 2 Db Adapter 执行查询:
$db = new Adapter(array(
'driver' => 'oci8',
'username' => 'myusername',
'password' => 'mypassword',
'connection_string' => 'myconnectionstring',
'charset' => 'utf8',
));
$sql = 'SELECT lastname FROM accounts WHERE username = ?';
$statement = $db->createStatement($sql, array('luca'));
$result = $statement->execute();
但是 PHP 给了我这个信息:
[23-May-2013 10:23:30 Europe/Rome] PHP Fatal error: Uncaught
exception 'Zend\Db\Adapter\Exception\RuntimeException'
with message 'ORA-00911: invalid character' in
/var/www/zend/server/library/ZF2-.2.0/library/Zend/Db/Adapter/Driver/Oci8/Statement.php:261
Stack trace:
#0 /var/www/html/secure/zendAuth2.php(92):
Zend\Db\Adapter\Driver\Oci8\Statement->execute()
#1 {main}
thrown in /var/www/zend/server/library/ZF2-2.2.0/library/Zend/Db/Adapter/Driver/Oci8/Statement.php on line 261
SQL 中的无效字符似乎是“?”。我究竟做错了什么?阅读框架手册似乎是正确的(http://framework.zend.com/manual/2.2/en/modules/zend.db.adapter.html#query-preparation-through-zend-db-adapter-adapter-查询),但我仍然收到此错误。如果相反我使用
$sql = 'SELECT lastname FROM accounts WHERE username = :username';
$statement = $db->createStatement($sql, array(':username' => 'luca'));
$result = $statement->execute();
它工作正常。
谢谢您的帮助。