1

So I want to match countrycodes with regions.

if ($region == 'eu') {
    $country = 'nl', 'be'; etc.. etc..
} 

Then I want to get all the users WHERE countrycode is one of the above.

$countryGet = $db->prepare("SELECT countrycode FROM `users` WHERE countrycode = :countrycode DESC LIMIT 100");
$countryGet->execute(array(
    ':countrycode' => $country,
));

Maybe I'm thinking to complicated but I cant figure out a nice way to do this. Thanks in advance.

1
  • 1
    You could use a combination of in_array() and IN() or FIND_IN_SET() depending on how your DB is setup, if it isn't normalized. Commented Jul 25, 2014 at 14:27

1 Answer 1

3

There is no nice way to do this. It is an unfortunate price we have to pay for the safety of prepared statements.

Try this:

if( $region == "eu") {
    $countries = array("nl","be",...);
}

Then:

$sql = "
    SELECT countrycode
    FROM users
    WHERE countrycode IN (".implode(",",array_fill(0,count($countries),"?")).")
";
$countryGet = $db->prepare($sql);
$countryGet->execute($countries);

It gets even messier if you have additional parameters, but this should give you the idea.

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

2 Comments

Thanks! Got it working with your help. It looks a bit messy indeed but I understand it and it works. Thanks again!
@Terry Glad I could help :) Might be a good idea to comment the code to explain what it's doing, so you remember when you come back to it. Better still, make a function out of it ^_^

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.