Jess McKenzie
Jess McKenzie

Reputation: 8385

Codeigniter MYSQL Returning Most Recent Row

I have the following code below, and I am not sure of the best way to modify it so that it shows the last entered row into the database.

The user_date column is displayed like this: 2010-05-06 12:37:28

Controller:

$result = $this->shared_model->GetJustJoinedUser();
    $data['just_joined_user'] = $result->row_array();

Model:

function GetJustJoinedUser()
{
    $query = $this->db->query("SELECT *
                              FROM {$this->_table['users']} user
                              WHERE user_description != '' AND user_image != '' AND (user_twitter != '' OR user_youtube != '' OR user_vimeo!= '')
                              ORDER BY user_date DESC
                              LIMIT 1");

    return $query;
}

Upvotes: 0

Views: 895

Answers (2)

Aurel
Aurel

Reputation: 3740

Your query already get the last inserted row but with some restriction in the WHERE statement.

If you want the real last row, just strip the WHERE statement like this:

$query = $this->db->query("SELECT * FROM {$this->_table['users']} user ORDER BY user_date DESC LIMIT 1");

you do not seem to know much about the SQL queries, so i suggest you read some tuto about it.

Upvotes: 1

Þaw
Þaw

Reputation: 2057

I'm not sure if this could fix your problem but you could try something like this.

$last_id = $this->db->insert_id();

then use this $last_id to query your last inserted user. I hope this helps

$this->db->insert_id() Retrieves the ID generated for an AUTO_INCREMENT column by the previous query (usually INSERT).

UPDATE

$last_id = $this->db->insert_id();

$this->db->where('id', $last_id);
$query = $this->db->get('users');
if($query)
{
    $query = $query->result_array();
    //$query here contains the details of the last inserted user
}

Im just showing the idea on how to get the last user inserted. I hope that would help you

Upvotes: 0

Related Questions