【问题标题】:Wordpress, getting query from sql database and echoing itWordpress,从 sql 数据库中获取查询并回显它
【发布时间】:2013-01-23 17:32:02
【问题描述】:

我想获取特定用户的上次修改/创建日期,然后回显它。

我想做的是:

<?php $id = get_the_ID();               

global $current_user;
$current_user = wp_get_current_user();

$postID = $id;  //assigning post id
$userID = $current_user->ID;  // assigning user ID

$sql = "SELECT post_date FROM wp_posts WHERE ID = $postID AND post_author = $userID ORDER BY post_date DESC LIMIT 1";
$userModifiedDate = $wpdb->query($sql);
echo $userModifiedDate; ?>

我的错误在哪里?谁能带领我完成这个?

目前$userModifiedDate 回复我1

【问题讨论】:

    标签: php sql database wordpress


    【解决方案1】:

    $wpdb-&gt;query() 返回受影响的行数,而不是实际的查询结果。

    http://codex.wordpress.org/Class_Reference/wpdb#Run_Any_Query_on_the_Database

    尝试使用更具体的函数,例如$wpdb-&gt;get_var()$wpdb-&gt;get_results()

    $userModifiedDate = $wpdb->get_var( $sql );
    

    http://codex.wordpress.org/Class_Reference/wpdb#SELECT_a_Variable

    另外,虽然不是绝对必要,但我总是喜欢先通过$wpdb-&gt;prepare() 传递任何查询:

    $sql = $wpdb->prepare( "SELECT post_date FROM wp_posts WHERE ID = %d AND post_author = %d ORDER BY post_date DESC LIMIT 1", $postID, $userID );
    

    http://codex.wordpress.org/Class_Reference/wpdb#Protect_Queries_Against_SQL_Injection_Attacks

    【讨论】:

    • 非常感谢,需要稍微调整一下,但这对我帮助很大! :)
    猜你喜欢
    • 1970-01-01
    • 2018-08-27
    • 2014-06-07
    • 1970-01-01
    • 1970-01-01
    • 2014-10-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多