0

I have this prepared Statment:

$stmt = $dbh->prepare("select * from t where name like :name ");

Binding like this works:

$p = "%glas%";
$stmt->bindParam(':name', $p );

If I put in the term direktly, it fails:

$stmt->bindParam(':name', "%glas%" );

What´s the difference?

Ahmed Siouani
  • 13,701
  • 12
  • 61
  • 72
mica
  • 3,898
  • 4
  • 34
  • 62

1 Answers1

3

If you look at the method definition for bindParam:

public bool PDOStatement::bindParam ( mixed $parameter , mixed &$variable [, int $data_type = PDO::PARAM_STR [, int $length [, mixed $driver_options ]]] )

You will notice it expects mixed &$variable meaning it needs an address. When you pass a literal string, it is not stored with an address the way a conventional variable is.

The reason for the address requirement is also discussed in the documentation:

Unlike PDOStatement::bindValue(), the variable is bound as a reference and will only be evaluated at the time that PDOStatement::execute() is called.

Jeremy Harris
  • 24,318
  • 13
  • 79
  • 133