首页 > 解决方案 > 我可以在 codeigniter 的 $this->db->e​​scape() 中使用 md5 散列值吗?

问题描述

当我尝试在 $this->db->e​​scape() 中使用 md5 哈希值时,当我尝试获取结果计数“调用 bool 上的成员函数 num_rows()”时,出现如下错误

我的代码

$hashedUniqueId= md5($uniqueId);
$query = "select * from my_table where uId_hash= '".$this->db->escape($hashedUniqueId)."' AND password= '".$this->db->escape($password)."' ";
$result = $this->db->query($query);
print_r($result->num_rows());

标签: phpsqlcodeigniter

解决方案


正如我所见,你正在双重逃避。从 $this->db->e​​scape() 中删除单引号。

$query = "select * from my_table where uId_hash= ".$this->db->escape($hashedUniqueId)." AND password= ".$this->db->escape($password);

或者更好的方法是在 $this->db->query($query); 中设置变量 这样,codeigniter 将为您转义它。

$hashedUniqueId= md5( $uniqueId );
$query = "select * from my_table where uId_hash= ? AND password= ?";
$result = $this->db->query( $query, array( $hashedUniqueId, $password ) );
print_r($result->num_rows());

推荐阅读