2

I am trying to insert values into a database using mysqli prepared statements. The values that will be inserted will vary at runtime, so I am trying to use variables instead of listing out all the parameters. I have seen that I can use call_user_func_array to achieve this, but this doesn't seem to work.

My code so far is below, albeit greatly reduced and modified for simplicity:

// in the real world, these will be set dynamically
$table = 'my_table';
$sql_fields = 'field_1,field_2,field_3,field_4';
$sql_types = 'ssss';
$sql_holders = '?,?,?,?';
$data = array('value_1', 'value_2', 'value_3', 'value_4');

$stmt = $con->prepare("INSERT INTO $table ($sql_fields) VALUES ($sql_holders)");
$params = array_merge(array($sql_types), $data);
call_user_func_array(array($stmt, "bind_param"), $params);

If I var_dump $params, I get the following.

array(5) {
    [0]=>
    string(4) "ssss"
    [1]=>
    string(7) "value_1"
    [2]=>
    string(7) "value_2"
    [3]=>
    string(7) "value_3"
    [4]=>
    string(7) "value_4"
}

This all seems okay to me, yet when I run the script, PHP crashes. I can comment out the "call_user_func_array" line and it works. Obviously nothing happens, but it doesn't crash.

I am afraid I don't know too much about my environment, except that I am using PHP 7, PhpStorm IDE and WAMP. When PHP crashes, I get the PhpStorm error "CLI has stopped working". My research tells me that this error is a PHP crashing and not the IDE, so the problem should be with my code. (incidentally, I have tried this on two machines running PhpStorm and get the same result, so this research seems to be validated)

Is anyone able to shed some light on this?

Thanks

7
  • It's worth noting that PDO makes this considerably easier since execute() takes an array argument, no fancy dancing required. Commented Feb 21, 2017 at 16:58
  • check server logs, your solution should work, need to find the error Commented Feb 21, 2017 at 17:09
  • you also have an answer waiting for you, see that. check for errors on the query and with error reporting Commented Feb 21, 2017 at 17:49
  • that answer below works. If you tried it and it still doesn't work, then you have errors somewhere and a probable indexing issue. Btw, did you leave the question only to return at a future date? You've been given comments and an answer but no response for anything. Commented Feb 21, 2017 at 17:59
  • @Roman "your solution should work" - Actually it won't work and they should have gotten the following error: Warning: Parameter 2 to mysqli_stmt::bind_param() expected to be a reference, value given.... Ivan's answer below is the solution to this. Commented Feb 21, 2017 at 18:01

1 Answer 1

3

Can you try this, to send reference to values instead of real values:

    $params = array_merge(array($sql_types), $data);
    foreach( $params as $key => $value ) {
        $params[$key] = &$params[$key];
    }
    call_user_func_array(array($stmt, "bind_param"), $params);
Sign up to request clarification or add additional context in comments.

2 Comments

That worked perfectly. I am not entirely sure why, but it worked. Thanks for your help
mine says Warning: call_user_func_array() expects parameter 1 to be a valid callback, first array member is not a valid class name or object I've used the exact code!! PHP-7, WAMP

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.