【问题标题】:msqli stmt->bind error 'Cannot pass parameter 2 by referencemsqli stmt->bind error '不能通过引用传递参数2
【发布时间】:2014-02-16 01:53:05
【问题描述】:
$arr = array (
        array ('item'=>'Salt', 'on_hand'=>2, 'cost'=>3.29, 'format'=>'box'),
        array ('item'=>'Pepper', 'on_hand'=>1, 'cost'=>2.19, 'format'=>'bag'),
        array ('item'=>'Cinnamon', 'on_hand'=>1, 'cost'=>1.55, 'format'=>'shaker'),
        );
  $sql = "INSERT INTO item_list VALUES (?, ?, ?, ?, ?)";
  if (!($stmt=$db->prepare($sql))) {
    echo "<br />Prepare failed: (".$db->errno.") ".$db->error; }
  foreach($arr as $key) {
    $item = $key['item'];
    $on_hand = $key['on_hand'];                    // format 1. $item or format 2. $key[;item']
    $cost = $key['cost'];                          // echo works with both formats...
    $format = $key['format'];                      // stmt->bind neither format works...
    echo "<br>$item $on_hand $cost $format<br>";   // first var is auto-increment *NULL*
    if (!$stmt->bind_param("isids", null, $key["item"], $key["on_hand"], $key["cost"], $key["format"])) {
        echo "<br />Binding failed: (".$stmt->errno.") ".$stmt->error; }
    if(!$stmt->execute()) {
      echo "<br />Execute failed: (".$stmt->errno.") ".$stmt->error; }
  }

不断收到此错误 - 致命错误:无法通过...中的引用传递参数 2 似乎没有任何效果 - 这可能与我的服务器设置有关吗? 我在这个论坛上看到了这个确切的 'stmt->bind_param() 格式,那个人说它有效...... PS:也需要从数据库中获取它,所以如果你能告诉我如何取回它,那就太好了 网络上的最佳论坛 - 实际有效的最新答案(到目前为止) 这对我有很大的帮助......谢谢一百万 还尝试了循环外的 stmt->bind_param() ... 格式为 #1

【问题讨论】:

  • 你可以试试 foreach($arr as $key => $value) { $item = $value['item'];
  • Satha thnx 但同样的错误

标签: php mysql foreach insert


【解决方案1】:

您可以在INSERT 语句中指定您的列名,而不是添加AUTO_INCREMENT 列。

INSERT INTO item_list (column2, column3, column4, column5) VALUES (?, ?, ?, ?);

试试这样的:

$stmt=$db->prepare($sql);
$stmt->bind_param("sids", $item, $on_hand, $cost, $format);

foreach($arr as $key) {
    $item = $key['item'];
    $on_hand = $key['on_hand'];
    $cost = $key['cost'];                          
    $format = $key['format']; 

    $stmt->execute();
}

【讨论】:

【解决方案2】:

经过数小时的搜索、测试、测试、测试和测试...... 终于想通了如何使用REFLECTION。 希望这对其他人有帮助!
PS:表格的第一行项设置为AutoIncrement,不包含在'prepare'绑定模板中。

  // Make sure error reporting on full - last setting of this func may affect all others using mysqli.
  // To be safe always call mysqli_report(MYSQLI_REPORT_OFF) at the end of the script.
  mysqli_report(MYSQLI_REPORT_ALL);

  // Multi dimensional associative array that will be reflected
  $ref_arr = array (  // Note the 'sids' mimics the stmt->bind_param() syntax
        array ('sids', 'item'=>'Salt', 'on_hand'=>2, 'cost'=>3.29, 'format'=>'box'),
        array ('sids', 'item'=>'Pepper', 'on_hand'=>1, 'cost'=>2.19, 'format'=>'bag'),
        array ('sids', 'item'=>'Cinnamon', 'on_hand'=>1, 'cost'=>1.55, 'format'=>'shaker'),
        );

  // Insert all the items/rows using Prepared Statement & Reflection
  if (!($res = $db->prepare("INSERT INTO item_list SET  item=?, cnt=?, cost=?, format=?"))) {
    echo "<br/>Prepare failed: (".$db->errno.") ".$db->error;
    return; }
  $ref = new ReflectionClass('mysqli_stmt');  // Instantiate the reflection class
  $method = $ref->getMethod("bind_param");  // Set the binding method
  foreach($ref_arr as $arr) { // Loop through the multi dimensional associative array
    $method->invokeArgs($res,$arr);
    $res->execute();
  }

【讨论】:

    【解决方案3】:

    我看到你已经完全修改了你的代码,但你原来的问题很简单——你试图将 null 绑定为参数:

    bind_param("isids", null, ...
                        ^^^^
    

    这就是导致您的错误的原因,但很容易修复:

    $n = null;
    bind_param("isids", $n, ...
    

    【讨论】:

    • 对不起皮特,但问题不在于参数 #1
    猜你喜欢
    • 1970-01-01
    • 2014-01-25
    • 2012-11-04
    • 2010-10-07
    • 2011-08-21
    • 1970-01-01
    • 2012-10-17
    • 2017-09-25
    相关资源
    最近更新 更多