PHP 错误:“无法通过引用传递参数 2”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13105373/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 04:46:56  来源:igfitidea点击:

PHP error: "Cannot pass parameter 2 by reference"

phpmysqlpdomysqli

提问by user1723760

I just need help on this PHP error which I do not quite understand:

我只需要关于这个我不太明白的 PHP 错误的帮助:

Fatal error: Cannot pass parameter 2 by reference in /web/stud/openup/inactivatesession.php on line 13

致命错误:无法在第 13 行的 /web/stud/openup/inactivatesession.php 中通过引用传递参数 2

<?php

error_reporting(E_ALL);

include('connect.php');

$createDate = mktime(0,0,0,09,05,date("Y"));
$selectedDate =  date('d-m-Y', ($createDate));

$sql = "UPDATE Session SET Active = ? WHERE DATE_FORMAT(SessionDate,'%Y-%m-%d' ) <= ?";                                         
$update = $mysqli->prepare($sql);
$update->bind_param("is", 0, $selectedDate);  //LINE 13
$update->execute();

?>

What does this error mean? How can this error be fixed?

这个错误是什么意思?如何修复此错误?

回答by Gung Foo

The error means that the 2nd argument is expected to be a reference to a variable.

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

Since you are not handing a variablebut an integer of value 0, it generates said error.

由于您处理的不是变量,而是值为 0整数,因此会产生上述错误。

To circumvent this do:

要避免这种情况,请执行以下操作:

$update->bind_param("is", $a = 0, $selectedDate);  //LINE 13

In case you want to understand what is happening, as opposed to just fixing your Fatal error, read this: http://php.net/manual/en/language.references.pass.php

如果您想了解正在发生的事情,而不是仅仅修复您的Fatal error,请阅读:http: //php.net/manual/en/language.references.pass.php

回答by John Woo

First,you shouldn't use DATE_FORMATwhen you want to compare date because DATE_FORMATchanges it to string not date anymore,

首先,DATE_FORMAT当您想比较日期时不应使用,因为DATE_FORMAT将其更改为字符串而不是日期

UPDATE Session 
SET Active = ? 
WHERE SessionDate <= ?

Second, store the value first on a variable and pass it on the paramater

其次,首先将值存储在变量上并将其传递给参数

$createDate = mktime(0,0,0,09,05,date("Y"));
$selectedDate =  date('d-m-Y', ($createDate));
$active = 0;
$sql = "UPDATE Session SET Active = ? WHERE SessionDate <= ?";                                         
$update = $mysqli->prepare($sql);
$update->bind_param("is", $active, $selectedDate);  
$update->execute();