简体   繁体   中英

Wordpress mysql query no accepting variable

I have been trying to code a mysql query in wordpress but it does not seem to work. code:

foreach ($postList as $curpostid) {
        echo "postID ".$curpostid." + ";
        $sql2 = 'SELECT * FROM wp_mr_rating_item_entry WHERE post_id='.$curpostid;
        $results2 = $GLOBALS['wpdb']->get_results($sql2) or die(mysql_error());
}

$postList prints as:

Array
(
[0] => 3148
[1] => 3097
[2] => 3048
[3] => 1036
)

These post ids do exist, and the echo shows them up fine. If I enter a specific value for $curpostid as one of the array entries the query works. It seems the query is not accepting the variable form the array. I have tried making the variable as int (int)$curpostid since the field type is bigint but still not working. Any help appreciated.

I have managed to place the output in a multidimensional array, but now I am not able to place the values in a single array: this is the array:

 Array
(
   [0] => Array
    (
        [0] => stdClass Object
            (
                [rating_item_entry_id] => 1
            )

        [1] => stdClass Object
            (
                [rating_item_entry_id] => 2
            )

        [2] => stdClass Object
            (
                [rating_item_entry_id] => 3
            )

        [3] => stdClass Object
            (
                [rating_item_entry_id] => 4
            )

        [4] => stdClass Object
            (
                [rating_item_entry_id] => 5
            )

        [5] => stdClass Object
            (
                [rating_item_entry_id] => 6
            )

        [6] => stdClass Object
            (
                [rating_item_entry_id] => 7
            )

        [7] => stdClass Object
            (
                [rating_item_entry_id] => 8
            )

    )

[1] => Array
    (
        [0] => stdClass Object
            (
                [rating_item_entry_id] => 10
            )

    )

[2] => Array
    (
        [0] => stdClass Object
            (
                [rating_item_entry_id] => 11
            )

    )

[3] => Array
    (
        [0] => stdClass Object
            (
                [rating_item_entry_id] => 14
            )

    )

)

How can I place all the final rating_item_entry_id values in a single array? Have tried with following code but I am bit confused and its not working.

foreach($results2Array as $result2) {
        foreach($result2 as $res2) {
            echo   $res2['rating_item_entry_id'];
            $ratingentries[] = $res2['rating_item_entry_id'];
        }
    }

Try this:

foreach ($postList as $curpostid) {

        $sql2 = "SELECT * FROM wp_mr_rating_item_entry WHERE post_id='".$curpostid."'"; //change here
        $results2 = $GLOBALS['wpdb']->get_results($sql2) or die(mysql_error());
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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