1

I'm not sure about this behaviour.

$query = 'INSERT INTO XXX (row1) VALUES (:myparam)';
$stmt = $mycon->prepare($query);
$stmt->bindParam(':myparam','',PDO::PARAM_STR);

above code will fail at third line. When I use following code, everything works fine. What's the (internal) difference?

$query = 'INSERT INTO XXX (row1) VALUES (:myparam)';
$stmt = $mycon->prepare($query);
$empty = '';
$stmt->bindParam(':myparam',$empty,PDO::PARAM_STR);

Regards Jens

  • 2
    [bindParam](https://www.php.net/manual/en/pdostatement.bindparam.php) ... "_binds a parameter to the specified variable name_" – brombeer Feb 03 '22 at 08:54
  • 1
    I believe the issue is that it needs to call by reference, not by value, and is probably (though I've never tried the latter) the difference between bindParam and bindValue. bindParam points to a variable and the value of that variable _at the point you call `execute()`_ is what is passed to the query. Hence if you need to run the query for multiple values, you `prepare()` and `bindParam()` before you loop through the values, and just assign the values then call `execute()` inside the loop. – droopsnoot Feb 03 '22 at 09:19
  • Yes, you have to pass a variable (which is then done by reference). You can't pass a hard-coded value, or the result of a function, for example. P.S. Your question would be more useful if you actually state the specific error / result you get when it fails. This will help others who are searching about a similar issue. I assume you get a notice about "only variables can be passed by reference" or similar? – ADyson Feb 03 '22 at 10:31

0 Answers0