-- Adam Balgach <[EMAIL PROTECTED]> wrote
(on Monday, 05 March 2007, 12:20 PM -0500):
> I am trying to form the query:
> 
> Select firstname, lastname, email from table where user_id = 1
> 
> so when i do:
>    
>     $select=$this->_db->select();
>     $select->from('table', 'table.firstname ');
>     $select->from('table', 'table.lastname');
>     $select->from('table', 'table.email');
>     $select->where('table.user_id = '.$userId);

If you're selecting from the same table, pass an array of fields to
select as the second argument to from(). So, I'd write this as:

    $select->from('table', array('firstname', 'lastname', 'email'))
           ->where('user_id = ?', $userId)

That should work. You could also prefix the field names in the array
with 'table.' if you need to.

(Note the change in the where() method as well -- the notation I provide
will escape the data in $userId and replace the escaped version into the
'?' sigil in the clause; much safer.)

The additional from() calls to the same table is what creates the joins
you see below.

> it is generating this query:
> 
>  SELECT
>         table.`firstname`,
>         table_2.`lastname`,
>         table_3.`email`
> FROM `table` table
>  INNER JOIN `table` table_2
>  INNER JOIN `table` table_3
> WHERE
>       table.user_id = 11
> 
> 
> which clearly does not return the same result.  Is this a bug?  Or am I using
> select incorrectly.

-- 
Matthew Weier O'Phinney
PHP Developer            | [EMAIL PROTECTED]
Zend - The PHP Company   | http://www.zend.com/

Reply via email to