开发者

mysqli & bound statements, 2031: No data for parameters in statement

Here is my code, that isn't working, don't know why:

$sql = `INSERT INTO `_translations` (id, module, item_id, name_id, output, language) VALUES (?, ?, ?, ?, ?, ?) ON DUPLICATE KEY UPDATE output = ?`
$stmt = mysqli_prepare($this->connection, $sql);
$params = array($stmt, $types);
foreach($array as $k=>&$v){
    $params[] =& $v;
}
// this call is update call, therefore parameters are merged
if($update && $_update) $params = array_merge($params, $_update);
call_user_func_array('mysqli_stmt_bind_param', $params); # i think the problem is in this line..
if(mysqli_stmt_execute($stmt)){
    if($this->transaction){ $this->affected++; }
    else{ $this->affected = 1; }
}else{ 
    $error = mysqli_stmt_error($stmt); 
    $errno = mysqli_stmt_errno($stmt);
    die("{$sql}<br/><pre>".var_export($params, true)."</pre><br/>{$errno}: {$error}");
}

die() results in:

INSERT INTO `_translations` (id, module, item_id, name_id, output, language) VALUES (?, ?, ?, ?, ?, ?) ON DUPLICATE KEY UPDATE output = ?
array (
  0 => 
  mysqli_stmt::__set_state(array(
     'affected_rows' => NULL,
     'insert_id' => NULL,
     'num_rows' => NULL,
     'param_count' => NULL,
     'field_count' => NULL,
     'errno' => NULL,
     'error' => NULL,
     'sqlstate' => NULL,
     'id' => NULL,
  )),
  1 => 'isissss',
  2 => '',
  3 => 'seo',
  4 => '',
  5 => 'keywords',
  6 => 'tesoutput',
  7 => 'en',
  8 => 'tesoutput',
)

2031: No data supplied for parameters in prepared statement

Well, type count equals parameters and everything should be working fine, but it isn't. Since this same function runs for simple inserts, I know it works... If more data is needed from my side, feel free to ask.

Any solutions?

Thanks in advance!

UPDATE #1:

Here are $array, $types and $update print outs.

P.S. There's a chain before each of all these 3 variables are set, but they contain exactly what's needed!

// $array
array(6) {
  ["id"]=>
  string(0) ""
  ["module"]=>
  string(3) "seo"
  ["item_id"]=>
  string(0) ""
  ["name_id"]=>
  string(8) "keywords"
  ["output"]=>
  string(9) "tesoutput"
  ["language"]=>
  string(2) "en"
}
// $types
string(7) "isissss"
// $update
array(1) {
  [0]=>
  string(9) "tesoutput"
}

I have already thought that the problem could be in $types string not maching $array value types, because all values are strings there. I'm not sure, th开发者_如何学编程ough, because when inserting without update, it works, even if those are strings.

UPDATE #2:

It looks like it's a version related bug, driver bug - http://bugs.php.net/bug.php?id=43568 ... Anyone has managed to get this to work > 5.2 with mysqlnd? And, yes, here on localhost I'm running 5.2.6 with default mysql driver, where on production server, PHP is running version 5.3.5 with mysqlnd.


After a bit more research on http://bugs.php.net/bug.php?id=43568, I've managed to fix the problem by replacing:

foreach($array as $k=>&$v){
    $params[] =& $v;
}
if($update && $_update) $params = array_merge($params, $_update); # this is the actual line, that I've replaced

with:

// did a little housekeeping too
foreach($array as $k=>&$v) $params[] =& $v;
if($update && $_update) foreach($_update as $kk => $vv) $params[] =& $v; # instead of merging, I'm populating values the hard way.

Huh, interesting how 5.2 allows constant value binding, and 5.3 doesn't. Anyways, I'm happy I've resolved this, and I hope it will help others in future.

This actually explains why insert did work regardless of version... those values were getting populated with reference variables from the beggining.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜