views:

70

answers:

1

Okay I do apologise that I'm out of my depth here. I'm not the kind of person to rely on places like this and any help towards improving my PHP & MySQL knowledge is greatly appreciated!

I have this query which is called to collect information about users who are registered on our wordpress site.

$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");

Now I know that query in itself isn't ideal. But what I'd like to do is change the ORDER BY value using a variable or some other way which would save file size (system resources too?), would this be possible?

For example if I this is what I want to achieve (even though it's wrong)

$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';

I did read somewhere that you can have a "ORDER BY Clause" but I'm not sure if this is how it can be used???

Any help is much appreciated!

Craig

A: 

What you have as far as putting the variable in the query looks correct. However, you'll need to define the variable before the query is executed..

$MYVARIABLE = 'my_order_by_column_name';

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

If the value of $MYVARIABLE is coming from user input, I would suggest creating an array of columns that are allowed to be used for sorting and verify that the value of $MYVARIABLE is one of those columns.

// 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");
Chris Gutierrez
Thanks Chris... Silly me I had it right all along I just missed a space - DohI'll have a look at this array suggestion - Thanks very much!
Craig