SELECT statement wrong?

时间:2017-01-17 作者:James

此代码在从底部开始的第八行都能正常工作。那里的SELECT语句找不到刚才插入的一条记录。这是目前表中唯一的记录。我做错了什么??

global $wpdb;

//Get post id for auction from post where auction is inserted. Used to track bids db.
$postid = get_the_id();
$starting_bid = 25;
$starting_email = "[email protected]";

//Creates jwp_bids table in database if it doesn\'t exist.
$table = $wpdb->prefix . "jwp_bids"; 
$charset_collate = $wpdb->get_charset_collate();
$sql = "CREATE TABLE IF NOT EXISTS $table (
    `id` mediumint(15) NOT NULL AUTO_INCREMENT,
    `bid_amt` decimal(10,2) NOT NULL,
    `email` varchar(255) NOT NULL,
    `bids_time` timestamp NULL DEFAULT CURRENT_TIMESTAMP,  
    `post_id` mediumint(15) NOT NULL,
     PRIMARY KEY (`id`)
) $charset_collate;";
require_once( ABSPATH . \'wp-admin/includes/upgrade.php\' );
dbDelta( $sql );

    $count = $wpdb->get_var("SELECT COUNT(*)
        FROM $table WHERE post_id IS NOT NULL");

        if($count == 0){
            $insert_db = $wpdb->insert( 
                    $table, 
                        array(
                            \'email\' => $starting_email,
                            \'bid_amt\' => $starting_bid,
                            \'post_id\' => $postid,
                            )
                );
        }
    // We now have one record in the table representing the starting bid.       
    //Gets highest bid information and assigns to variables for display in post.
    $highest_bid_info = $wpdb->get_results(
                        "SELECT max(bid_amt) AS bid, bid_time, email
                        FROM $table
                        WHERE post_id = $postid", ARRAY_A );
    $highest_bid_info = array_shift ( $highest_bid_info );
    $high_bid = $highest_bid_info[\'bid\'];
    $high_bidder = $highest_bid_info[\'email\'];
    $bid_time = $highest_bid_info[\'bid_time\'];

1 个回复
最合适的回答,由SO网友:Vishal Kumar Sahu 整理而成

寻找$wpdb->last_error. 你可以自己帮助自己。

相关推荐

将MySQL转换为WordPress$WPDB

我正在从3.7升级一个古老的Wordpress网站。它有一些SQL查询请求,在3.9以上版本之后无法运行,您现在必须通过$WPDB连接到DB(https://codex.wordpress.org/Class_Reference/wpdb). 我对此进行了初步尝试,但挂断了mysql\\u fetch\\u array部分的连接,也不清楚我所做的是否正确。mysql\\u fetch\\u数组似乎需要使用get\\u结果(https://developer.wordpress.org/reference/