Question

I'm very new to zend (1.12)..so please excuse my very basic question:

I want to fetch only one row from a database. Thatfore I want to use the fetchRow(..) function like this

$row = $db->fetchRow($db->select()->where("col1 = '".val1."' AND col2='".val2."'")); 

The problem is, that there may be many rows that fit to the where-clause and I only want to get the one with the highest id. How can I do this?

Was it helpful?

Solution

The fetchRow() method returs only one row. If you want to choose row with the highest ID meeting other conditions, invoke it like this:

$select = $db->select()
             ->where('col1 = ?', $val1)
             ->where('col2 = ?', $val2)
             ->order('id DESC');
$row = $db->fetchRow($select);

Also, remember to pass values to SQL query in the way as in above code (to avoid SQL injection attack risk).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top