0

errors.textWordPress Codex を調べてみましたが、私の構文は正しいように見えますが、以下のコードに関連するファイルに行ごとにエラーが発生し続ける理由を追跡できないようです。 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;
}

前に述べたように、このコードは問題なく完全に実行されます。次のエラーが表示されるだけです。

[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
4

2 に答える 2

0

私はこれと同じ問題を抱えており、答えはエラーにありますが、明らかではありません。クッキーを持っているかどうかに関係なく、誰かがページに来るたびに 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;

お役に立てれば。

于 2013-03-21T19:18:01.887 に答える
0

Wordpress の WPDB データベース モジュールは、デフォルトで SQL エラーを抑制しているようです。

show_errors と hide_errors をそれぞれ使用して、エラー エコーのオンとオフを切り替えることができます。

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

print_error を使用して、最新のクエリによって生成されたエラー (ある場合) を出力することもできます。

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

それが問題でしょうか?

于 2012-07-10T15:44:54.617 に答える