问号占位符

1 php replace sqlbuilder

我怎样才能更换所有'?' 变量?就像是:

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

输出:

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

有任何想法吗?

Tim*_*per 5

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

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;
}
Run Code Online (Sandbox Code Playgroud)