views:

147

answers:

4

Hi Friends,

I need to get only 1 record from sql result. we use "SELECT TOP 1" in standard sql, but how can we do that in CodeIgniter? Is there any func for that? I researched so much on net, but could not find :/

appreciate! thanks,

A: 

Not sure about codeigniter, but you could do a regular select and order by and just use the first record that is returned, i.e. ignore the ones that would come after that.

Basically, that is what the SQL engine is doing for you when you specify TOP 1.

IronGoofy
A: 
SELECT * FROM table LIMIT 1

is the syntax for mySQL

knittl
+1  A: 

Use

$this->db->limit(1);
Zahymaka
+1  A: 

with LIMIT

$this->db->limit(1);
$query = $this->db->get('my_table');
$myRow = $query->row();

with OFFSET and LIMIT

$query = $this->db->get('mytable', 0, 1);
$myRow = $query->row();
David Elizondo