0
$option_meno = ["Lukas", "Ivka"];
$sql = "SELECT * 
        FROM uctovnictvo 
        WHERE meno IN ('$option_meno[1]', '$option_meno[0]')
          AND datum BETWEEN '$date_start' AND '$date_end'
        ORDER BY $order ";

For sure there has to be a better way how to select user based on name (meno). There can be more or fewer names in the $option_meno array.

I would like to make especially this more simple than listing out each index in the option array ('$option_meno[1]','$option_meno[0]').

2 Answers 2

2

You could use some array functions to auto generate the correct IN statement

$option_meno = ["Lukas","Ivka"];
$in = implode(',', array_map(function($item) use ($pdo) {
    return '"' . $pdo->quote($item) . '"';
}, $option_meno);

$sql = "SELECT * FROM uctovnictvo WHERE meno IN ($in)...";

instead of PDO::quote you could use also mysqli_real_escape_string, etc. (depends on your connection).

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

Comments

1

You can use implode() :

$sql = " SELECT * 
         FROM uctovnictvo 
         WHERE meno IN ('" . implode('\', \'', $option_meno) . "') 
           AND datum BETWEEN '$date_start' AND '$date_end'  
         ORDER BY $order ";

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.