2

I am ignorant to what is happening!

Here is the error:

Warning: Parameter 2 to mysqli_stmt::bind_param() expected to be a reference, value given in C:\xxxx\htdocs\xxxxx\xxxxx\class.mysql.php on line 51
Fatal error: Uncaught exception 'ReflectionException' with message 'Invocation of method mysqli_stmt::bind_param() failed' in C:\xampp\htdocs\assemblyx.net\class\class.mysql.php:51 Stack trace: #0 C:\xxxx\htdocs\xxxxx\xxxxx\class.mysql.php(51): ReflectionMethod->invokeArgs(Object(mysqli_stmt), Array) #1 C:\xxxx\htdocs\xxxxx\xxxxx\index.php(17): mysql->Insert('test', Array) #2 {main} thrown in C:\xxxx\htdocs\xxxxx\xxxxx\class.mysql.php on line 51

Code in index.php:

$a=array();
$a[0]=array();
$a[0]["test_value"]="helloMysqli";
$a[0]["test_int"]="2";
$a[0]["test_dec"]="3.1";
$db->Insert("test", $a);

Method in class.mysql.php:

function Insert($table, $arrInsert){
 $types = $cols = $q = "";
 $value = array();
 foreach($arrInsert as $k => $v){
 foreach($v as $k2 => $v2){
 $cols .= ($cols=="")?$k2:", ".$k2;
 $q .= ($q=="")?"?":", ?";
 $value[] = $v2;
 $types .= substr(gettype($v2),0,1);
 }
 }
 $sql = "insert into ".$table." (".$cols.") value (".$q.");";
 $stmt = $this->prepare($sql);
 $refArr = array();
 $refArr[0] = $types;
 foreach($value as $k => $v){
 $refArr[] = $v;
 }
 $ref = new ReflectionClass('mysqli_stmt'); 
 $method = $ref->getMethod("bind_param");
 $method->invokeArgs($stmt,$refArr); 
 $stmt->execute();
}

Now if I code the array into the Insert method every works fine:

function Insert($table, $arrInsert){
 $types = $cols = $q = "";
 $value = array();
 foreach($arrInsert as $k => $v){
 foreach($v as $k2 => $v2){
 $cols .= ($cols=="")?$k2:", ".$k2;
 $q .= ($q=="")?"?":", ?";
 $value[] = $v2;
 $types .= substr(gettype($v2),0,1);
 }
 }
 $sql = "insert into ".$table." (".$cols.") value (".$q.");";
 $stmt = $this->prepare($sql);
 $refArr = array();
 $refArr[] = $types;
 foreach($value as $k => $v){
 $refArr[] = $v;
 }
//****************
// added array here
//***************** 
 $refArr = array();
 $refArr[] = "sss";
 $refArr[] = "helloMysqli";
 $refArr[] = "2";
 $refArr[] = "3.1";
 $ref = new ReflectionClass('mysqli_stmt'); 
 $method = $ref->getMethod("bind_param");
 $method->invokeArgs($stmt,$refArr); 
 $stmt->execute();
}

I am obviosly not understanding how the array is being referenced. Please someone enlighten me!

Here are the print_r outputs to show that both arrays are outputting the same:

 function Insert($table, $arrInsert){
 $types = $cols = $q = "";
 $value = array();
 foreach($arrInsert as $k => $v){
 foreach($v as $k2 => $v2){
 $cols .= ($cols=="")?$k2:", ".$k2;
 $q .= ($q=="")?"?":", ?";
 $value[] = $v2;
 $types .= substr(gettype($v2),0,1);
 }
 }
 $sql = "insert into ".$table." (".$cols.") value (".$q.");";
 $stmt = $this->prepare($sql);
 $refArr = array();
 $refArr[] = $types;
 foreach($value as $k => $v){
 $refArr[] = $v;
 }
 print_r($refArr);
 exit;
// print_r output : Array ( [0] => sss [1] => helloMysqli [2] => 2 [3] => 3.1 )
 $refArr = array();
 $refArr[] = "sss";
 $refArr[] = "helloMysqli";
 $refArr[] = "2";
 $refArr[] = "3.1";
// print_r output : Array ( [0] => sss [1] => helloMysqli [2] => 2 [3] => 3.1 )
 $ref = new ReflectionClass('mysqli_stmt'); 
 $method = $ref->getMethod("bind_param");
 $method->invokeArgs($stmt,$refArr); 
 $stmt->execute();
 }
asked Apr 25, 2014 at 15:14
7
  • If you do a print_r($value); prior to the foreach() loop foreach($value as $k => $v){.... what are you seeing? It would be nice to know that you did some kind of error checking prior to just tossing a question up here. Commented Apr 25, 2014 at 15:38
  • I did a var_dump on both arrays, they both returned the exact same value, is var_dump equivalent to print_r? Commented Apr 25, 2014 at 16:17
  • print_r is "human readable", but what comes back form those arrays? It could be that your foreach loop is incorrect but again, we can't help without knowing what you are seeing. Commented Apr 25, 2014 at 16:20
  • I have tested this over and over, they both bring back the exact same result, just did a print_r : Array ( [0] => sss [1] => helloMysqli [2] => 2 [3] => 3.1 ) Commented Apr 25, 2014 at 16:27
  • What does $refArr look like after $refArr[] = $types; and again after the foreach loop where you also try and add the values from $values to it, and also, what does $values look like before the foreach loop? Commented Apr 25, 2014 at 16:30

1 Answer 1

1

When I add & in the forloop to look like this it works:

foreach($value as $k => $v){$refArr[] = &$value[$k];}

answered May 22, 2014 at 16:27

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.