How to add special condition to bind_param?

Viewed 43

I have an array with topics and which I generate attributes to add to the query and to the bind_param:

   $arr = explode(',', $theme);
   $strMarcas  = str_repeat('?,', count($arr) - 1) . '?';
   $strTipos = str_repeat('s', count($arr));

For example usage:

   WHERE main_cover in ($strMarcas)

And in the bind_param:

   $stmt->bind_param($strTipos, ...$arr);

Now the problem is that I need to pass other conditions to the WHERE example:

   WHERE main_cover in ($strMarcas) AND language=? AND active=?

And, I don't know how to pass the conditions, I already tried this:

   $stmt->bind_param($strTipos . "si", ...$arr, $language, $active);

And, that generates this error:

Fatal error: Cannot use positional argument after argument unpacking

2 Answers

Can you do something like this?

Instead of:

$stmt->bind_param($strTipos . "si", ...$arr, $language, $active);

which errors with:

Fatal error: Cannot use positional argument after argument unpacking

Can you append to the array before you try to bind it?

$ar[] = $language;
$ar[] = $active;
$stmt->bind_param($strTipos . "si", ...$arr);

If that doesn't work, can you make a temporary array by looping through the values, then append the last 2, then bind the temporary array instead?

Why use bind_param() at all?

Just pass the array to execute()

$stmt->execute($arr);
Related