Fatal error occurred in execute. Cause was bindValue argument (PHP)

Aug 25, 2020 PHP MySQL Beginner Learning

Fatal error occurred in #execute. The cause was bindValue

[Error code]


$name = John;
$id = 1;

$sql = SELECT * FROM bbs WHERE name? OR id=?
$statement = prepare($sql);
$statement->bindValue(1, $name);
$statement->bindValue(2, $id);
$statement->execute();
fatal error:C in...

I do not understand the cause at all with an error like this! If you have studied a little, you may find that this is the cause at first glance. It took me hours to notice.

**The answer is the third argument of bindValue! **

If you do not enter the third argument of bindValue, STR type will be set as an initial value. I was entering a numeric type there, so an error occurred.

No hassle when using bindValue

$statement->bindValue(1, $name, PDO::PARAM_STR); //STR type
$statement->bindValue(2, $id, PDO::PARAM_INT); //INT type

You can prevent such mistakes by specifying the third argument like. I would be happy if it would be helpful for those who are new to programming and are stuck.

#reference [PHP official document] https://www.php.net/manual/ja/pdostatement.bindvalue.php