0
,以显示他们这大概已经回答了(或者应该是基础知识),所以请原谅noob问题...数据库查询retuyrns领域的一个订单,我想在另一个
我使用的WP user_meta数据库来存储用户的高级属性。
数据存储在这个顺序(这是所有用户ID = 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的instrucitons后,我还是百思不得其解....
而且后续: 这是当前的代码
$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
仍然抓我的头就这一个...我张贴新的代码和错误消息的原始邮件。在查看您提供的链接以及与GROUP_CONCAT –
相关的其他几项搜索后,我仍然感到困惑,其中“GROUP_CONCAT”之前的“SELECT”关键字在哪里? –
'SELECT GROUP_CONCAT(meta_value)val_output ........' –