我正在尝试通过准备查询并执行的函数传递准备好的查询,但我不断收到“Number of variables doesn't match number of parameters in prepared statement
”。
我无法弄清楚什么是错的,因为我有与参数相同数量的变量。
查询:
UPDATE `users` SET fullname = ?, telephone = ?, email = ?, company_name = ?, company_addr2 = ?, company_town = ?, company_postcode = ?, sra_registration = ?, company_number = ?, vat_registration = ?, company_website = ?, list_of_partners = ?, my_staff = ? WHERE id = ?
PARAMS:
Array
(
[0] => Ben Shepherd
[1] => 071111111111
[2] => john.doe@domain.com
[3] => Company name
[4] => addr2
[5] => town
[6] => postcode
[7] => 123456
[8] => 123456
[9] => 123465
[10] => http://www.somewebsite.com
[11] => United Kingdom
[12] => "[{\\\"staff_firstname\\\":\\\"John\\\",\\\"staff_surname\\\":\\\"Smith\\\",\\\"staff_email\\\":\\\"some@email.com\\\"},{\\\"staff_firstname\\\":\\\"Jane\\\",\\\"staff_surname\\\":\\\"Smith\\\",\\\"staff_email\\\":\\\"some2@email.com\\\"},{\\\"staff_firstname\\\":\\\"John 3\\\",\\\"staff_surname\\\":\\\"Smith 3\\\",\\\"staff_email\\\":\\\"john.smith3@email.com\\\"}]"
[13] => 1
)
查询功能:
public static function query($query, $params = array())
{
$params = !is_array($params) ? array($params) : $params;
$data = array();
$stmt = self::$i->prepare($query);
if(!$stmt)
return false;
foreach($params as $value)
{
/* Bind parameters. Types: s = string, i = integer, d = double, b = blob */
switch( true )
{
case (is_double($value)):
$type = 'd';
break;
case (is_int($value)):
$type = 'i';
break;
default:
case (is_string($value)):
$type = 's';
break;
}
$stmt->bind_param($type, $value);
}
$stmt->execute();
$res = $stmt->get_result();
if(!$res) return false;
while($row = $res->fetch_array(MYSQLI_ASSOC) )
{
$data[] = $row;
}
self::$customError = "";
return $data;
}
答案 0 :(得分:1)
尝试将所有参数放入一个bind_param
来电。
$stmt->bind_param($type, $value);
移出foreach循环
<?php
public static function query($query, $params = array())
{
$params = !is_array($params) ? array($params) : $params;
...
$types = array();
foreach($params as $value)
{
/* Bind parameters. Types: s = string, i = integer, d = double, b = blob */
switch( true )
{
case (is_double($value)):
$type = 'd';
break;
case (is_int($value)):
$type = 'i';
break;
default:
case (is_string($value)):
$type = 's';
break;
}
$types .= $type;
}
call_user_func_array(array($stmt, 'bind_param'), array_merge(array($types), $params));