Php 数据库查询retuyrns字段按一种顺序显示,我想按另一种顺序显示它们
这可能已经得到了回答(或者应该是基础知识),所以请原谅noob的问题 我正在使用WP user_元数据库来存储用户的高级属性 数据按此顺序存储(这是userid=15的全部内容): 以下是相关的PHP代码:Php 数据库查询retuyrns字段按一种顺序显示,我想按另一种顺序显示它们,php,mysql,Php,Mysql,这可能已经得到了回答(或者应该是基础知识),所以请原谅noob的问题 我正在使用WP user_元数据库来存储用户的高级属性 数据按此顺序存储(这是userid=15的全部内容): 以下是相关的PHP代码: $my_exportlist = $wpdb->get_results("SELECT `user_id` FROM `wp_m_membership_relationships` WHERE `level_id` = '2' "); foreach ($my_exportlist
$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
使用GROUP\u CONCAT()
GROUP_CONCAT
之前的SELECT
关键字在哪里?好的,已经解决了。当然会忘记一个字!!现在我只需要弄清楚如何将val_输出数组转储到csv输出中…感谢您的帮助!唉…45分钟后,我再也没有了…提示我应该做什么?val_输出
默认用逗号分隔。你想用它做什么?
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
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')