2

I'm building a WordPress website.

With a MySql Query I load all companies from my database that have a specific brand:

$results = $wpdb->get_results("SELECT * FROM $wpdb->postmeta WHERE meta_key = 'brand'")`

brand is an array so I search for a specific value in array:

$brand = $_GET['brand'];
$brandNeedle = $brand;

if(in_array($brandNeedle, $brand[0]))

Now, my client asked to list all users that are connected to all those companies that are selected.

So I need to create a new query. Fortunately the users all have a field added to their colum that tells me where they are working.

So I thought: what if I create a array that holds all companynames that are queried from the companylist.

With this array I can select all users who have the company name in their column

So I think the query should be something like this:

SELECT * FROM $wpdb->usermeta WHERE meta_key='company' AND meta_value ='THE ARRAY'

But this doesn't work obvious because it can't search with an array in an array.

I can't seem to figure it out. Any ideas?

-- EDIT --
Okay so I did the implode function and I do something wrong:

$brand_array = array();
for($i=0; $i <count($results); $i++){
    $result = $results[$i]; 
    if ($i % 2 == 0){
        $oddeven = 'even';
    }else{
        $oddeven = 'odd';
    }
    $post_id = $result->post_id;
    $company_name = get_post_meta($post_id, 'company_name', true); 
    $brand_array[] = $company_name;
}
    print_r($brand_array);
    $imploded = implode(',',$brand_array);

    $query = "SELECT * FROM $wpdb->usermeta WHERE meta_key='company' AND meta_value IN $imploded";
    $persons = $wpdb->get_results($query);

This gives me the results I need (the two companies in a new array) and gives me the following error:

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
'brand1,brand2' at line 1]
SELECT * FROM wp_usermeta WHERE meta_key='company' AND meta_value IN
brand1,brand2
1
  • try to combine imploding the array values then use an IN clause in your query Commented Mar 24, 2015 at 11:26

2 Answers 2

3

Have you thought about using the "IN" clause on MySQL? https://dev.mysql.com/doc/refman/5.0/en/comparison-operators.html#function_in

You would probably have something like:

$query = "SELECT * FROM ".$wpdb->usermeta." WHERE meta_key='company' AND meta_value IN ('".implode("', ', $brand)."')";

(edit: forgot to add the parenthesis around the "in" content)

Sign up to request clarification or add additional context in comments.

10 Comments

No I haven't but I like it. So I still have to create a array that holds all my brandnames? Thank you for this push in the right direction! I'll come back if it works (or not)
yes, and you join your array using the "implode" function. just remember to add quotes in your implode, otherwise the query will search for the whole string as one.
it seems that you forgot the quotes: SELECT * FROM wp_usermeta WHERE meta_key='company' AND meta_value IN 'brand1','brand2' just remember to add the quotes in the join as well, otherwise it will search as just one big string.
try like this: ` $imploded = implode("','",$brand_array); $query = "SELECT * FROM $wpdb->usermeta WHERE meta_key='company' AND meta_value IN '".$imploded."'"; `
What join??? LOL I'm over my head here, so please bare with me. I add the quotes in the implode() like this implode("','",$brand_array); and where do I join this?
|
0

Search for mysql for IN clause

SELECT * FROM $wpdb->usermeta WHERE meta_key='company' AND meta_value IN implode(',',$brand_array)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.