因此,我准备了以下sql语句:
SELECT carName, modelName
FROM cars
INNER JOIN model ON cars.idCar = model.idCar
WHERE carName IN (?)`
$input=“‘丰田’、‘本田’”
我试图把这个放到sql语句中,但它给出了零行。我已经尝试了在phpMyAdmin查询和那里它的工作都很好。有人知道问题所在吗?
在列表中,每个值需要一个参数。如果传递单个参数,它将被解释为一个包含逗号的唯一字符串,而逗号不是您想要的(由于表中的所有名称都与此值不匹配,因此最终不匹配)。
因此,对于两个参数:
SELECT carName, modelName
FROM cars
INNER JOIN model ON cars.idCar = model.idCar WHERE carName IN (?, ?);
IN
子句接受多个参数,每个参数只能接受一个值。因此,要自动执行在此语句中插入多个参数的过程,您可以使用以下内容:
/* Execute a prepared statement using an array of values for an IN clause */
$params = ['toyota', 'honda'];
/* Create a string for the parameter placeholders filled to the number of params */
$place_holders = implode(',', array_fill(0, count($params), '?'));
/*
This prepares the statement with enough unnamed placeholders for every value
in our $params array. The values of the $params array are then bound to the
placeholders in the prepared statement when the statement is executed.
This is not the same thing as using PDOStatement::bindParam() since this
requires a reference to the variable. PDOStatement::execute() only binds
by value instead.
*/
$st = $db->prepare("SELECT carName, modelName FROM cars WHERE carName IN ($place_holders)");
$st->execute($params);