type definition string doesn't match number of bind variables [duplicate]

拟墨画扇 提交于 2019-12-23 20:23:17

问题


I use a dynamic database class I constructed for all my projects. Just started with a new one, and the class is giving me a hiccup. This is my code, not the details. But I recreated the error through minimal code.

function vref($arr) {
  if (strnatcmp(phpversion(),'5.3') >= 0) {//Reference is required for PHP 5.3+
    $refs = array();
    foreach($arr as $key => $value) $refs[$key] = &$arr[$key];
    return $refs;
  }
  return $arr;
}

$bind = 's,i,i,i,i, i,s,s,s,s, i,s,s';
$reward = ( $data['reward'] ) ? $data['reward'] : '0';
$special = '0';
$icon = '0';
$mastery = ( @$data['mastery'] ) ? 'Yes' : 'No';
$added = time();
settype($added, 'string')
$arr = array(
        $bind,
        $data['name'],
        intval($data['cost']),
        intval($data['per']),
        intval($data['serv']),
        intval($data['earns']),
        intval($data['cp']),
        $data['cookTime'],
        $reward,
        $special,
        $icon,
        intval($data['type']),
        $mastery,
        $added );

$db = new mysqli(...);
$stmt = $db2->prepare('INSERT INTO recipe2 (
     `name`,cost,perserv,servings,earns,
     cp,cooktime,`unlock`,special,icon,
     `type`,options,added) 
   VALUES (?,?,?,?,?, ?,?,?,?,?, ?,?,?) ');
call_user_func_array( array( $stmt, 'bind_param' ), vref($arr) );

// var_dump($arr)
array(14) {
[0]=> string(25) "s,i,i,i,i,i,s,s,s,s,i,s,s"
[1]=> string(18) "Bacon Cheeseburger"
[2]=> int(15)
[3]=> int(4)
[4]=> int(13)
[5]=> int(56)
[6]=> int(6)
[7]=> string(2) "5m"
[8]=> string(1) "0"
[9]=> string(1) "0"
[10]=>int(0)
[11]=>int(0)
[12]=>string(2) "No"
[13]=>string(10) "1325300795"
}

// Database Column listing
name        varchar(255)            
cost        int(10)
perserv     int(5)
servings    int(10)
earns       int(10)
cp      int(10)
cooktime    varchar(11)
unlock      varchar(255)
special     varchar(255)
icon        varchar(255)
type        int(5)
options     varchar(255)
added       varchar(12)

Error: Warning: mysqli_stmt::bind_param(): Number of elements in type definition string doesn't match number of bind variables

And before anyone just simply says, count your variables. I have. Probably over 50 times now. No joke. Before an hour ago, I started using the variables, instead of direct data being put into that. vref() simply passes all the items in the array as reference variables. I have since learned that references are not to be taken lightly. Heh. But I still can't figure this out, and its frusterating.

I have 13 columns. 14 parameters being passed into bind_param().


回答1:


Simple mistake... Ugh. I hate that. Not my credit.

$bind = 'siiiiissssiss'; // No commas



回答2:


Given your var_dump($arr):

[0]=> string(25) "s,i,i,i,i,i,s,s,s,s,i,s,s"
[1]=> string(18) "Bacon Cheeseburger"
[2]=> int(15)
[3]=> int(4)
[4]=> int(13)
[5]=> int(56)
[6]=> int(6)
[7]=> string(2) "5m"
[8]=> string(1) "0"
[9]=> string(1) "0"
[10]=>int(0)
[11]=>int(0)
[12]=>string(2) "No"
[13]=>int(1325298618)

It seems to be like your $bind variable needs to be:

$bind = 's,i,i,i,i,i,s,s,s,i,i,s,i';

instead of

$bind = 's,i,i,i,i,i,s,s,s,s,i,s,s';


来源:https://stackoverflow.com/questions/8685586/type-definition-string-doesnt-match-number-of-bind-variables

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!