2

I get datetime from this way:

class DB_Functions extends DB_Connect{

    private $dbConnect = "";
    public $dtime;

    public function __construct() {
        $this->dbConnect = $this->pdo_connect();
        $this->dtime = new DateTime();
    }

    public function destruct() {
        $this->dbConnect = null;
    }


    public function exampleInsert() {
        .
        .
        .
        $result->bindParam(':dateCreation',$this->dtime->format("Y-m-d H:i:s"),PDO::PARAM_STR);
    }
}

Then when I use dtime to insert in a table, like this:

Line 1708: $result->bindParam(':dateCreation',$this->dtime->format("Y-m-d H:i:s"),PDO::PARAM_STR);

Display this error:

<b>Strict Standards</b>:  Only variables should be passed by reference in <b>include\DB_Functions.php</b> on line <b>1708</b><br />

My declaration to get datetime is wrong?

Sebi
  • 1,390
  • 1
  • 13
  • 22
SoldierCorp
  • 7,610
  • 16
  • 60
  • 100
  • That DB_Functions class you posted isn't 1700 lines long. What *exactly* is on line 1708? – Mike B Feb 08 '13 at 18:36
  • Possible duplicate? http://stackoverflow.com/questions/2967597/only-variables-can-be-passed-by-reference or at least related – Damp Feb 08 '13 at 18:39
  • Line 1708 is $result->bindParam(':dateCreation',$this->dtime->format("Y-m-d H:i:s"),PDO::PARAM_STR); the same in the post. – SoldierCorp Feb 08 '13 at 18:39
  • 1
    http://www.php.net/manual/en/pdostatement.bindparam.php#111215. Function definition for bindParam says it passes the second argument by reference - meaning it must be a variable and not a value – Mike B Feb 08 '13 at 18:39

1 Answers1

2

The problem is that you are using bindParam() which actually binds a variable to the parameter in the query. This must be a variable, not a method call that returns a value.

This allows for usage like:

$value = 'somevalue';
$result->bindParam(':some_field', $value);
$value = 'someothervalue';
$result->execute(); // executes query with 'someothervalue' passed as parameter.

In your case you might want to use bindValue(). Which actually binds the value to the parameter in an immutable fashion. Either that or store your formatted datetime into a different class variable and continue to use bindParam() with that new variable.

Mike Brant
  • 70,514
  • 10
  • 99
  • 103