dsdukbc60905239 2015-02-11 10:03
浏览 37
已采纳

在PHP中使用mysql数组数据

how to convert array data to work with it in this case? especially in wordpress. when i print the results i get stdClass Object.

Array
(
    [0] => stdClass Object
        (
            [posts_id] => 336
            [value] => 8
            [count(*)] => 2
        )

    [1] => stdClass Object
        (
            [posts_id] => 329
            [value] => 2
            [count(*)] => 1
        )

    [2] => stdClass Object
        (
            [posts_id] => 327
            [value] => 3
            [count(*)] => 1
        )

    [3] => stdClass Object
        (
            [posts_id] => 338
            [value] => 1
            [count(*)] => 1
        )

)

and this is my query

   <?php global $wpdb;

$test = $wpdb->get_results('select
    posts_id,
    value,
    count(*)
From
    wp_mrp_rating_item_entry_value
group by
    posts_id,
    value
order by
    count(*) desc'); 
echo '<pre>';
print_r($test);
echo '</pre>';

How can i get the data out of this array to save into another database table? Or how can i at least save the single values into variables for every id? I tried allot but i have no idea yet so i hope someone is knowing it better than me? thanks for any help.

  • 写回答

5条回答 默认 最新

  • duanniling0018 2015-02-11 10:20
    关注

    foreach($test as $item) :
    
    $ID = $item->posts_id; 
    var_dump($ID); //test variable
    
    
    //if you want to place into the db 
    
       //DB CODE>>>
    
    $firstName = "Dylan"  //example of field in db to change
    $LastName =  "bloggs" //example of second field in db to change
    
    $wpdb->query( $wpdb->prepare( 
        "
            INSERT INTO $wpdb->name_of_wp_table_goes_here
            ( name_of_field_goes_here )
            VALUES ( %d, %s, $s ) 
        ", 
        $ID,
        $field
    ) );
    
       //<<<DB END
    
    endforeach; 

    Remove the DB code if you want to manually store into a database.

    You could also access other keys the same way. e.g., $value = $item->value; -- you would place this in the foreach loop and continue.

    </div>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 PADS Logic 原理图
  • ¥15 PADS Logic 图标
  • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
  • ¥50 STM32单片机传感器读取错误
  • ¥15 (关键词-阻抗匹配,HFSS,RFID标签天线)
  • ¥15 机器人轨迹规划相关问题
  • ¥15 word样式右侧翻页键消失