0

这可能已经回答(或应该是基本知识),所以请原谅菜鸟问题......

我正在使用 WP user_meta 数据库来存储用户的高级属性。

数据按以下顺序存储(这都是针对 userid=15)​​:

city = Altoona
state = PA
phone = 9999999999
name = Fred

I want to generate the output as
Fred, Altoona, PA, 9999999999

这是相关的PHP代码:

 $my_exportlist = $wpdb->get_results("SELECT `user_id` FROM `wp_m_membership_relationships` WHERE `level_id` = '2' ");
foreach ($my_exportlist as $duser) {

$my_stationinfo = $wpdb->get_results("SELECT * FROM `wp_usermeta` WHERE `meta_key` IN ('name', 'city', 'state', 'office') AND user_id ='$duser->user_id'");

foreach ($my_stationinfo as $myinfo) {
    $csv_output .= $myinfo->meta_value . ",";
    }
$csv_output .= "\n";
}

在第一个答案之后,我这样做了:

$my_stationinfo = $wpdb->get_results("GROUP_CONCAT(meta_value) val_output
        FROM `wp_usermeta`
        WHERE `meta_key` IN ('name', 'city', 'state', 'office') AND user_id ='$duser->user_id'
        ORDER BY FIELD(meta_key, 'name', 'city', 'state', 'office'");

我收到此错误消息...

WordPress database error: [You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'GROUP_CONCAT(meta_value) val_output FROM `wp_usermeta` WHERE `meta_key' at line 1]

在查看了一些 q/a 和链接的 mySQL 指令之后,我仍然感到困惑....

进一步跟进:这是当前代码

   $my_exportlist = $wpdb->get_results("SELECT `user_id` FROM `wp_m_membership_relationships` WHERE `level_id` = '2' ");
foreach ($my_exportlist as $duser) {

$my_stationinfo = $wpdb->get_results(
    "SELECT GROUP_CONCAT(meta_value) val_output
        FROM   wp_usermeta 
        WHERE  meta_key IN ('primary_calls', 'city', 'state', 'office', 'contact_emerg', 'contact_routine', 'contact_backup', 'contact_billing', 'contact_eng', 'contact_web', 'contact_prod', 'contact_traffic') AND
               user_id = '$duser->user_id'
               ORDER BY FIELD(meta_key, 'primary_calls', 'city', 'primary_calls','state', 'office', 'contact_emerg', 'contact_routine', 'contact_backup', 'contact_billing', 'contact_eng', 'contact_web', 'contact_prod', 'contact_traffic')");
print_r($my_stationinfo);
var_dump($my_stationinfo);
echo $my_stationinfo->val_output[0] ;


foreach ($my_stationinfo as $myinfo) {

    $csv_output .= $myinfo;
}
$csv_output .= "\n";

结果:

Array ( [0] => stdClass Object ( [val_output] => Altoona,PA,814.943.8112,WRTA ) )


array
  0 => 
    object(stdClass)[5]
      public 'val_output' => string 'Altoona,PA,814.943.8112,WRTA' (length=28)


Notice: Trying to get property of non-object in D:\Users\Dev\Documents\Websites\contract.dev\wp-content\plugins\custom_plugin\index_page.php on line 40

Catchable fatal error: Object of class stdClass could not be converted to string in D:\Users\Dev\Documents\Websites\contract.dev\wp-content\plugins\custom_plugin\index_page.php on line 45
4

1 回答 1

1

利用GROUP_CONCAT()

SELECT GROUP_CONCAT(meta_value) val_output
FROM   wp_usermeta 
WHERE  meta_key IN ('name', 'city', 'state', 'office') AND 
        user_id = '$duser->user_id'
ORDER BY FIELD(meta_key, 'name', 'city', 'state', 'office')
于 2013-09-27T19:46:07.823 回答