Dr3am3rz
Dr3am3rz

Reputation: 563

CodeIgniter get last username without primary id

I am having a problem with getting the latest username in my database. The database I am having happens to have no auto_increment id field. Below is my code:

public function _username_check( $user_name )
{

    $this->db->select('user_name');
    $this->db->like('user_name', $user_name);
    $this->db->order_by('user_date');
    $query = $this->db->get('users');


    if ($query->num_rows() > 0)
    { 


        $row = $query->row();

       $db_username = $row->username;

       echo $db_username;
    }

echo "did not get into the if function";
}

I am currently using the Community Auth files for my projects. It does not have any auto increment id field so I am using the user_date to check for the last entry.

Somehow it does not go into the if function, am I doing any wrong here?

If user were to enter username, I will need to be able to return username2 and if user were to enter user, I will need to be able to return user3.

Is this the right way to do it?

Any kind souls out there can help me on this? Thanks in advance for the help.

Upvotes: 0

Views: 120

Answers (1)

Nouphal.M
Nouphal.M

Reputation: 6344

I would suggest you to add an additional column into the user's table with date time field. By adding a date time field you can find the latest username while not altering the current db relations.

$this->db->select('user_name');
$this->db->like('user_name', $user_name);
$this->db->order_by('user_date','desc');
$this->db->limit(1);   
$query = $this->db->get('users');

Upvotes: 1

Related Questions