1

i'm having trouble using call_user_func_array in this code

$stmt=$mysqli->prepare($query);
$txt=array('ii',1,1);
call_user_func_array(array($stmt,"bind_param"),$txt);
$stmt->execute();
$result=$stmt->get_result();
while ($row = $result->fetch_array(MYSQLI_ASSOC)){
    $r[$count]=$row;
    $count++;
}

Using the $txt variable gives an error with fetch_array():

Call to a member function fetch_array() on boolean

But, if i create the array in the code it works fine. Example:

...
call_user_func_array(array($stmt,"bind_param"),array('ii',1,1));
...
Luciano
  • 55
  • 6
  • 3
    Also: ["Care must be taken when using mysqli_stmt_bind_param() in conjunction with call_user_func_array(). Note that mysqli_stmt_bind_param() requires parameters to be passed by reference, whereas call_user_func_array() can accept as a parameter a list of variables that can represent references or values."](http://php.net/manual/en/mysqli-stmt.bind-param.php) - per mysqli::bind_param, YMMV – user2864740 Dec 30 '17 at 23:13
  • or just use [argument unpacking](http://php.net/manual/en/migration56.new-features.php) – Lawrence Cherone Dec 30 '17 at 23:16
  • 1
    @user2864740 the problem i get is "Call to a member function fetch_array() on boolean" – Luciano Dec 30 '17 at 23:18
  • That's because `bind_param` must receive parameters passed by reference not by value, so you need a little trick to make it work. – Havenard Dec 30 '17 at 23:19
  • @Havenard i tried passing parameters by reference but still doesn't work – Luciano Dec 30 '17 at 23:24
  • @LawrenceCherone sorry i forgot to post it. I updated the post with the $result variable initialization – Luciano Dec 30 '17 at 23:28
  • 1
    Hmmm, if only there was something like PDO, where you could do all of this in three lines of code.... – mario Dec 30 '17 at 23:29

1 Answers1

0

If course it would be nice to know what kind of a query is involved, but in any event the following code works:

<?php
error_reporting(E_ALL);
$id=5;
$code = 'GR';
$country='Greece';

$mysqli = new mysqli("localhost", "root", "", "exp");
if ($mysqli->connect_errno) {
    echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . 

$mysqli->connect_error;
}
echo $mysqli->host_info . "\n";

if (!($stmt = $mysqli->prepare("INSERT INTO countries VALUES (?,?,?)"))) {
    echo "Prepare failed: (" . $mysqli->errno . ") " . $mysqli->error;
}

//$stmt->bind_param('iss', $id, $code, $country);

$params = array('iss',$id,$code,$country);
$tmp = array();
foreach($params as $key => $value) $tmp[$key] = &$params[$key];

call_user_func_array( array($stmt, 'bind_param'), $tmp);


if (!$stmt->execute()) {
    echo "Execute failed: (" . $stmt->errno . ") " . $stmt->error;
}

The code only works with my software when the $params array includes the letters specifying the type of data. Also the params themselves need to be references and not values. Note: I did borrow the code to turn values into references from this member of Stackoverflow.

slevy1
  • 3,797
  • 2
  • 27
  • 33