我一直在討論WordPress Codex,看起來好像我的語法是正確的,但我似乎無法追查爲什麼我一直在尋找線在我errors.text
文件中的錯誤與下面的代碼,這是一個WordPress簡碼:SQL查詢在錯誤日誌中顯示錯誤而代碼執行完美
function blahblah_display_referrer() {
global $wpdb, $user_ID;
// Logged in user
if (is_user_logged_in() == true) {
$sql = "SELECT display_name FROM " .$wpdb->prefix. "users WHERE ID=".$user_ID;
$ref = $wpdb->get_var($wpdb->prepare($sql));
return 'Welcome Back: '.$ref;
}
// Visitor message with cookie or without...
$ref = $_COOKIE['ref'];
$sql = "SELECT display_name FROM " .$wpdb->prefix. "users WHERE ID=".$ref;
$ref = $wpdb->get_var($wpdb->prepare($sql));
if (!isset($ref)) {
return 'Welcome Visitor';
}
return 'Referred By: '.$ref;
}
正如我以前說過,這段代碼執行完美,沒有任何問題。它只是顯示了以下錯誤:
[10-Jul-2012 15:10:45] WordPress database error 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 for query SELECT display_name FROM wp_users WHERE ID= made by
require('wp-blog-header.php'), require_once('wp-includes/template-loader.php'),
include('/themes/kaboodle/index.php'), get_sidebar, locate_template, load_template,
require_once('/themes/kaboodle/sidebar.php'), woo_sidebar, dynamic_sidebar,
call_user_func_array, WP_Widget->display_callback, WP_Widget_Text->widget,
apply_filters('widget_text'), call_user_func_array, do_shortcode, preg_replace_callback,
do_shortcode_tag, call_user_func, blahblah_display_referrer
這裏是我的服務器信息:
Apache version 2.2.21
PHP version 5.2.17
MySQL version 5.1.63-cll
Architecture x86_64
Operating system linux
仍然有這個問題,我試圖顯示最後一個錯誤:\t $ wpdb-> print_error();沒有返回結果,但服務器錯誤日誌仍然顯示上面列出的錯誤...一遍又一遍。錯誤字面上填充我的錯誤日誌,在繁忙的網站上出現數千個相同的錯誤。 – 2012-07-26 17:09:50