简体   繁体   English

MySQL语法问题-ORDER BY-Joomla PHP

[英]MySQL Syntax Issue - ORDER BY - Joomla PHP

I have a syntax issue that I can't seem to figure out. 我有一个语法问题,我似乎无法弄清楚。 I'm following the Joomla docs found here: 我正在关注在此处找到的Joomla文档:

https://docs.joomla.org/Selecting_data_using_JDatabase#Selecting_Records_from_a_Single_Table https://docs.joomla.org/Selecting_data_using_JDatabase#Selecting_Records_from_a_Single_Table

And I'm getting this error: 我收到此错误:

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 'order DESC' at line 3 SQL=SELECT `id`,`image`,`order`,`status` FROM `vsem6_session_1` ORDER BY order DESC 

My Query is: 我的查询是:

$db = JFactory::getDbo();
$query = $db->getQuery(true);

$query->select($db->quoteName(array('id', 'image', 'order', 'status')));
$query->from($db->quoteName('#__session_' . $id));
$query->order('order DESC');

$db->setQuery($query);
$results = $db->loadObjectList();   

return $results;

It works great if I take out the order clause, but of course the results aren't ordered. 如果我删除了order子句,效果很好,但是当然结果不是有序的。 I've search high and low and can't figure it out. 我搜索过高和低,无法弄清楚。 Anyone notice anything? 有人注意到吗? I am 100% certain there is an "order" column in the table. 我100%肯定表中有一个“订单”列。

I tried this, which shows no error, but it still doesn't order properly. 我尝试了此操作,它没有显示任何错误,但是仍然无法正确排序。

$query->orderby('order DESC');

order is a reserved word as its used in order by . order保留字,因为它被order by So to solve mysql's confusion you need to mark your use of order as a field name with back ticks 因此,要解决mysql的混乱,您需要将命令的使用标记为带有反勾号的字段名称

$query->order('order DESC');

The problem with this is that that you have not used quoteName(). 问题是您没有使用quoteName()。

$query->order($db->quoteName('order') . 'DESC');

Even though you said you would change the field name, I'm adding this for the situation in which someone could not change the name. 即使您说过要更改字段名称,我还是要添加此名称,以防有人无法更改名称。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM