我不明白......我测试了很多,但不明白为什么会出现SQL错误......
在我的情况下,我使用一个函数来生成一个mysql查询,以从数据库中获取所有额外字段项(K2 - Component)。
它的用法如下:
public static function readExtraFields ($fields, $as = 'array') {
if (!is_array($fields)) $fields = json_decode($fields);
$result = array();
$whereId = array();
$allFieldValues = array();
foreach ($fields as $field) {
$whereId[] = "`id`='".$field->id."'";
$allFieldValues[$field->id] = $field->value;
}
$query = "SELECT * FROM `#__k2_extra_fields` WHERE " . implode(" OR ", $whereId);
$queryResult = self::execQuery($query);
[...]
在这种方法中,将生成一个类似的查询(我打印出来)
SELECT * FROM `#__k2_extra_fields` WHERE `id`='1' OR `id`='2'
这很好。当我使用给定的查询执行methode execQuery时,会出现此错误:
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 SQL=SELECT * FROM `j7req_k2_extra_fields` WHERE
多数民众赞成......我不明白。我执行mysql查询的方法:
public static function execQuery ($query) {
$db = JFactory::getDbo();
$db->setQuery($query);
$result = $db->loadObjectList();
return $result;
}
为了测试,我打印出了Query(上面的内容)并将$ query设置为打印查询,这样运行没有任何问题,比如应该工作的方式......所以给出的查询是正确的...什么可以是问题?
修改1 在我的execQuery方法中,我打印了$ db。给出了这个重新组合(你可以在这个对象的底部找到查询)
JDatabaseDriverMysqli Object
(
[name] => mysqli
[nameQuote:protected] => `
[nullDate:protected] => 0000-00-00 00:00:00
[_database:JDatabaseDriver:private] => podopraxis
[connection:protected] => mysqli Object
(
[affected_rows] => 1
[client_info] => mysqlnd 5.0.11-dev - 20120503 - $Id: bf9ad53b11c9a57efdb1057292d73b928b8c5c77 $
[client_version] => 50011
[connect_errno] => 0
[connect_error] =>
[errno] => 0
[error] =>
[error_list] => Array
(
)
[field_count] => 1
[host_info] => localhost via TCP/IP
[info] =>
[insert_id] => 0
[server_info] => 5.6.16
[server_version] => 50616
[stat] => Uptime: 48979 Threads: 1 Questions: 6461 Slow queries: 0 Opens: 163 Flush tables: 1 Open tables: 155 Queries per second avg: 0.131
[sqlstate] => 00000
[protocol_version] => 10
[thread_id] => 203
[warning_count] => 0
)
[count:protected] => 45
[cursor:protected] =>
[debug:protected] =>
[limit:protected] => 0
[log:protected] => Array
(
)
[timings:protected] => Array
(
)
[callStacks:protected] => Array
(
)
[offset:protected] => 0
[options:protected] => Array
(
[driver] => mysqli
[host] => localhost
[user] => root
[password] =>
[database] => podopraxis
[prefix] => j7req_
[select] => 1
[port] => 3306
[socket] =>
)
[sql:protected] => SELECT * FROM `#__k2_extra_fields` WHERE `id`='1' OR `id`='2'
[tablePrefix:protected] => j7req_
[utf:protected] => 1
[errorNum:protected] => 0
[errorMsg:protected] =>
[transactionDepth:protected] => 0
[disconnectHandlers:protected] => Array
(
)
)
答案 0 :(得分:0)
看起来你在execQuery()函数中触发的查询缺少查询“WHERE”子句后的条件语句。
请确保以下部分条件出现在您的查询中:
`id`='1' OR `id`='2'
如果我对你的问题不满意,请告诉我?
还尝试通过打印在此处发布整个$ db对象,如下所示:
echo "<pre />";print_r($db);exit;