如何解决 PHP 中的“无法通过引用传递参数”错误?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8287581/
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
How to resolve 'cannot pass parameter by reference' error in PHP?
提问by Kumar Kush
Here's my code:
这是我的代码:
$stmt = $conn->mysqli->prepare('INSERT INTO photos (CaseNo, ImageName, CaptureTime, UploadTime) VALUES (?,?,?,?)');
$stmt->bind_param('isss', $caseno, $index.'.'.$extension, date('Y-m-d H:i:s'), date('Y-m-d H:i:s'));
I have tried this also:
我也试过这个:
$stmt = $conn->mysqli->prepare('INSERT INTO photos (CaseNo, ImageName, CaptureTime, UploadTime) VALUES (?,?,?,?)');
$captureTime = date('Y-m-d H:i:s');
$uploadTime = date('Y-m-d H:i:s');
$stmt->bind_param('isss', $caseno, $index.'.'.$extension, $captureTime, $uploadTime);
I am getting the error:
我收到错误:
Fatal error:** Cannot pass parameter 3 by reference in **...file path...line #
致命错误:** 无法通过 **...文件路径...行中的引用传递参数 3
Please note that CaptureTimeand UploadeTimehave datatype date. And ignore the fact that I am passing the value of 3rd and 4th parameter same.
请注意CaptureTime和UploadeTime 的数据类型为date。并忽略我传递的第 3 个和第 4 个参数的值相同的事实。
What's wrong with the code?
代码有什么问题?
回答by Kevin Vandenborne
Change
改变
$stmt->bind_param('isss', $caseno, $index.'.'.$extension, $captureTime, $uploadTime);
to
到
$isss = 'isss';
$indexExtention = $index.'.'.$extension
$stmt->bind_param($isss, $caseno, $indexExtention , $captureTime, $uploadTime);
I believe you have to pass variables rather than a string.
我相信你必须传递变量而不是字符串。
Or you could use bindvalue()
instead of bindparam()
if you're using PDO.
或者bindvalue()
,bindparam()
如果您使用的是 PDO ,则可以使用代替。
回答by Stijn Leenknegt
Maybe you need to cast it to string?
也许您需要将其转换为字符串?
...$extension, (string) $captureTime, (string) $uploadTime);