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();
}
AssemblyXAssemblyX
asked Apr 25, 2014 at 15:14
1 Answer 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
lang-php
print_r($value);
prior to theforeach()
loopforeach($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.var_dump
on both arrays, they both returned the exact same value, isvar_dump
equivalent toprint_r
?print_r
is "human readable", but what comes back form those arrays? It could be that yourforeach
loop is incorrect but again, we can't help without knowing what you are seeing.print_r
:Array ( [0] => sss [1] => helloMysqli [2] => 2 [3] => 3.1 )
$refArr
look like after$refArr[] = $types;
and again after theforeach
loop where you also try and add the values from$values
to it, and also, what does$values
look like before theforeach
loop?