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

时间:2011-09-14 11:25:17

标签: php mysql mysqli

现在这听起来似乎很简单,但我确实陷入了困境。

我尝试运行此查询

$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

我不正确的是什么?

由于

2 个答案:

答案 0 :(得分:0)

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

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

答案 1 :(得分:0)

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

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

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