Valid SQL query return empty

时间:2017-12-04 作者:Bruno Sousa

打印准备好的查询时,我看到以下字符串:

"SELECT p.ID, COALESCE(w.value,Display_name) value, p.post_title, p.post_date, p.post_content,p.post_author,user_login FROM wp_posts p INNER JOIN wp_users a ON a.ID = p.post_author LEFT JOIN wp_bp_xprofile_data w ON w.user_id = a.ID WHERE post_type = \'reply\' and post_parent = 53592568 and p.ID > 53592614 AND TIMEDIFF( \'2017-12-04 06:14:58\', post_date ) < \'00:00:15\' AND post_content is not null ORDER BY post_date ASC LIMIT 0 , 5"
当我通过SQL客户端运行它时,它似乎返回良好:mySQL client

但当我试着和wpdb->get_resultswpdb->query 我的wordpress模板中的函数。

这是我的代码:

global $wpdb;

$reply_id = $_REQUEST["id"];
$postId   = $_REQUEST["post_id"];

$data = date("Y-m-d H:i:sa");
$dataStr = str_replace("am","",str_replace("pm","",$data));

$mysqli_query = "SELECT p.ID, COALESCE(w.value,Display_name) value, p.post_title, p.post_date, p.post_content,p.post_author,user_login ";
$mysqli_query .= " FROM  wp_posts p ";
$mysqli_query .= " INNER JOIN wp_users a ON a.ID = p.post_author ";
$mysqli_query .= " LEFT JOIN wp_bp_xprofile_data w ON w.user_id = a.ID ";
$mysqli_query .= " WHERE post_type = \'reply\' ";
$mysqli_query .= " and post_parent = %d ";
$mysqli_query .= " and p.ID > %d ";
$mysqli_query .= " AND TIMEDIFF(  \'%s\', post_date ) <  \'00:00:15\' ";
$mysqli_query .= " AND post_content is not null ";
$mysqli_query .= " ORDER BY post_date ASC  ";
$mysqli_query .= " LIMIT 0 , 5";

$reply_query = $wpdb->prepare($mysqli_query, $postId, $reply_id, $dataStr);

$results = $wpdb->get_results( $reply_query );
var_dump($results);
这里是什么var_dump 为我显示:

array(0) { }
有人能帮我找出这个代码的错误吗?

非常感谢。

2 个回复
最合适的回答,由SO网友:Drupalizeme 整理而成

查询存在视图问题:

首先是简写的date("Y-m-d H:i:s"); $wpdb->prefix

  • 用户表列名为display_namebp_xprofile_data 我嘲笑的表:

    $reply_id = $_REQUEST["id"];
    $postId   = $_REQUEST["post_id"];
    $data = date("Y-m-d H:i:s");
    $mysqli_query = "SELECT p.ID, COALESCE(w.value,a.display_name) value, p.post_title, p.post_date, p.post_content, p.post_author, a.user_login ";
    $mysqli_query .= " FROM {$wpdb->prefix}posts p ";
    $mysqli_query .= " INNER JOIN {$wpdb->prefix}users a ON a.ID = p.post_author ";
    $mysqli_query .= " LEFT JOIN {$wpdb->prefix}bp_xprofile_data w ON w.user_id = a.ID ";
    $mysqli_query .= " WHERE p.post_type = \'reply\' ";
    $mysqli_query .= " AND p.post_parent = %d ";
    $mysqli_query .= " AND p.ID > %d ";
    $mysqli_query .= " AND TIMEDIFF(  %s, p.post_date ) <  \'00:00:15\' ";
    $mysqli_query .= " AND p.post_content IS NOT NULL ";
    $mysqli_query .= " ORDER BY p.post_date ASC  ";
    $mysqli_query .= " LIMIT 0 , 5";
    
    $reply_query = $wpdb->prepare($mysqli_query, $postId, $reply_id, $data);
    
    $results = $wpdb->get_results( $reply_query );
    

  • SO网友:janh

    不要在占位符周围加单引号,它们是自动添加的。将\'%1!\'转换为%2。

    结束

    相关推荐

    PRE_GET_POST完全删除TAX_QUERY

    我正在使用pre_get_posts 使用筛选$wp_query->set( \'tax_query\', $tax_query );I\'m probably better-off doing this with a new WP_Query, but if someone knows something I don\'t I\'d love to hear it问题,我需要设置默认分类法;保留显示所有帖子的功能。例如:/cpt/?taxo=this 显示所有具有taxo术语==此的cpt。$ta