为什么Zend Framework (Zend_Db_table)拒绝这个SQL查询?

时间:2022-09-24 00:17:55

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

我正在处理两个表(url和公司)的简单连接。我正在使用这个查询调用:

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:

现在,我想选c。*实际上没有出现,但不管怎样都没关系。ZF死于以下错误:

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?

但是我可以在我的MySQL CLI中很好地运行这个查询。有什么办法解决这个问题吗?

1 个解决方案

#1


5  

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

我只是在测试脚本中尝试了针对ZF 1.10和MySQL 5.1的代码,它运行良好。

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).

您使用什么用户/密码来连接数据库?它表示“或访问违反”,因此我将测试您的db用户名是否具有正确的特权。尝试使用完全相同的用户/密码和连接方法在MySQL CLI中连接(因为特权可以根据客户端主机而变化,即使是相同的用户/密码)。

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

参见MySQL Zend Framework - SQLSTATE[42000]:语法错误或访问违规:

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

顺便说一句,你可以省略c。*将列的空数组作为连接()的第三个参数传递给列:

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

#1


5  

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

我只是在测试脚本中尝试了针对ZF 1.10和MySQL 5.1的代码,它运行良好。

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).

您使用什么用户/密码来连接数据库?它表示“或访问违反”,因此我将测试您的db用户名是否具有正确的特权。尝试使用完全相同的用户/密码和连接方法在MySQL CLI中连接(因为特权可以根据客户端主机而变化,即使是相同的用户/密码)。

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

参见MySQL Zend Framework - SQLSTATE[42000]:语法错误或访问违规:

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

顺便说一句,你可以省略c。*将列的空数组作为连接()的第三个参数传递给列:

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