gpt4 book ai didi

MySql 查询中的 PHP 变量

转载 作者:行者123 更新时间:2023-11-29 01:00:13 24 4
gpt4 key购买 nike

好吧,我很抱歉我无法理解这里。我不是那种依赖这样的地方的人,非常感谢任何帮助我提高 PHP 和 MySQL 知识的人!

我有这个查询,调用它来收集有关在我们的 wordpress 网站上注册的用户的信息。

$verified = $wpdb->get_results("SELECT wp_users.ID, wp_users.user_login, wp_users.user_nicename, wp_users.user_email, m1.meta_value AS first_name, m2.meta_value AS last_name, m3.meta_value AS address, m4.meta_value AS date_of_birth_ddmmyyyy, m5.meta_value AS do_you_consider_yourself_as_having_a_disability, m6.meta_value AS hearing_ability, m7.meta_value AS ethnic_origin, m8.meta_value AS sex, m9.meta_value AS mobile_number, m10.meta_value AS telephone_number, m11.meta_value AS postcode, m12.meta_value AS towncity, m13.meta_value AS county, m14.meta_value AS who_is_paying_for_the_course, m15.meta_value AS course_type, m16.meta_value AS start_date, m17.meta_value AS course_times, m18.meta_value AS duration, m19.meta_value AS venue, m20.meta_value AS price, m21.meta_value AS code
FROM wp_users
INNER JOIN wp_usermeta m ON wp_users.id = m.user_id
AND m.meta_key = 'wp_user_level'
AND m.meta_value =0
LEFT JOIN wp_usermeta m1 ON wp_users.id = m1.user_id
AND m1.meta_key = 'first_name'
LEFT JOIN wp_usermeta m2 ON wp_users.id = m2.user_id
AND m2.meta_key = 'last_name'
LEFT JOIN wp_usermeta m3 ON wp_users.id = m3.user_id
AND m3.meta_key = 'address'
LEFT JOIN wp_usermeta m4 ON wp_users.id = m4.user_id
AND m4.meta_key = 'date_of_birth_ddmmyyyy'
LEFT JOIN wp_usermeta m5 ON wp_users.id = m5.user_id
AND m5.meta_key = 'do_you_consider_yourself_as_having_a_disability'
LEFT JOIN wp_usermeta m6 ON wp_users.id = m6.user_id
AND m6.meta_key = 'hearing_ability'
LEFT JOIN wp_usermeta m7 ON wp_users.id = m7.user_id
AND m7.meta_key = 'ethnic_origin'
LEFT JOIN wp_usermeta m8 ON wp_users.id = m8.user_id
AND m8.meta_key = 'sex'
LEFT JOIN wp_usermeta m9 ON wp_users.id = m9.user_id
AND m9.meta_key = 'mobile_number'
LEFT JOIN wp_usermeta m10 ON wp_users.id = m10.user_id
AND m10.meta_key = 'telephone_number'
LEFT JOIN wp_usermeta m11 ON wp_users.id = m11.user_id
AND m11.meta_key = 'postcode'
LEFT JOIN wp_usermeta m12 ON wp_users.id = m12.user_id
AND m12.meta_key = 'towncity'
LEFT JOIN wp_usermeta m13 ON wp_users.id = m13.user_id
AND m13.meta_key = 'county'
LEFT JOIN wp_usermeta m14 ON wp_users.id = m14.user_id
AND m14.meta_key = 'who_is_paying_for_the_course'
LEFT JOIN wp_usermeta m15 ON wp_users.id = m15.user_id
AND m15.meta_key = 'course_type'
LEFT JOIN wp_usermeta m16 ON wp_users.id = m16.user_id
AND m16.meta_key = 'start_date'
LEFT JOIN wp_usermeta m17 ON wp_users.id = m17.user_id
AND m17.meta_key = 'course_times'
LEFT JOIN wp_usermeta m18 ON wp_users.id = m18.user_id
AND m18.meta_key = 'duration'
LEFT JOIN wp_usermeta m19 ON wp_users.id = m19.user_id
AND m19.meta_key = 'venue'
LEFT JOIN wp_usermeta m20 ON wp_users.id = m20.user_id
AND m20.meta_key = 'price'
LEFT JOIN wp_usermeta m21 ON wp_users.id = m21.user_id
AND m21.meta_key = 'code'
WHERE user_login NOT LIKE '%unverified__%'
ORDER BY course_type ASC");

现在我知道查询本身并不理想。但是我想做的是使用变量或其他可以节省文件大小(也可以节省系统资源?)的方式更改 ORDER BY 值,这可能吗?

例如,如果这是我想要实现的(即使它是错误的)

$verified = $wpdb->get_results("SELECT wp_users.ID, wp_users.user_login, wp_users.user_nicename, wp_users.user_email, m1.meta_value AS first_name, m2.meta_value AS last_name, m3.meta_value AS address, m4.meta_value AS date_of_birth_ddmmyyyy, m5.meta_value AS do_you_consider_yourself_as_having_a_disability, m6.meta_value AS hearing_ability, m7.meta_value AS ethnic_origin, m8.meta_value AS sex, m9.meta_value AS mobile_number, m10.meta_value AS telephone_number, m11.meta_value AS postcode, m12.meta_value AS towncity, m13.meta_value AS county, m14.meta_value AS who_is_paying_for_the_course, m15.meta_value AS course_type, m16.meta_value AS start_date, m17.meta_value AS course_times, m18.meta_value AS duration, m19.meta_value AS venue, m20.meta_value AS price, m21.meta_value AS code
FROM wp_users
INNER JOIN wp_usermeta m ON wp_users.id = m.user_id
AND m.meta_key = 'wp_user_level'
AND m.meta_value =0
LEFT JOIN wp_usermeta m1 ON wp_users.id = m1.user_id
AND m1.meta_key = 'first_name'
LEFT JOIN wp_usermeta m2 ON wp_users.id = m2.user_id
AND m2.meta_key = 'last_name'
LEFT JOIN wp_usermeta m3 ON wp_users.id = m3.user_id
AND m3.meta_key = 'address'
LEFT JOIN wp_usermeta m4 ON wp_users.id = m4.user_id
AND m4.meta_key = 'date_of_birth_ddmmyyyy'
LEFT JOIN wp_usermeta m5 ON wp_users.id = m5.user_id
AND m5.meta_key = 'do_you_consider_yourself_as_having_a_disability'
LEFT JOIN wp_usermeta m6 ON wp_users.id = m6.user_id
AND m6.meta_key = 'hearing_ability'
LEFT JOIN wp_usermeta m7 ON wp_users.id = m7.user_id
AND m7.meta_key = 'ethnic_origin'
LEFT JOIN wp_usermeta m8 ON wp_users.id = m8.user_id
AND m8.meta_key = 'sex'
LEFT JOIN wp_usermeta m9 ON wp_users.id = m9.user_id
AND m9.meta_key = 'mobile_number'
LEFT JOIN wp_usermeta m10 ON wp_users.id = m10.user_id
AND m10.meta_key = 'telephone_number'
LEFT JOIN wp_usermeta m11 ON wp_users.id = m11.user_id
AND m11.meta_key = 'postcode'
LEFT JOIN wp_usermeta m12 ON wp_users.id = m12.user_id
AND m12.meta_key = 'towncity'
LEFT JOIN wp_usermeta m13 ON wp_users.id = m13.user_id
AND m13.meta_key = 'county'
LEFT JOIN wp_usermeta m14 ON wp_users.id = m14.user_id
AND m14.meta_key = 'who_is_paying_for_the_course'
LEFT JOIN wp_usermeta m15 ON wp_users.id = m15.user_id
AND m15.meta_key = 'course_type'
LEFT JOIN wp_usermeta m16 ON wp_users.id = m16.user_id
AND m16.meta_key = 'start_date'
LEFT JOIN wp_usermeta m17 ON wp_users.id = m17.user_id
AND m17.meta_key = 'course_times'
LEFT JOIN wp_usermeta m18 ON wp_users.id = m18.user_id
AND m18.meta_key = 'duration'
LEFT JOIN wp_usermeta m19 ON wp_users.id = m19.user_id
AND m19.meta_key = 'venue'
LEFT JOIN wp_usermeta m20 ON wp_users.id = m20.user_id
AND m20.meta_key = 'price'
LEFT JOIN wp_usermeta m21 ON wp_users.id = m21.user_id
AND m21.meta_key = 'code'
WHERE user_login NOT LIKE '%unverified__%'
ORDER BY " . $MYVARIABLE ."ASC");

$MYVARIABLE = 'my_order_by_column_name';

我确实在某处读到过您可以使用“ORDER BY 条款”,但我不确定它是否可以这样使用???

非常感谢任何帮助!

克雷格

最佳答案

就将变量放入查询而言,您所拥有的看起来是正确的。但是,您需要在执行查询之前定义变量。

$MYVARIABLE = 'my_order_by_column_name';

$verified = $wpdb->get_results("SELECT...ORDER BY $MYVARIABLE ASC");

如果 $MYVARIABLE 的值来自用户输入,我会建议创建一个允许用于排序的列数组,并验证 $MYVARIABLE 的值是这些列之一。

// columns that can be used for sorting
$sortable = array('column1', 'column2', 'column3');

// make sure its a valid column, if not use the first sortable column as a default
if (!in_array($MYVARIABLE, $sortable)) $MYVARIABLE = $sortable[0];

$verified = $wpdb->get_results("SELECT... ORDER BY $MYVARIABLE ASC");

关于MySql 查询中的 PHP 变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3821131/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com