views:

76

answers:

1

I'm working on a simple JOIN of two tables (urls and companies). I am using this query call:

print $this->_db->select()->from(array('u' => 'urls'),
                                 array('id', 'url', 'company_id'))
                          ->join(array('c' => 'companies'),
                                 'u.company_id = c.id');

which is out putting this query:

SELECT `u`.`id`, `u`.`url`, `u`.`company_id`, `c`.* FROM `urls` AS `u` INNER JOIN `companies` AS `c` ON u.company_id = c.id

Now, I'd prefer the c.* to not actually appear, but either way it doesn't matter. ZF dies with this error:

SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1"

but I can run that query perfectly fine in my MySQL CLI. Any ideas how to fix up this query?

+4  A: 

I just tried that code in a test script against ZF 1.10 and MySQL 5.1, and it works fine.

What user/password are you using to connect to your database? It says "or access violation" so I would test that your db username has the right privileges. Try connecting in the MySQL CLI using the exact same user/password and connection method (because privileges can vary depending on the client host, even for the same user/password).

See MySQL Zend Framework - SQLSTATE[42000]: Syntax error or access violation:

Btw, you can omit c.* columns by passing an empty array for columns as the third argument to join():

->join(array('c' => 'companies'), 'u.company_id = c.id', array());
Bill Karwin