74

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

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

<?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?

ggorlen
  • 44,755
  • 7
  • 76
  • 106
user1723760
  • 1,157
  • 1
  • 9
  • 18
  • reference for you: this issue has similar problem http://stackoverflow.com/questions/8287581/how-to-resolve-cannot-pass-parameter-by-reference-error-in-php#autocomment44785441 – Yoshi Jan 29 '15 at 08:57

2 Answers2

132

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

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

To circumvent this do:

$a = 0;
$update->bind_param("is", $a, $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

Dharman
  • 30,962
  • 25
  • 85
  • 135
Gung Foo
  • 13,392
  • 5
  • 31
  • 39
  • 44
    PHP can be so weird sometimes. – Adam Fowler Jul 27 '16 at 18:15
  • 4
    Really funny, because the error message seems to indicate exactly the opposite :-) It said "Cannot pass parameter 2 by reference" and I was like "But I am not passing it as reference so what is the problem?" – lot Nov 04 '20 at 14:34
4

First,you shouldn't use DATE_FORMAT when you want to compare date because DATE_FORMAT changes it to string not date anymore,

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();
John Woo
  • 258,903
  • 69
  • 498
  • 492