Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/joomla/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql CodeIgniter-在查询未返回结果时执行操作_Sql_Codeigniter - Fatal编程技术网

Sql CodeIgniter-在查询未返回结果时执行操作

Sql CodeIgniter-在查询未返回结果时执行操作,sql,codeigniter,Sql,Codeigniter,我有一个模型,它接受用户名和密码,并返回其他与用户相关的详细信息,例如名称和数组中的总点数。 若查询返回空白(若未找到记录),则应重定向回登录页面。 在这里,我还将空数组添加为“$results=array()”。 请指导我的代码中的错误在哪里。在所有情况下,它都返回为true 我的模型: function validate() { $this->db->where('username', $this->input->post('username'));

我有一个模型,它接受用户名和密码,并返回其他与用户相关的详细信息,例如名称和数组中的总点数。 若查询返回空白(若未找到记录),则应重定向回登录页面。 在这里,我还将空数组添加为“$results=array()”。 请指导我的代码中的错误在哪里。在所有情况下,它都返回为true

我的模型:

function validate()
{
    $this->db->where('username', $this->input->post('username'));
    $this->db->where('password', $this->input->post('password'));
    $query=$this->db->get('users');
    $results=array();
        foreach($query->result() as $rows)
            {
                $results[]= array(
               'first_name'   => $rows->first_name,
               'phone'        => $rows->phone,
               'total_points' => $rows->total_points,
                                );
            }

    return $results;
    }
这是我的控制器:

public function validate_credentials()
{
    $this->load->model('Membership_model');
    $query['result']=$this->Membership_model->validate();
    if(empty($query))  //if $query is blank it should redirect to home page
    {
        redirect('http://localhost/luckyDraw/en');
    }
    else echo "details received";
    // else
    // {

    //  $data=array(
    //      'username' => $this->input->post('username'),
    //      'is_logged_in' => true,
    //          );
    //  $this->session->set_userdata($data);
    //  $this->load->view('members_area',$query);
    // }
}
试试这个:

if ($this->db->get_where('users', array('username' => $this->input->post('username', true), 'password' => $this->input->post('password', true))->num_rows() > 0)
{
 // write your foreach statement here (I think you shouldn't write foreach statement to fetch user data, you can use `row_array`)
}
else
{
 // if there is no result
}

提示:在数据库中加密密码。您可以使用
password\u hash
password\u verify
。这是非常重要的。请参阅。

获取对象,然后将相同的数据转换为数组。 我不知道您在您的模型中执行此操作的内容和原因,也不知道您为什么在控制器上接收到
消息并将其发送到视图和显示器

你应该怎么做? 我看到您正在从数据库获取单用户数据。然后使用
$query->row_arary()

您的查询和模型代码应为:

$this->db->select('first_name,phone,total_points');
$this->db->where('username,$this->input->post('username')');
$this->db->where('password', $this->input->post('password'));
$query=$this->db->get('users');

// Now To get the User data Array in result:
$user_data = $query->row_array();
return $user_data; // return the data to the controller:
您的控制器代码:

public function validate_credentials()
{
    $this->load->model('membership_model');
    $user_data = $this->membership_model->validate();
    
    if(isset($user_data) && !empty($user_data)){
        
         $this->load->view('your_view'.['msg'=>'details received']);
        
    } else {        
        //if $query is blank it should redirect to home page        
    }
}
如果要在视图侧回显用户数据,请执行以下操作:

<?php 
echo $user_data['first_name']; // Same with other data.
?>

为什么不使用var_dump($query)来确保它像您期望的那样为空?