0

如何在 LIKE 子句中使用 var?

以下代码未发送正确的响应...

$query = sprintf("
SELECT
place.ID AS id,

IF(
    place.translationID IS NULL,
    place.name,
    placel10n.text
    ) AS cityname,

FROM
places AS place

 LEFT JOIN `l10n-strings` AS placel10n ON (place.translationID = placel10n.translationID AND placel10n.languageCode = 'de')

 WHERE
 place.name LIKE CONCAT('$fchar', '%');
 AND
place.`status` = '1'

");
4

2 回答 2

0

如果$fchar包含您要搜索的字符串,您可以这样做:

$sql = "
SELECT
place.ID AS id,

IF(
    place.translationID IS NULL,
    place.name,
    placel10n.text
    ) AS cityname,

FROM
places AS place

 LEFT JOIN `l10n-strings` AS placel10n ON (place.translationID = placel10n.translationID AND placel10n.languageCode = 'de')

 WHERE
 place.name LIKE CONCAT('%s', '%');
 AND
place.`status` = '1'";

$query = sprintf($sql,$fchar);

因此,如果$fchar = 'apple',那么您的查询将尝试匹配“apple%”。

于 2013-03-20T20:34:25.957 回答
0

认为你可以做到

LIKE '$fchar%';

或者使用准备好的语句(但不清楚使用 sprintf 想要什么)

place.name LIKE :fchar;

$fchar = $fchar'.'%';

$something->bindParam(':fchar', $fchar, PDO::PARAM_STR);
于 2013-03-20T20:34:52.493 回答