问号占位符

问题描述:

如何替换所有的?"变量?类似的东西:

How can I replace all '?' the variables? Something like:

   $name = 'test' ;
   $lname = 'lastTest';
   $id = 1 ;
   ->where ( 'customers.name = ? and customers.lastname = ? and customers.id = ?' , $name , $lname , $id ) ;

输出:

customers.name = 'test' and customers.lastname = 'lastTest' and customers.id = 1

有什么想法吗?

我真的认为你应该使用像 PDO 这样的库,但这里有一个解决方案:

I really think you should use a library like PDO, but here is a solution nonetheless:

public function where($query)
{
    $args = func_get_args();
    array_shift($args);

    $query = preg_replace_callback('/\?/', function($match) use(&$args)
    {
        return array_shift($args); // wrap in quotes and sanitize
    }, $query);

    return $query;
}