I wrote a script that should simply insert the data into the SELECT and return the result:

$searchSql = "SELECT * FROM consult AS c LEFT JOIN user AS u ON c.lectorId=u.id WHERE c.date>=NOW() "; $sqlParams = []; if (!empty($group)){ $searchSql .= "AND WHERE `group` LIKE :group"; $sqlParams[':group'] = "%$group%"; } if (!empty($discipline)){ $searchSql .= "AND WHERE `discipline` LIKE :discipline"; $sqlParams[':discipline'] = "%$discipline%"; } if (!empty($lector)){ $searchSql .= "AND WHERE `lector` LIKE :lector"; $sqlParams[':lector'] = "%$lector%"; } $stm = $db->prepare($searchSql); foreach ($sqlParams as $key=>$value){ $stm->bindValue($key,$value,PDO::PARAM_STR); } $stm->execute(); $result = $stm->fetchAll(); 

Request that arrives in MySQL:

 SELECT * FROM consult AS c LEFT JOIN user AS u ON c.lectorId=u.id WHERE c.date>=NOW() AND WHERE `group` LIKE '%12%' 

Why was the parameter: group inserted in single quotes?

PHP 5.6 MySQL 5.5 (OpenServer 5.2.4)

  • The fact that in the request the group parameter was wrapped in single quotes there is no error. Error in building the query, and more specifically - in the second WHERE ( AND WHERE ). The request threw an error due to incorrect syntax. - atom-22

2 answers 2

Why was the parameter: group inserted in single quotes?

Because it is not the PDO that selects the type, but you tell him that all your parameters are lowercase:

 $stm->bindValue($key,$value,PDO::PARAM_STR); 

If you need some other type, you need to specify this explicitly. List of available types: http://php.net/manual/ru/pdo.constants.php

Although, judging by the fact that you use LIKE %%, everything works exactly as it should.

    Otherwise, MySQL will consider this a syntax and throw an error. Reserved Words

    Nonreserved keywords are not permitted as identifiers without quoting. If you quote them

    • The author in the query has two group words in single quotes, so you are probably confused. The fact that he wrapped the name of the column in the background says that he understands what you wrote and asks about something else. - Alexey Ukolov