doulu8341 2015-11-13 11:48
浏览 51
已采纳

循环遍历SQL结果,仅输出set field_value

I am using the following SQL query to pull table date from another database.

$query = "SELECT option_value FROM db_name.wpm_options where option_name like 'options_go_tracking_%_tracking_pixel'";

$results = $wpdb->get_results( $query );

Everything is connecting fine and the results of the query return as expected. I am a little unsure as to how I can now loop through the results of the query, I think a foreach function is required but being a novice with sql I am having trouble proceeding.

The name of the field is "option_value", would this be used in the foreach?

My attempt to far is below, the echo is being output 10 times and there are 10 records in the sql query so I must be close, just need to output the data within the field.

    $query = "SELECT option_value FROM db_nameprivate.wpm_options where option_name like 'options_go_tracking_%_tracking_pixel'";
    $results = $wpdb->get_results( $query );

    if($results):
    foreach($results as $row): setup_postdata($row);

        echo "output";
        // Trying to output the results of query here, the content of each "option_value" field


    endforeach;
    else: '.echo "No Records Found".';
    endif;
  • 写回答

3条回答 默认 最新

  • doukenqiong0588 2015-11-13 12:04
    关注

    In WordPress, the $wpdb->get_results($query) returns an array of objects with properties that represent their associated column names. When iterating over a result-set, you can simply access the column by its associated property, just like this:

    echo $row->option_value

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

报告相同问题?

悬赏问题

  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化