There is MySQL query with unknown number of parameters (dynamically built) of the following format:
SELECT * FROM actions WHERE user1 = ? AND user10 = ? AND user11 = ? AND time = :time
Since I am using PDO prepared statement, I can't mix named and positional parameters in one query. So I need to repalce all question marks with named parameters sequentialy. I have an Array() that holds these parameters:
$parameters = Array();
$parameters['time'] = 1234567;
Now I need to replace every question mark with a sequential named parameter, so the query will look like this:
SELECT * FROM actions WHERE user1 = :user0 AND user10 = :user1 AND user11 = :user2 AND time = :time
And $parameters would contain every named parameter in it. I need to find every " ? " in the query string and looping through the occurences, replace them with an incrementing string. In JavaScript I would use a regex to find ? and pass them to a function, which would have a global incrementing variable to track the current named parameter number, but I have no idea how to do this in PHP.
P.S. Besides replacing them sequentially, I would also need to add parameters to array:
$parameters['user0'] = $user;
$parameters['user1'] = $user;
$parameters['user2'] = $user;
SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens?and named parameters, which is 'illegal' format. I only need to change?to named parameters in a loop.