mercredi 24 février 2016

How to use Reflection to insert data with mysqli by bind_param method? what is difference of these two arrays?

As bind_param($types, $var1, $var2, $var3,..) method of mysqli_stmt class it gets just a series of $variables after second parameter (I want to pass array there), and the number of $variables is unknown in my case, I want to use Reflection in my insert($data) function. http://ift.tt/1TFBnWJ

I do not show the unnecessary part of my function, to avoid confusion...

public function insert($data)
{
    $types = 'sss';
    $values = array_values($data);

Removed unrelated code

    $ref    = new ReflectionClass($this->stmt);
    $method = $ref->getMethod("bind_param");

    //array_unshift($values,$types);                             1-option
    $values = array($types,'alex','alex@code.com','cats');       2-option

    $method->invokeArgs($this->stmt, $values);
    $done = $this->stmt->execute();

    $this->stmt->close();
    return $done;

}

As shown in

$method = $ref->getMethod("bind_param");

$method->invokeArgs($this->stmt, $values); 

In this part I use Reflection to pass array to second parameter of bind_param() method of $this->$stmt object.

$method->invokeArgs($this->stmt, $values); 

It doesn`t make mysqli insert into table with 1-option.

But mysqli inserts data when I use 2-option. Why? I have to use with 1-option as number of parameters is unknown.

How can I benefit from Reflection and mysqli it?

What is the difference between those two options(arrays)?





Aucun commentaire:

Enregistrer un commentaire