Welcome Guest, Not a member yet? Register   Sign In
is there any super easy cool trick to get the next id in codeigniter :))
#14

[eluser]David Johansson[/eluser]
[quote author="NateL" date="1248643086"][quote author="David Johansson" date="1248611542"]
But you really need to use an ORDER BY statement!

In active record it would be:
Code:
$query = $this->db->select('id')->order_by('id', 'asc')->get_where('clients', array('id >' => $id), 1, 0);
if($query->num_rows == 1)
{
    $next_id = $query->row()->id;
}
else
{
    $next_id = FALSE;
}
[/quote]

Thanks for cleaning it up and putting it into CI Active Record for me Smile

Now, I'm not doubting you - just curious why you need to use ORDER BY ?

When you say "select by id greater than 5 limit 1" - it's pretty obvious to me it's going to look for the next highest id above 5, so why sort one result?[/quote]

Well by SQL standars the rows don't come in a particular order (it's defined by "set theory"), they can for what you know come like (12, 5, 37, 2, 16...). In actuality MySQL returns the rows in the order they where entered unless you have done any reordering with "ALTER TABLE... ORDER BY...". To sum up: to be 100% sure you must use an ORDER BY statement though it will with 99% probability work anyway. You should really always use order by whenever the order makes any difference.


Messages In This Thread
is there any super easy cool trick to get the next id in codeigniter :)) - by El Forum - 07-26-2009, 12:26 PM



Theme © iAndrew 2016 - Forum software by © MyBB