Arrays Codeigniter验证将查询行转换为数组
我最近经历了一个非常奇怪的错误。我正在编写一个密码重置函数,但我遇到了一个以前从未遇到过的非常奇怪的错误。不知怎的,表单验证将我的查询变成了空数组 这是我的密码:Arrays Codeigniter验证将查询行转换为数组,arrays,validation,codeigniter,Arrays,Validation,Codeigniter,我最近经历了一个非常奇怪的错误。我正在编写一个密码重置函数,但我遇到了一个以前从未遇到过的非常奇怪的错误。不知怎的,表单验证将我的查询变成了空数组 这是我的密码: public function password_reset() { if ($this->auth_model->is_logged_in()) redirect ('welcome'); $userid = $this->uri->segment(3); $code
public function password_reset()
{
if ($this->auth_model->is_logged_in()) redirect ('welcome');
$userid = $this->uri->segment(3);
$code = $this->uri->segment(4);
$this->db->select('forgot_password, id');
$this->db->from('users');
$this->db->limit(1);
$this->db->where('id',$userid);
$q = $this->db->get();
if (!is_numeric($this->uri->segment(4, 0)) == TRUE && !is_numeric($this->uri->segment(3, 0)) == TRUE) die;
if ($q->row('forgot_password') == $code) {
$this->form_validation->set_rules('password', 'Password', 'required|min_length[4]');
$this->form_validation->set_rules('confirm_password', 'Confirm', 'required|min_length[4]');
if ($this->form_validation->run() !== false) {
if ($this->input->post('password') != $this->input->post('confirm_password')) exit("wrong");
$update = array (
'password' => sha1($this->input->post('password'))
);
$id = $q->row('id');
$this->db->where('id', $id);
$this->db->update('users', $update);
echo $this->db->last_query();
die; // Don't mind the last 2 lines, they were made for debugging purposes.
echo "Password is reset";
} $this->load->view('auth/password_reset');
}
}
请注意这一行:
$id=$q->row'id'
这就是问题的开始
如果我回显这一行,将获得数组,我尝试在其上进行打印,但它似乎是完全空的数组
然而,如果我试着回应
$q->row('id');
以前
if ($this->form_validation->run() !== false) {
然后,我得到了预期的结果,在我的例子中,它将是整数,但同样,一旦表单验证通过,它将把我的整数变成空数组
现在,表单验证以某种方式将查询行转换为数组
任何帮助都将不胜感激。在您使用此功能的地方:
$q = $this->db->get();
//...
if ($q->row('forgot_password') == $code) {
您应该只使用row获取对象一次,然后引用该对象的属性,而不是反复调用row
因此,为了使您能够修复此问题,您的代码可能存在其他问题,但在解决此问题之前,我们不会知道,请尝试以下方法:
$row = $this->db->get()->row();
从第一行获取结果。完成后,现在将结果存储在对象中,无需再次获取它
现在,当您想要引用该对象的属性(本例中为列的值)时,您可以使用:
if ($row->forgot_password == $code) {}
//
$id = $row->id;
等等。请再次阅读文档以了解更多演示:最好使用结果数组并根据参数检查第一个记录
$q = $this->db->select('forgot_password, id')
->from('users')
->limit(1)
->where('id',$userid)
->get();
$res = $q->result_array();
$forgot_password = (count($res[0]) == 1) $res[0]['forgot_password'] : FALSE;
...
// And change this line
// if ($q->row('forgot_password') == $code) {
// into
if ( ! $forgot_password)
{
// Theres no record match with $userid
}
elseif($forgot_password == $code)
{
// Everythings fine...
}
else
{
// Theres a record match with $userid, but the $code not match
}
谢谢,但似乎没有按计划进行。每当我使用$row->columnname时;我获取错误:消息:试图获取非对象的属性,因此值返回为NULL。所以,$this->db->get->row->id;给你空值?嗯,我忘记了如果找不到一行,返回值是什么,但也许您只是没有找到与where'id',$userid匹配的值?无论哪种方式,您当前使用的代码语法都不正确。试着用一些exitvar_dump$vars来处理它,并确保每个变量都包含您所期望的内容。我想我已经找到了问题所在,但没有找到解决方法。因为我使用的是URL段,一旦表单通过验证,它就会丢失这些段,从而导致出现错误。找到了。-我认为必须重新编译一点代码,这样编辑函数将处于不同的函数中。无论如何,谢谢你的帮助,非常感谢。你的表单的动作属性正确吗?除非url发生更改,否则如果您以前能够访问URI段,就不会有其他解释说明为什么会丢失这些URI段。