向 Zend_Auth_Adapter_DbTable 提供的参数未能生成有效的 sql 语句
我有以下异常捕获异常:向 Zend_Auth_Adapter_DbTable 提供的参数未能生成有效的 sql 语句,请检查表和列名称的有效性。
我已经用谷歌搜索并一遍又一遍地检查我的代码,但我还没有找到解决办法。表名和列名都是正确的。
导致此问题的代码部分是 $result = $auth->authenticate($authAdapter);
。事实上,整个控制器代码如下:
class AuthenticationController extends Zend_Controller_Action
{
public function init()
{
$uri = $this->_request->getPathInfo();
$activenav = $this->view->navigation()->findByUri($uri);
$activenav->active = true;
}
public function indexAction()
{
// action body
}
public function loginAction()
{
if(Zend_Auth::getInstance()->hasIdentity())
{
$this->_redirect('index/index');
}
$request = $this->getRequest();
$form = new Application_Form_LoginForm();
if($request->isPost())
{
if($form->isValid($this->_request->getPost()))
{
$authAdapter = $this->getAuthAdapter();
$username = $form->getValue('username');
$password = $form->getValue('password');
$authAdapter->setIdentity($username)
->setCredential($password);
$auth = Zend_Auth::getInstance();
try
{
$result = $auth->authenticate($authAdapter);
}
catch (Exception $e)
{
echo 'Caught exception: ', $e->getMessage(), "\n";
}
if ($result->isValid())
{
$identity = $authAdapter->getResultRowObject();
$authstorage = $auth->getStorage();
$authstorage->write($identity);
$this->_redirect('index/index');
}
else
{
$this->view->errorMessage = "User name or password is wrong";
}
}
}
$this->view->form = $form;
}
public function logoutAction()
{
Zend_Auth::getInstance()->clearIdentity();
$this->_redirect('index/index');
}
private function getAuthAdapter()
{
$authAdapter = new Zend_Auth_Adapter_DbTable(Zend_Db_Table::getDefaultAdapter());
$authAdapter->setTableName('users')
->setIdentityColumn('username')
->setCredentialColumn('password')
->setCredentialTreatment('SHA1(CONCAT(?,salt))');
return $authAdapter;
}
}
我已经被困在这个问题上好几天了,它让我发疯。 顺便说一句,我如何回显正在生成的实际 sql? 谢谢大家
I have the following exception Caught exception: The supplied parameters to Zend_Auth_Adapter_DbTable failed to produce a valid sql statement, please check table and column names for validity.
I have googled and checked my code over and over again but I have not found a solution. The table and column names are all correct.
The section of code that is causing this problem is $result = $auth->authenticate($authAdapter);
. Infact the whole controller code is found below:
class AuthenticationController extends Zend_Controller_Action
{
public function init()
{
$uri = $this->_request->getPathInfo();
$activenav = $this->view->navigation()->findByUri($uri);
$activenav->active = true;
}
public function indexAction()
{
// action body
}
public function loginAction()
{
if(Zend_Auth::getInstance()->hasIdentity())
{
$this->_redirect('index/index');
}
$request = $this->getRequest();
$form = new Application_Form_LoginForm();
if($request->isPost())
{
if($form->isValid($this->_request->getPost()))
{
$authAdapter = $this->getAuthAdapter();
$username = $form->getValue('username');
$password = $form->getValue('password');
$authAdapter->setIdentity($username)
->setCredential($password);
$auth = Zend_Auth::getInstance();
try
{
$result = $auth->authenticate($authAdapter);
}
catch (Exception $e)
{
echo 'Caught exception: ', $e->getMessage(), "\n";
}
if ($result->isValid())
{
$identity = $authAdapter->getResultRowObject();
$authstorage = $auth->getStorage();
$authstorage->write($identity);
$this->_redirect('index/index');
}
else
{
$this->view->errorMessage = "User name or password is wrong";
}
}
}
$this->view->form = $form;
}
public function logoutAction()
{
Zend_Auth::getInstance()->clearIdentity();
$this->_redirect('index/index');
}
private function getAuthAdapter()
{
$authAdapter = new Zend_Auth_Adapter_DbTable(Zend_Db_Table::getDefaultAdapter());
$authAdapter->setTableName('users')
->setIdentityColumn('username')
->setCredentialColumn('password')
->setCredentialTreatment('SHA1(CONCAT(?,salt))');
return $authAdapter;
}
}
I have been stuck on this for a couple of days now and its driving me nuts.
BTW how can I echo out the actual sql that is being generated?
Thanks all
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
如上所述,这取决于 MySQL 版本。以下 MySQL 文档版本 5.5:
“如果应用程序存储来自返回十六进制数字字符串的函数(例如 MD5() 或 SHA1())的值,则可以通过使用 UNHEX 将十六进制表示形式转换为二进制来获得更高效的存储和比较() 并将结果存储在 BINARY(N) 列中,每对十六进制数字需要一个二进制形式的字节,因此 N 的值取决于十六进制字符串的长度,对于 MD5() 值,N 为 16。 SHA1() 值是 20。”
因此,您可以执行以下操作,而不是降级MySQL版本:
它在我的情况下效果很好。
编辑 -
如果您的密码盐也存储在二进制列中(例如,如果它也是通过 SHA1 函数生成的十六进制字符串),那么 Zend_Auth_Adapter_DbTable 的最后一个参数应该是:
'UNHEX(SHA1(CONCAT(?, LOWER(HEX(盐)))))'
因此,在与密码连接之前,我们将盐转换回小写的十六进制字符串。 HEX() 以大写形式返回盐,因此如果在使用 UNHEX() 存储盐之前盐最初是大写的,则可以省略 LOWER() 调用。
It depends on MySQL version as described above. Following MySQL Documentations for version 5.5:
"If an application stores values from a function such as MD5() or SHA1() that returns a string of hex digits, more efficient storage and comparisons can be obtained by converting the hex representation to binary using UNHEX() and storing the result in a BINARY(N) column. Each pair of hex digits requires one byte in binary form, so the value of N depends on the length of the hex string. N is 16 for an MD5() value and 20 for a SHA1() value."
So, instead of downgrading MySQL version, you may do as follows:
It works well in my case.
Edit --
If your password salt is also stored in a binary column (e.g. if it too was a hex string generated through the SHA1 function) then the last parameter of the Zend_Auth_Adapter_DbTable should be:
'UNHEX(SHA1(CONCAT(?, LOWER(HEX(salt)))))'
So, we are converting the salt back to a lowercase hex string before concatenating with the password. HEX() returns your salt in uppercase so you can just omit the LOWER() call if your salt was originally uppercase before you stored it using UNHEX().
好吧,首先我需要让所有人知道这个答案已经获得专利并根据其中一个许可证获得许可,这意味着您甚至无法阅读或提出类似的答案。(您知道我在开玩笑吧?)。好吧,言归正传……
三天后,我偶然发现了一个解决方案。这是一个奇怪的原因,但它解决了我的问题。所以事情不起作用,没有人回答我的问题,所以我买了这本新的 zend 书,只是为了分散自己对问题的注意力。另一个干扰是引导到 Linux 而不是 Windows(我是双引导,你知道)。
在 Linux 中,我决定为有问题的项目创建一个虚拟主机,然后尝试运行它。令我惊讶的是它运行没有问题。我能够登录。然后我看了一下 phpmyadmin,发现我的 mysql 版本是 5.1,而我的 widows 设置中的版本是 5.5。所以我想为什么不将windows中的mysql从5.5降级到5.1。
所以我这样做了,我的问题就消失了。我不知道 mysql 的人对此做了什么,但似乎 5.5 版可能存在 SHA1 问题。但不确定它是否适用于其他哈希函数。也许有人会证实这个怀疑?
Ok so first off I need to make all aware that this answer has been patented and licensed under one of those linceses that mean that you can't even read or come up with a similar answer.(you know I kidding right?). Ok ok to the point ....
After 3 days I stumbled upon a solution. A weird one of cause but it fixed my problem. So things were not working and no one had answered my question so I got hold of this new zend book that I bought to just try and distract myself from the problem. A further distraction was to boot into Linux instead of windows (I dual boot you know).
In Linux i just decided to create a virtual host for the problematic project and just try and run it. To my supprise it run without problems. I was able to login. Then i took a look at phpmyadmin and saw that I the mysql version is 5.1 where as the one on my widows setup is 5.5. So I thought why not downgrade the mysql in windows from 5.5 to 5.1.
So i did et viola my problem was gone. I don't know what the folks at mysql did to it but it seems ver 5.5 may have issues with SHA1. Not sure if it applies to other hash functions though. May be someone will comfirm this suspicion?
确保您的 unicode 'utf-8' 设置与您的 MySQL 服务器所期望的相匹配。
换句话说,如果您的服务器未配置为“utf-8”(默认情况下),请不要在 application.ini 文件中将字符集设置为“utf-8”。 A...
从 ZF 发送到 MySQL,导致错误。
删除 application.ini 中的“utf-8”字符集为我解决了这个问题。
Make sure you unicode 'utf-8' settings match what your MySQL Server is expecting.
In other words, don't set the charset as 'utf-8' in your application.ini file if your server is not configured for that ( as is the default ). A...
is sent to MySQL from ZF which causes the error.
Removing the 'utf-8' charset in the application.ini solved this for me.
我解决了这个问题如下:
1)vim /etc/php.ini
2)然后我安装了 php 其他软件包,如下所示,它可以工作,然后将那些缺少的软件包添加到主服务器,它可以工作。
I resolved this as below:
1) vim /etc/php.ini
2) Then i installed php other packages such as below where it works, and then added those missing packages to the main server, and it works.
正如 Kervin 已经回答的那样,出现此错误是因为 php 和 mysql 之间的排序规则不匹配。
您可以按照上述解决方案中的建议停止使用 utf8,也可以更改数据库表以使用 utf8。(仅在我删除表内的数据并重新插入它们后,它才对我起作用。)
As Kervin has already answered, this error appears because of collation mismatch between php and mysql.
You can either stop using utf8 as suggested in the above solution or you can change the database table to use utf8.(It worked for me only after I deleted the data inside the table and reinserted them.)
我有同样的错误,并发现它具有误导性。就我而言,事实证明我连接到了错误的数据库,原因不值得解释。您想要做的是获取导致 Zend_Auth_Adapter_DbTable 抛出您提到的异常的先前异常。以下是我完成此任务的方法:
所以最终,答案并不完全正确:
这实际上可能是很多问题。最好的办法是让 MySQL 通过获取之前的异常来告诉您。也许答案与字符编码有关。就我而言,事实并非如此。
I had the same error and found it to be misleading. In my case, it turned out that I was connect to the wrong database for reasons not worth explaining. What you want to do is get the previous exception that caused Zend_Auth_Adapter_DbTable to throw the exception you mentioned. Below is how I accomplished this:
So in the end, the answer isn't exactly:
It could really be any number of issues. Best bet is to let MySQL tell you by getting the previous exception. Maybe the answer will be related to character encoding. In my case it wasn't.
请检查这个
这对我有用。
Please check this
It's works for me.