2

I want to get the maximum id of row data. In my table first column is id then firstname and etc. this is the sql command I used to get the max(id) of row data.

$maxid = $this->db->query("SELECT MAX(id) FROM `emplyee_personal_details`");
print_r($maxid);

but it prints bunch of data as it is a array. But I need only the maximam id of row data for validation before data inserting and updating.

How can I get the max id? I use the CodeIgniter framework.

1

4 Answers 4

10

Try this:

$maxid = $this->db->query('SELECT MAX(id) AS `maxid` FROM `emplyee_personal_details`')->row()->maxid;

UPDATE

This will work even if your table is empty (unlike my example above):

$maxid = 0;
$row = $this->db->query('SELECT MAX(id) AS `maxid` FROM `emplyee_personal_details`')->row();
if ($row) {
    $maxid = $row->maxid; 
}
Sign up to request clarification or add additional context in comments.

1 Comment

@Shomz you were right the first time. Even with an empty table a single-row result will be returned with the object property holding a null value. Your script is right (though you can write the column name as a parameter of row()).
5

The problem with using a raw query like "SELECT MAX(id) ..." is it is not abstract and may not work for every SQL engine. I think the active record way to do it is like this:

$this->db->select_max('id');
$query = $this->db->get('emplyee_personal_details');
// Produces: SELECT MAX(id) as age FROM emplyee_personal_details

See: http://ellislab.com/codeigniter/user-guide/database/active_record.html

4 Comments

I want to get the final row of id in order to decide to update or insert data in my application. there may be 10 rows but final id may be 15 since some rows can be deleted.
Then you want the last inserted id? Like this? $this->db->insert_id(); See: ellislab.com/codeigniter/user-guide/database/helpers.html
He's not inserting anything, but deciding whether to insert or update... which is why that won't work.
how to put $this->db->select_max('id') in $this->db->where() condition?
1
SELECT id FROM table ORDER BY id DESC LIMIT 1

That will get you the highest id value, and when I read this, "it prints bunch of data as it is a array" I get the sense that what you really want is a part of that array. DB queries always return complex structures like arrays or objects. So if you wanted just the scalar value (the number as an integer) you might use something like this:

$maxid = (int)$maxid['id'];

or like this (if you have an object):

$maxid = (int)$maxid->id;

HTH, ~Ray

4 Comments

See my comment on Samuel's answer.
@Kolink: I understand your point and that may be quite true, but as a practical matter, I also understand the following... the table is probably a list of employees, thus it does not have very many rows. The difference in elapsed time between any of the query solutions here will be impossibly small, perhaps below the threshold of measurement. Some things should not be optimized because it takes longer to make the change than all the value the change will ever produce!
Well, your answer is suggesting they change the whole query. Shomz's answer explains how to use the result of the query they have.
@Ray Paseur Looking at your Reputation, you may have considered formatting the code in you answer.
1
return $this->db
    ->select_max('id')
    ->get('your_table_name')
    ->row()
    ->id;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.