简体   繁体   English

错误执行代码时错误日志中显示错误的SQL查询

[英]SQL Query Showing Errors In Error Log While Code Executes Flawlessly

I have been going though the WordPress Codex, and it seems as though my syntax is correct, but I can't seem to track down why I keep getting line after line of errors in my errors.text file related to the code below, which is for a WordPress Shortcode: 我一直在使用WordPress Codex,似乎我的语法是正确的,但是我似乎无法追查为什么我的errors.text文件中与错误代码相关的错误不断出现的原因。适用于WordPress短代码:

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;
}

As I said before, this code executes perfectly without any issue. 如我之前所说,此代码可以完美执行,没有任何问题。 It just shows the following error: 它仅显示以下错误:

[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

Here is my server information: 这是我的服务器信息:

Apache version  2.2.21
PHP version     5.2.17
MySQL version   5.1.63-cll
Architecture    x86_64
Operating system    linux

It would seem that Wordpress's WPDB database module suppresses SQL errors by default . 默认情况下 ,Wordpress的WPDB数据库模块似乎抑制了SQL错误

You can turn error echoing on and off with the show_errors and hide_errors, respectively. 您可以分别使用show_errors和hide_errors打开和关闭错误回显。

 <?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. 您还可以使用print_error打印由最近查询生成的错误(如果有)。

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

Could that be the problem? 这可能是问题吗?

I've had this same issue, and the answer is in the error, but it's not obvious. 我遇到过同样的问题,答案出在错误中,但这并不明显。 You're running the Ref query every time someone comes to the page regardless of whether they have a cookie or not. 每当有人进入页面时,无论他们是否有cookie,您都在运行Ref查询。 When you're testing, you're most likely testing with a cookie so it's not generating the error. 在测试时,您很有可能使用Cookie进行测试,因此不会产生错误。 However, when it runs without a cookie, it's searching for a blank ID. 但是,当它运行时没有cookie时,它将搜索一个空白ID。

I've modified your code below and commented the changes. 我已经在下面修改了您的代码并评论了这些更改。

// 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;

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM