Zend_Auth:不工作,getDbSelect() 为空
我的身份验证失败,当我尝试 getDbSelect() 时,我什么也没得到......我做错了什么?
class AuthController extends Zend_Controller_Action { protected $auth; public function init() { $db = $this->getInvokeArg("bootstrap")->getResource("db"); $this->auth = new Zend_Auth_Adapter_DbTable($db, 'users', 'username', 'password', 'md5(?)'); } public function loginAction() { if (isset($_POST["username"])) { $result = $this->auth ->setIdentity($this->getRequest()->getParam('username')) ->setCredential($this->getRequest()->getParam('password')); echo " > " . $this->auth->getDbSelect();die(); $result = $this->auth ->setIdentity($this->getRequest()->getParam('username')) ->setCredential($this->getRequest()->getParam('password')) ->authenticate(); if ($result->isValid()) { $this->_redirect("postLogin"); } } }
更新1:
我也尝试过不使用MVC而使用我更熟悉的MySQL:
// setup auto loading require_once 'Zend/Loader/Autoloader.php'; Zend_Loader_Autoloader::getInstance(); // setup db connection $db = new Zend_Db_Adapter_Pdo_Mysql(array( 'host' => '127.0.0.1', 'username' => 'root', 'password' => '', 'dbname' => 'learnZendAuth' )); // create authentication adapter $auth = new Zend_Auth_Adapter_DbTable($db, 'users', 'username', 'password', 'md5(?)'); // do login if request is posted if (isset($_POST) && !empty($_POST)) { // do authencation $result = $auth->setIdentity($_POST['username']) ->setCredential($_POST['password']) ->authenticate(); // check authentication valid if ($result->isValid()) { // login success echo "successfully logged in as: " . $result->getIdentity(); } else { // login fail echo ""; print_r($result->getMessages()); echo ""; } } else { // request not posted // see if already logged in if (Zend_Auth::getInstance()->hasIdentity()) { echo "already logged in as: " . Zend_Auth::getInstance()->getIdentity(); } }
我明白了
致命错误:未捕获的异常 'Zend_Auth_Adapter_Exception' 与 message '提供的参数 Zend_Auth_Adapter_DbTable 失败 请生成有效的sql语句 检查表名和列名 有效性。'在 C:\Program Files 中 (x86)\Zend\ZendServer\share\ZendFramework\library\Zend\Auth\Adapter\DbTable.php:414 堆栈跟踪:#0 C:\Program Files (x86)\Zend\ZendServer\share\ZendFramework\library\Zend\Auth\Adapter\DbTable.php(306): Zend_Auth_Adapter_DbTable->_authenticateQuerySelect(Object(Zend_Db_Select))
1 C:\Program Files (x86)\Zend\Apache2\htdocs\learnZf\index.php(23):
Zend_Auth_Adapter_DbTable->authenticate()
2 {main} 抛出在 C:\Program Files (x86)\Zend\ZendServer\share\ZendFramework\library\Zend\Auth\Adapter\DbTable.php
414行
my authentication fails and when i try to getDbSelect() i get nothing ... what did i do wrong?
class AuthController extends Zend_Controller_Action { protected $auth; public function init() { $db = $this->getInvokeArg("bootstrap")->getResource("db"); $this->auth = new Zend_Auth_Adapter_DbTable($db, 'users', 'username', 'password', 'md5(?)'); } public function loginAction() { if (isset($_POST["username"])) { $result = $this->auth ->setIdentity($this->getRequest()->getParam('username')) ->setCredential($this->getRequest()->getParam('password')); echo " > " . $this->auth->getDbSelect();die(); $result = $this->auth ->setIdentity($this->getRequest()->getParam('username')) ->setCredential($this->getRequest()->getParam('password')) ->authenticate(); if ($result->isValid()) { $this->_redirect("postLogin"); } } }
Update 1:
i also tried without MVC and with MySQL which i am more familiar with:
// setup auto loading require_once 'Zend/Loader/Autoloader.php'; Zend_Loader_Autoloader::getInstance(); // setup db connection $db = new Zend_Db_Adapter_Pdo_Mysql(array( 'host' => '127.0.0.1', 'username' => 'root', 'password' => '', 'dbname' => 'learnZendAuth' )); // create authentication adapter $auth = new Zend_Auth_Adapter_DbTable($db, 'users', 'username', 'password', 'md5(?)'); // do login if request is posted if (isset($_POST) && !empty($_POST)) { // do authencation $result = $auth->setIdentity($_POST['username']) ->setCredential($_POST['password']) ->authenticate(); // check authentication valid if ($result->isValid()) { // login success echo "successfully logged in as: " . $result->getIdentity(); } else { // login fail echo ""; print_r($result->getMessages()); echo "";
}
} else {
// request not posted
// see if already logged in
if (Zend_Auth::getInstance()->hasIdentity()) {
echo "already logged in as: " . Zend_Auth::getInstance()->getIdentity();
}
}i get
Fatal error: Uncaught exception
'Zend_Auth_Adapter_Exception' with
message 'The supplied parameters to
Zend_Auth_Adapter_DbTable failed to
produce a valid sql statement, please
check table and column names for
validity.' in C:\Program Files
(x86)\Zend\ZendServer\share\ZendFramework\library\Zend\Auth\Adapter\DbTable.php:414
Stack trace: #0 C:\Program Files
(x86)\Zend\ZendServer\share\ZendFramework\library\Zend\Auth\Adapter\DbTable.php(306):
Zend_Auth_Adapter_DbTable->_authenticateQuerySelect(Object(Zend_Db_Select))1 C:\Program Files (x86)\Zend\Apache2\htdocs\learnZf\index.php(23):
Zend_Auth_Adapter_DbTable->authenticate()
2 {main} thrown in C:\Program Files (x86)\Zend\ZendServer\share\ZendFramework\library\Zend\Auth\Adapter\DbTable.php
on line 414
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试将 getDbSelect 移动到 is valid 语句中,
我通常也用来
获取用户信息,从未见过这个 getDbSelect 方法
try and move the getDbSelect in the is valid statement,
also i normally use
to get the user info, never saw this getDbSelect method
哦,我发现我做错了什么...
第一次尝试,我使用了Sqlite,它不支持md5(?)处理
第二次尝试,我指定了错误的列名...名称与用户名...
oh i found out what i did wrong ...
the 1st try, i used Sqlite, and it does not support md5(?) treatment
the 2nd try, i specified wrong column names ... name vs username ...