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条)

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效