首先,如果不允许这类问题,请告诉我。
以下是大约一年前的一些代码:
$search_sql = "SELECT Name, Amount, CardID FROM modernmasters WHERE";
$keywords = explode(" ", $query);
$keyCount = 0;
foreach ($keywords as $keys) {
if ($keyCount > 0){
$search_sql .= " AND";
}
$search_sql .= " Name LIKE '%$keys%'";
++$keyCount;
}
如果您原谅它来自已弃用的mysql_函数,我只想确保理解它的含义。
$search_sql = "SELECT Name, Amount, CardID FROM modernmasters WHERE";
$keywords = explode(" ", $query);
没有什么太难理解,它只是一个基本的fetch查询,其中使用explode函数将空格与$search_sql
字符串分开。
$keyCount = 0;
foreach ($keywords as $keys) {
if ($keyCount > 0){
$search_sql .= " AND";
}
$search_sql .= " Name LIKE '%$keys%'";
++$keyCount;
}
这将$keyCount
设置为0,然后进入foreach循环。这是我不太确定的地方,
if ($keyCount > 0){
$search_sql .= " AND";
}
这是否意味着当$keyCount
(数组中的每个单词)高于0时,它会在语句中对“AND”进行处理?
例如:
$keyCount
= 1;
"SELECT Name, Amount, CardID FROM modernmasters WHERE Name LIKE '%$keys%'";
$keyCount
= 2;
"SELECT Name, Amount, CardID FROM modernmasters WHERE Name LIKE '%$keys%' AND Name LIKE '%$keys%'";
因此,我搜索的每个单词都会添加" AND Name LIKE '%$keys%'"
,对吧?
或者我完全读错了吗?