bindParam(':color', $someClass->getColor(...">

PDO follows the link?

It:

$stmt = $dbh->prepare("SELECT thing FROM table WHERE color = :color"); $stmt->bindParam(':color', $someClass->getColor()); $stmt->execute(); 

gives the following:

Runtime Notification
Only variables should be passed Link

although it is still running.

It:

 $stmt = $dbh->prepare("SELECT thing FROM table WHERE color = :color"); $tempColor = $someClass->getColor(); $stmt->bindParam(':color',$tempColor); $stmt->execute(); 

works without complaint.

I do not understand the difference?

+8
pass-by-reference php pdo
source share
4 answers

The second parameter, bindParam, is the reference variable. Since the function cannot be returned, it cannot strictly meet the needs of the bindParam parameter (PHP will work with you, although it will only issue a warning here).

To get a better idea, here is an example: this code will give the same results as your second example:

 $stmt = $dbh->prepare("SELECT thing FROM table WHERE color = :color"); $tempColor = NULL; // assigned here $stmt->bindParam(':color',$tempColor); $tempColor = $someClass->getColor(); // but reassigned here $stmt->execute(); 

This is not possible with a return function.

+6
source share

The description of PDOStatement::bindParam() states that it binds the PHP variable to quesitonmark or to the name placeholder. Since you are trying to pass a class method (even if this method returns a value), it is still not a variable name, so a warning. You might want to look at PDOStatement::bindValue() for future code.

+9
source share

If you want to avoid assigning a value to a variable, you might be better off trying:

 $stmt = $dbh->prepare("SELECT thing FROM table WHERE color = ?"); $stmt->execute(array($someClass->getColor())); 

As already mentioned, the error is caused by the fact that PDO :: statement-> bindParam expects parameter 2 to be a variable passed by reference.

+2
source share

If you really want to bind a value instead of a link, you can use PDOStatement :: bindValue , and then the code will look something like this:

 $stmt = $dbh->prepare("SELECT thing FROM table WHERE color = :color"); $stmt->bindValue('color', $someObject->getColor()); $stmt->execute(); 
-one
source share

All Articles