【问题标题】:Fatal error: Cannot pass parameter 2 by reference致命错误:无法通过引用传递参数 2
【发布时间】:2013-03-07 12:35:00
【问题描述】:

我已尝试遵循-how-to-perform-update-with-mysqli-prepare 中给出的建议?在网站上,但我没有运气。

以下内容:

<?php

//connection
$con = new mysqli ("localhost","user","password","db");

$playno = "22";
$n1 = "4";
$n2 = "4";
$n3 = "4";

$stmt = $con -> prepare("UPDATE game SET no1 = ?, no2 = ?, no3 = ? WHERE id = ?");

$stmt -> bind_param ('iiii',"$n1","$n2","$n3","$playno");
$stmt -> execute();


?>

在浏览器中给出这个:

致命错误:无法通过引用传递参数 2 C:\xampp\htdocs... 在第 13 行

【问题讨论】:

    标签: php mysqli


    【解决方案1】:

    你的问题很简单:

    这是一个字符串 $playno = "22"; //细绳 $n1 = "4"; //细绳 $n2 = "4"; //细绳 $n3 = "4"; //字符串

    $stmt = $con -> prepare("UPDATE game SET no1 = ?, no2 = ?, no3 = ? WHERE id = ?");
    

    这是你的错误 $stmt -> bind_param ('iii',"$n1","$n2","$n3","$playno"); //失败,你必须使用s $stmt -> bind_param ('ssss',$n1,$n2,$n3,$playno); //这是正确的

    或改变:

    $playno = 22; //integer
    $n1 = 4; //integer
    $n2 = 4; //integer
    $n3 = 4; //integer
    $stmt -> bind_param ('iiii',$n1,$n2,$n3,$playno); //this is correct
    

    然后:

    $stmt -> execute();
    

    【讨论】:

      【解决方案2】:
      $stmt -> bind_param ('iiii',"$n1","$n2","$n3","$playno");
      

      应该是

      $stmt -> bind_param ('ssss',$n1,$n2,$n3,$playno);
      

      $playno = 22;
      $n1 = 4;
      $n2 = 4;
      $n3 = 4;
      
      $stmt -> bind_param ('iiii',$n1,$n2,$n3,$playno);
      

      【讨论】:

        【解决方案3】:

        当您尝试按如下方式传递参数时

        $stmt -> bind_param ('iiii',"$n1","$n2","$n3","$playno");
        

        "$n1" 等被视为常量字符串,因此会导致 错误(而不是警告)。

        此外,您需要传递整数值而不是字符串。

        $stmt -> bind_param( 'iiii',$n1, $n2, $n3, $playno );
        

        【讨论】:

          【解决方案4】:

          错误意味着bind_param 中的第二个参数应该是对变量的引用。

          $stmt -> bind_param ('iiii',"$n1","$n2","$n3","$playno");
          

          来自this

          注意 mysqli_stmt_bind_param() 需要通过参数传递 参考

          应该是:$stmt -&gt; bind_param ('iiii',$n1,$n2,$n3,$playno);

          【讨论】:

            【解决方案5】:
            $stmt -> bind_param ('iiii',$n1,$n2,$n3,$playno);
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2021-03-31
              • 2012-10-17
              • 1970-01-01
              • 2017-07-24
              • 2018-02-21
              • 2012-11-04
              • 1970-01-01
              相关资源
              最近更新 更多