准备好的语句按引用传递错误

发布于 2024-08-03 20:52:52 字数 906 浏览 4 评论 0原文

我没有看到错误,希望有人能弄清楚:

 public static function createMessage($title, $message, $startDate, $endDate, $author, $status){
      //$dbConn is now a mysqli instance with a connection to the database foobar
      $dbConn = Database::getConnection("foobar");

      $stmt = $dbConn->prepare("INSERT into messages(title, msg, date_start, date_end, app_usersID_FK, date_created, activeflag, msg_status) VALUES (?,?,?,?,?,?,?,?)");

      if(!$stmt){
           throw new Exception("Unable to prepare the statement");
      }

      $dt = date("Y-m-d");
      $stmt->bind_param("ssssisii", $title, $message, $startDate, $endDate, $author, $dt, 1, $status);
      $stmt->execute();

      return true;
 }

Function call

MessageCenter::createMessage("Hello", "Just Calling to say hi", "2009-09-12", "2009-09-12" 、“1”、“1”);

错误消息是:

致命错误:无法通过引用传递参数 8

I'm not seeing the error and was hoping someone could figure it out:

 public static function createMessage($title, $message, $startDate, $endDate, $author, $status){
      //$dbConn is now a mysqli instance with a connection to the database foobar
      $dbConn = Database::getConnection("foobar");

      $stmt = $dbConn->prepare("INSERT into messages(title, msg, date_start, date_end, app_usersID_FK, date_created, activeflag, msg_status) VALUES (?,?,?,?,?,?,?,?)");

      if(!$stmt){
           throw new Exception("Unable to prepare the statement");
      }

      $dt = date("Y-m-d");
      $stmt->bind_param("ssssisii", $title, $message, $startDate, $endDate, $author, $dt, 1, $status);
      $stmt->execute();

      return true;
 }

Function call

MessageCenter::createMessage("Hello", "Just calling to say hi", "2009-09-12", "2009-09-12", "1", "1");

Error Message is:

Fatal error: Cannot pass parameter 8 by reference

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

世界等同你 2024-08-10 20:52:52

我猜你的 bind_param 方法实际上是 <代码>mysqli_stmt::bind_param。如果是:每个参数(第一个参数除外)必须是通过引用传递的变量,因此它们可以“绑定”。

就像手册所说的(强调我的)

mysqli_stmt::bind_param --
mysqli_stmt_bind_param — 绑定
变量作为参数传递给准备好的语句

这意味着您不能传递值:您必须使用变量。

Si,就你的情况而言,应该这样做:

$my_var = 1;
$stmt->bind_param("ssssisii", $title, $message, $startDate, 
   $endDate, $author, $dt, $my_var, $status);

I'm guessing your bind_param method is actually mysqli_stmt::bind_param. If yes : every parameters (except for the first one) must be variables passed by reference, so they can be "binded".

Like the manual says (emphasis mine) :

mysqli_stmt::bind_param --
mysqli_stmt_bind_param — Binds
variables to a prepared statement as parameters

This means you cannot pass a value : you have to use variables.

Si, on your case, something like this should do :

$my_var = 1;
$stmt->bind_param("ssssisii", $title, $message, $startDate, 
   $endDate, $author, $dt, $my_var, $status);
‘画卷フ 2024-08-10 20:52:52

找到了!它希望 activeFlag 成为一个变量。以下作品:

$dt = date("Y-m-d");
$flag = 1;
$stmt->bind_param("ssssisii", $title, $message, $startDate, $endDate, $author, $dt, $flag, $status);
$stmt->execute();

Found it! It wanted activeFlag to be a variable. The following works:

$dt = date("Y-m-d");
$flag = 1;
$stmt->bind_param("ssssisii", $title, $message, $startDate, $endDate, $author, $dt, $flag, $status);
$stmt->execute();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文