How to resolve 'cannot pass parameter by reference' error in PHP? [duplicate]
Asked Answered
W

2

14

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 #

Please note that CaptureTime and UploadeTime have datatype date. And ignore the fact that I am passing the value of 3rd and 4th parameter same.

What's wrong with the code?

Wilhite answered 27/11, 2011 at 17:43 Comment(1)
reference for you: this issue has similar problem #13105873Barytes
L
33

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.

Leathers answered 27/11, 2011 at 17:49 Comment(2)
Are you sure mysqli statement supports bindvalue function()? In the mysqli manaul on php.net, I do not see that function.Wilhite
Ahh, no. Only PDO seems to support it. I thought MySQLi did too.Leathers
B
-2

Maybe you need to cast it to string?

...$extension, (string) $captureTime, (string) $uploadTime);
Baddie answered 27/11, 2011 at 17:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.