mysql问题:类型定义字符串中的元素数量与绑定变量的数量不匹配


Mysqli Problem: Number of elements in type definition string doesn't match number of bind variables

这听起来似乎很简单,但我真的卡住了。

我试着运行这个查询

$sql="insert into employers (id, username, password, email, contact_person, company_name, location_country, location_state,     location_address, website, profile, logo, subdomain, activation_key, ipaddress) values ('', ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
$params="$reg_username, $reg_password, $reg_email, $reg_contact_person, $reg_companyname, $reg_country, $reg_state, $reg_address, $reg_website, $reg_profile, $reg_logo, $reg_subdomain, $reg_activation_key, $reg_ip";
$resultobj=otherquery ($sql, "sssssssssbssss", $params);
function otherquery ($sql, $types, $params)
    {
       $connection = getConnect ();  
       $result = $connection->prepare("$sql");
       $result->bind_param($types, $params);
       $status = $result->execute();
       $result->store_result();
       $return=array('obj'=>$result, 'status' => $status, 'data'=>array());
       return $return; 
    }

,我一直得到这个错误:

Warning: mysqli_stmt::bind_param() [mysqli-stmt.bind-param]: Number of elements in type definition string doesn't match number of bind variables in /home/xyz/public_html/xyzabc.com/functions.php on line 69

我数错了什么?

谢谢

您不能使用$params来存储所有参数:bind_param()在使用$params调用时只能看到两个参数,因此出现错误。你必须直接传递它们:

$result->bind_param($types, $reg_username, $reg_password, $reg_email, $reg_contact_person, ...);

我想我已经解决了这个问题:

我将参数作为数组传递给函数,并使用call_user_func_array。

$newparams = array_merge( (array) $types, $params);
call_user_func_array(array($result, 'bind_param'), $newparams);