2012-07-10 43 views
0

我一直在討論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 
+0

仍然有這個問題,我試圖顯示最後一個錯誤:\t $ wpdb-> print_error();沒有返回結果,但服務器錯誤日誌仍然顯示上面列出的錯誤...一遍又一遍。錯誤字面上填充我的錯誤日誌,在繁忙的網站上出現數千個相同的錯誤。 – 2012-07-26 17:09:50

回答

0

這似乎是的WordPress的數據庫WPDB模塊suppresses SQL errors by default

You can turn error echoing on and off with the show_errors and hide_errors, respectively.

<?php $wpdb->show_errors(); ?> 
<?php $wpdb->hide_errors(); ?> 

You can also print the error (if any) generated by the most recent query with print_error.

<?php $wpdb->print_error(); ?> 

這可能是問題嗎?

+0

我試圖用 print_error()打印錯誤; ?>,沒有返回,但仍顯示在服務器的錯誤日誌中。有趣......;) – 2012-07-10 16:06:20

0

我有這個相同的問題,答案是在錯誤,但它並不明顯。無論用戶是否擁有cookie,每次有人訪問頁面時都會運行Ref查詢。在測試時,您最可能使用cookie進行測試,因此不會產生錯誤。但是,當它運行時沒有cookie時,它正在搜索一個空白的ID。

我修改了下面的代碼並對其進行了評論。

// Visitor message with cookie or without... 
$ref = $_COOKIE['ref'];   
    /* This section should only run if there is a ref from the cookie 
    $sql = "SELECT display_name FROM " .$wpdb->prefix. "users WHERE ID=".$ref; 
    $ref = $wpdb->get_var($wpdb->prepare($sql)); */ 
if (!isset($ref) || $ref == "") { //In case the cookie returns blank instead of null 
    return 'Welcome Visitor'; 
} else { //added this section after the check for $ref so it only runs where there is a ref 
    $sql = "SELECT display_name FROM " .$wpdb->prefix. "users WHERE ID=".$ref; 
    $ref = $wpdb->get_var($wpdb->prepare($sql)); 
}  
return 'Referred By: '.$ref; 

希望這會有所幫助。