1

I'm looking for the best way to do what this probably can't do until the future:

// Only one of these, different each time
$arg_string = "'arg1', 'arg2', 'arg3'";         // Unknown number of arguments!
$arg_string = "'arg1', 'arg2', 'arg3', 'arg4'"; // Unknown number of arguments!
$arg_string = "'arg1'";                         // Unknown number of arguments!

// Where I need help
$stmt->execute([$arg_string]); // Wish I could, maybe PHP 9 will

My problem is that a $string or $array can't be easily passed to a function() as its parameters.

This is important for my work because I am creating a method to query the database and I may have an unknown number of columns or values. This is what the code will look like just before using exec().

I found this SO article (PHP use variable to pass multiple arguments to function) which discusses call_user_func_array(). But, I can't figure out how to use call_user_func_array() with execute(). Even if I were to guess, I still would want to know what is best.

I don't mind turning $arg_string into an array first, or not, whatever is best.

What is the best way to do what this can't:

$stmt->execute([$arg_string]);
3
  • 2
    Don't use a string, use an array that you fill dynamically. Commented Jul 31, 2021 at 11:15
  • @Barmar can you demonstrate what that would look like? Commented Jul 31, 2021 at 11:17
  • 2
    The manual has some pretty clear examples: php.net/manual/en/pdostatement.execute.php (which is always a good place to start when in doubt) Commented Jul 31, 2021 at 11:19

2 Answers 2

2

You don't need a string, each arg should be a separate array element.

$arg_array = ['arg1']; // or
$arg_array = ['arg1', 'arg2']; // or
$arg_array = ['arg1', 'arg2', 'arg3'];

$stmt->execute($arg_array);
Sign up to request clarification or add additional context in comments.

3 Comments

To accept this answer, I need you to demonstrate starting with $arg_string and how you got it into the array to use. I have already been using explode() and others to no avail.
I think you need to go back a step. Why do you need $arg_string in the first place? If you're creating everything dynamically, do it by building $arg_array from what you need.
Or use a parsable string like JSON.
-1

From PDOStatement::execute | php.net:

// Only one of these, different each time
$arg_string = "'arg1', 'arg2', 'arg3'";         // Unknown number of arguments!
$arg_string = "'arg1', 'arg2', 'arg3', 'arg4'"; // Unknown number of arguments!
$arg_string = "'arg1'";                         // Unknown number of arguments!

// Where I need help
$arg_array = explode(",", str_replace(["'", ' '], '', $arg_string)); // My solution
$stmt->execute([$arg_array]);

3 Comments

Is this an answer or an addition to your question? Are you asking how to explode the string into an array?
You don't want spaces or single quotes in your array, so use $arg_array = explode(",", str_replace(["'", ' '], '', $arg_string));
@brombeer it is a way to get $arg_string into the explode() method as the question seeks. I found the right answer.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.