开发者

PHP and MySQL -Replacing N delimeters with N values from an array

I've been reading a PHP book on design patterns and would like to incorporate one of their database patterns in my code. Specifically,the class method returns an an array with two elements within it.

The first element is a string looks like this.

"SELECT col1, col2, col3 FROM table WHERE col1 = ?, col2= ?"

The second element is an array which contains an array of string values (in this case 1 and 2)

I am looking for the most efficient way(it will be used quite often to instantiate objects) to merge these two arrays on the "?" delimiter so that the end result is a string which = "SELECT col1, col2, col3 FROM table WHERE col1=1, col2=2"

I've seen stuff such开发者_Python百科 as preg_replace, but that relies on a fixed N array of patterns. I'm looking for something a little more dynamic. I will use the output of the code in a PDO select statement. Note that the above is a specific example, but that I need it to work for arbitrary amount of ? and inputs

Here is the code

//This function returns an array of two elements, $string_ar[0] is the string, $string_ar[1] are the values
$string_ar = $selectionFactory->doNewSelection($friend_idobj,'has_friend');

Thanks everyone!


First of all I would like to inform you about PHP's PDO extension that has support for, and it's focused on, prepared statements and even emulates them for database engines that do not support them.

Secondly, if you want to handle this without prepared statements, you should take a look into strtok, or even explode may do it if you don't have a very complex logic.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜