0

これはおそらく答えられている(または基本的な知識であるはずです)ので、noobの質問を許してください...

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