我正在尝试使用以下查询获得结果,idRegion在数据库中记录为1,2,3,4为每个特许经营者,所以我想要显示所有特许经营者idRegion 2.我通过$ _Get获得idRegion 。这个显示只有昏迷前的第一个数字,我想它应该准备整个字符串1,2,3,4?当我使用有效的静态值时?
$colname_franchisee = "-1";
if (isset($_GET['id'])) {
$colname_franchisee = $_GET['id'];
}
$query_franchisee = sprintf("SELECT * FROM franchise WHERE stiShowInLinks = 'Y' AND idRegion LIKE '%s%' ORDER BY stiName ASC", $colname_franchisee);
答案 0 :(得分:1)
虽然我不喜欢你的数据库设计,但这应该可行:
$query_franchisee = sprintf("
SELECT *
FROM franchise
WHERE
stiShowInLinks = 'Y' AND (
idRegion = '%d' OR
idRegion LIKE '%d,%%' OR
idRegion LIKE '%%,%d' OR
idRegion LIKE '%%,%d,%%'
)
ORDER BY stiName ASC
",
$colname_franchisee,
$colname_franchisee,
$colname_franchisee,
$colname_franchisee
);
sprintf函数将%
字符视为格式说明符,并以特殊方式处理接下来的几个字符。要按字面意思使用%
字符,您必须使用%%
。因此,在sprintf之后,您的查询变为:
idRegion = '1234' OR
idRegion LIKE '1234,%' OR
idRegion LIKE '%,1234' OR
idRegion LIKE '%,1234,%'
答案 1 :(得分:0)
$colname_franchisee = "-1";
if (isset($_GET['id'])) {
$cf = intval($_GET['id']); only if id integer.
}
$query_franchisee = "SELECT * FROM franchise
WHERE stiShowInLinks = 'Y'
AND idRegion LIKE '%cf%'
ORDER BY stiName ASC", $cf);