Michael Grigsby
Michael Grigsby

Reputation: 12163

CodeIgniter querying the database

If I place the echo "h"; inside the if statement I get nothing returned. Also if I place a WHERE clause inside the SQL code and use that method, I also get nothing returned inside the foreach loop. Any ideas as to why this is happening? I only need the user that matches the current user logged in. And what the script's doing is looping through all the rows in the users table

public function dcr() {
    // pass all dashboard accesses through this function
    $username = $this->session->userdata("username");
    $query = $this->db->query("SELECT * FROM users");
    foreach ($query->result() as $row) {
                    echo "h";
        if ($username == $row->username) {
                $data = array('firstname' => $row->firstname);
                $this->load->view('dashboard', $data);
            }
    }
}

Upvotes: 0

Views: 524

Answers (1)

Jasonw
Jasonw

Reputation: 5064

This should do the trick (have not tested), you don't need the if statement, just use function get_where, as the code will become cleaner and readable.

public function dcr() {
    // pass all dashboard accesses through this function
    $username = $this->session->userdata("username");
    $query = $this->db->get_where('users', array('username' => $username));
    foreach ($query->result() as $row) {
        $data = array('firstname' => $row->firstname);
        $this->load->view('dashboard', $data);
    }
}

Upvotes: 1

Related Questions