从Joomla 2.5中的表中选择时出现SQL语法错误

时间:2014-03-06 10:48:00

标签: php mysql sql joomla

我收到此错误:

  

您的SQL语法有错误;查看与MySQL对应的手册   服务器版本,用于在'FROM lg3ni_k2_favorites AS faves附近使用正确的语法,   lg3ni_k2_items AS项,WHERE(第1行的faves.user'SQL = SELECT faves.item_id AS item_id,   items.title AS items_name,FROM lg3ni_k2_favorites AS faves,lg3ni_k2_items AS项目,   WHERE(faves.user_id ='349')AND(faves.item_id = items.id)

从语法:

$db =& JFactory::getDBO();

    $query = "SELECT faves.item_id AS item_id, items.title AS items_name, ";
    $query .= "FROM `#__k2_favorites` AS faves, `#__k2_items` AS items, ";
    $query .= "WHERE (faves.user_id='".intval($userID)."') ";
    $query .= "AND (faves.item_id = items.id) ";

    $db->setQuery($query);
    $ff = $db->loadObjectList();
    echo $db->getErrorMsg();
    return $ff; 

问题出在哪里?

3 个答案:

答案 0 :(得分:0)

items_name,之后还有一个额外的逗号,AS items之后的另一个额外逗号

 $query = "SELECT faves.item_id AS item_id, items.title AS items_name ";
 $query .= "FROM `#__k2_favorites` AS faves, `#__k2_items` AS items ";

答案 1 :(得分:0)

从以下两个查询中删除逗号

$query = "SELECT faves.item_id AS item_id, items.title AS items_name ";
    $query .= "FROM `#__k2_favorites` AS faves, `#__k2_items` AS items ";

答案 2 :(得分:0)

尝试使用以下内容:

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

$query->select($db->quoteName('faves.item_id') . ' AS ' . $db->quoteName('item_id') . ',' . $db->quoteName('items.title') . ' AS ' . $db->quoteName('items_name')))
      ->from($db->quoteName('#__k2_favorites') . ' AS ' . $db->quoteName('faves') . ',' . $db->quoteName('#__k2_items') . ' AS ' . $db->quoteName('items'))
      ->where($db->quoteName('faves.user_id') . ' = '. $db->quote(intval($userID)) . ' AND ' . $db->quoteName('faves.item_id') . ' = '. $db->quote('items.id'));     
$db->setQuery($query);

$ff = $db->loadObjectList();

这使用Joomla编码标准,但请注意我没有测试过,所以请告诉我它是否有效。