phpreplacesqlbuilder

Question mark placeholder


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 ) ;

output:

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

Any ideas?


Solution

  • 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;
    }