This is my class code:
class DAL
{
public function paramtypez($val)
{
$types = ''; //initial sting with types
foreach($val as $para)
{
if(is_int($para)) {
$types .= 'i'; //integer
} elseif (is_float($para)) {
$types .= 'd'; //double
} elseif (is_string($para)) {
$types .= 's'; //string
} else {
$types .= 'b'; //blob and unknown
}
}
return $types;
}
public function execsql($query,$param)
{
$conn=new mysqli("127.0.0.1","root","","sample");
if($row=$conn->prepare($query))
{
array_unshift($param,array(DAL::paramtypez($param)));
call_user_func_array(array($row, 'bind_param'), $param);
$row->execute();
return true;
}
else
return false;
}
}
and is use this class like this:
$vars = new DAL();
$vars->execsql('insert into tesst (name,family,age) values(?,?,?)',array('mori','gre','25'));
but that's return me this error:
Warning: Parameter 2 to mysqli_stmt::bind_param() expected to be a reference, value given in ..\htdocs\inc.php on line 30
how can fix this error? thanks for any help
This is happening because you are giving an array when bind_param expects an actual reference:
Try and see if this works:
class DAL
{
public function paramtypez($val)
{
$types = ''; //initial sting with types
foreach($val as $para)
{
if(is_int($para)) {
$types .= 'i'; //integer
} elseif (is_float($para)) {
$types .= 'd'; //double
} elseif (is_string($para)) {
$types .= 's'; //string
} else {
$types .= 'b'; //blob and unknown
}
}
return $types;
}
public function execsql($query,$param)
{
$conn=new mysqli("127.0.0.1","root","","sample");
if($row=$conn->prepare($query))
{
array_unshift($param,array(DAL::paramtypez($param)));
call_user_func_array(array($row, 'bind_param'), $this->makeValuesReferenced($param));
$row->execute();
return true;
}
else
return false;
}
protected function makeValuesReferenced(&$arr)
{
$refs = array();
foreach($arr as $key => $value)
$refs[$key] = &$arr[$key];
return $refs;
}
}
$param
looks like this right before you pass it to call_user_func_array:
[['ssi'],'mori','gre','25']
This is incorrect. You should change this:
array_unshift($param,array(DAL::paramtypez($param)));
To this:
array_unshift($param,DAL::paramtypez($param));
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.