我在codeigniter中使用sha2哈希函数编写了一个查询,但结果查询没有正确形成。
这是我在模型中的代码:
>>> len(fib.__closure__[1].cell_contents)
501
以上是我收到的错误(由于查询格式错误):
public function validate_user($data = NULL)
{
$email = $data["email"];
$password = $data["password"];
$this->db->select('user_id');
$query = $this->db->get_where('users', array('sha2(username, NULL)' => $data["username"], 'password' => $password));
return $query->row_array();
}
为什么You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''FCD8FE02F495400DC020499AC31FC75ABA05005622FD7A2F01D37153724CB395' AND `password' at line 3
SELECT `user_id` FROM `users` WHERE sha2(username, NULL) 'FCD8FE02F495400DC020499AC31FC75ABA05005622FD7A2F01D37153724CB395' AND `password` = '2668dcd9bac3c8bb60742f139876d1d6'
符号不在=
和sha2(username, NULL)
之间
实际查询应该是:
'FCD8FE02F495400DC020499AC31FC75ABA05005622FD7A2F01D37153724CB395'
而不是:
SELECT `user_id` FROM `users` WHERE sha2(username, NULL) = 'FCD8FE02F495400DC020499AC31FC75ABA05005622FD7A2F01D37153724CB395' AND `password` = '2668dcd9bac3c8bb60742f139876d1d6'
任何想法我做错了什么?
答案 0 :(得分:1)
这实际上是一个简单的查询,使用Codeigniter方法构建语句在这种情况下无济于事。
尝试使用这样一个更简单的绑定查询。
$sql = "SELECT user_id FROM users WHERE sha2(username, 256) = ? AND `password` = ?";
$query = $this->db->query($sql, array($data["username"], $data["password"]));
return $query->row_array();
注意我将第二个参数放入mysql sha2()
调用。我认为两个参数都是必需的,如果其中任何一个丢失或为null,则sha2()
返回null。