1

I use CodeIgniter and have an issue with SELECT MAX().

It looks like it returns only id. It's giving an error for other columns of table that I'm trying to access.

Model:

function get_default() 
{
    $this->db->select_max('id');
    $query = $this->db->getwhere('gallery', array('cat' => "1"));   
                
    if ($query->num_rows() > 0) {
        return $query->row_array(); //return the row as an associative array
    }
}

Controller:

$default_img = $this->blabla_model->get_default();
$data['default_id'] = $default_img['id']; // it returns this
$data['default_name'] = $default_img['gname']; // it gives error for gname although it is at table

5 Answers 5

5

To achieve your goal, your desire SQL can look something like:

SELECT *
FROM gallery
WHERE cat = '1'
ORDER BY id
LIMIT 1

And to utilise CodeIgniter database class:

$this->db->select('*');
$this->db->where('cat', '1');
$this->db->order_by('id', 'DESC'); 
$this->db->limit(1);
$query = $this->db->get('gallery');

Sign up to request clarification or add additional context in comments.

1 Comment

yes yes i know that, thats a good alternative solution :) thanks for that. but i actually wanted to know more about select max in codeigniter.
4

That is correct: select_max returns only the value, and no other column. From the specs:

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

You may want to read the value first, and run another query.
For an id, you can also use $id = $this->db->insert_id();

See also: http://www.hostfree.com/user_guide/database/active_record.html#select

1 Comment

so , does select_max seriously return only the value in select_max($bla)? wooa, weird! so I will have to run an another query with the id i got from select_max ... not very efficient :/
0

It should be noted that you may of course also utilize your own "custom" sql statements in CodeIgniter, you're not limited to the active record sql functions you've outlined thus far. Another active record function that CodeIgniter provides is $this->db->query(); Which allows you to submit your own SQL queries (including variables) like so:

function foo_bar()

{
   $cat = 1;
   $limit = 1;
   $sql = "
        SELECT *
        FROM gallery
        WHERE cat = $cat
        ORDER BY id
        LIMIT $limit
   ";
   $data['query'] = $this->db->query($sql);
   return $data['query'];
}

Recently I have been utilizing this quite a bit as I've been doing some queries that are difficult (if not annoying or impossible) to pull off with CI's explicit active record functions. I realize you may know this already, just thought it would help to include for posterity.

2 helpful links are:

http://codeigniter.com/user_guide/database/results.html

http://codeigniter.com/user_guide/database/examples.html

Comments

0

CodeIgniter will select * if nothing else is selected. By setting select_max() you are populating the select property and therefore saying you ONLY want that value.

To solve this, just combine select_max() and select():

$this->db->select('somefield, another_field');
$this->db->select_max('age');

or even:

$this->db->select('sometable.*', FALSE);
$this->db->select_max('age');

Should do the trick.

1 Comment

This advice is simply no good, right Phil?
0

Your querying logic is fundamentally flawed. Using MAX() without GROUP BY will return s single row of results. Appending non-aggregate data in the SELECT clause will yield data from a record which is undeterministic/unpredictable/unrelated.

So the following script can broadly be considered an antipattern:

public function BAD_getLatestRow_BAD(): ?array
{
    // DO NOT USE THIS SCRIPT!
    return $this->db
        ->select()
        ->select_max('id')
        ->get_where('gallery', ['cat' => '1'])
        ->row_array();
}

select_max() does not serve as a way to target a row with the max value in a particular column. Here's a SQLize Demo of how SELECT *, MAX(id) is simply flawed. When I tested SELECT MAX(id), *, the query simply failed.

Instead, use ORDER BY and LIMIT ro return a single row containing the highest id value.

public function getLatestRow(): ?array
{
    return $this->db
        ->order_by('id', 'desc')
        ->get_where('gallery', ['cat' => '1'], 1)
        ->row_array();
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.