尝试运行数据库查询时收到此警告:
Warning: wpdb::prepare()缺少参数2
有问题的查询是:
$result = $wpdb->get_var(
$wpdb->prepare(
"SELECT DISTINCT meta_value FROM $metatable
WHERE meta_key LIKE \'%matchme%\'
AND meta_value IS NOT NULL
AND meta_value <> \'\'"
)
);
有没有办法通过修改上述查询来删除错误?
最合适的回答,由SO网友:Mark Kaplun 整理而成
删除对的呼叫$wpdb->prepare()
:
$result = $wpdb->get_var(
"SELECT DISTINCT meta_value FROM $metatable
WHERE meta_key LIKE \'%matchme%\'
AND meta_value IS NOT NULL
AND meta_value <> \'\'"
);
在这种情况下
$wpdb->prepare()
函数为
not doing anything. 没有包含未知值的变量,因此无需对其进行清理。
如果确实有需要清理的变量,可以向函数添加第二个参数:
$result = $wpdb->get_var(
$wpdb->prepare(
"SELECT DISTINCT meta_value FROM %s
WHERE meta_key LIKE \'%matchme%\'
AND meta_value IS NOT NULL
AND meta_value <> \'\'",
$metatable )
);
Relevant links: