gpt4 book ai didi

php - Foreach 嵌套在带有 PDO 查询的 while 循环中

转载 作者:行者123 更新时间:2023-11-30 22:39:21 24 4
gpt4 key购买 nike

为了在我的脚本中减少可以这么说的脂肪,我决定使用 1 个 PDO prepare 来跨越一组预定义的表。 PDO 在 while 循环中执行,在 while 循环中有一个 foreach 来构建每个结果集的输出。

这是搜索代码。该脚本目前在 3 个表中搜索结果(通过 while 迭代)。我们称它们为表 a、b 和 c。对于测试搜索,它在表 a 中找到 2 个结果,在表 b 中找到 0 个结果,在表 c 中找到 1 个结果。

虽然它总共找到了 3 个结果,但它只显示了 2 个。一个来自表 'a',一个来自表 'c'。该脚本未构建表“a”中第二次查找的结果。

我仔细看了一遍,直到我的眼睛流血,并在寻找我可能做错的地方,我想不通。对这段代码有什么问题有什么想法吗?

// --- Build an array of places to search
$tableArray = array("services", "webPages", "dsiu");
$tableCount = count($tableArray);
$count = "0";
$resCount = "0";

$result = "";
while ($tableCount > $count) {
// -- Search tables in the array for matches
$quotedString = $db->quote($searchString);
$qSQL = "SELECT title, ldesc, SUM(MATCH(title, sdesc, ldesc) AGAINST(:string IN BOOLEAN MODE)) AS score FROM ".$tableArray[$count]." WHERE MATCH (title, sdesc, ldesc) AGAINST (:string IN BOOLEAN MODE) ORDER BY score DESC";
$q = $db->prepare($qSQL);
$q->execute(array(':string'=>$quotedString));

// -- keep a count of the results
$rowCount = $q->rowCount();

if ($rowCount > 0) {
$resCount = $resCount + $rowCount;

// -- build result html
$html = $q->fetchAll(PDO::FETCH_ASSOC);
foreach ($html as $row) {

// --- Clean the results for display
$desc = cleanURL($row['ldesc']);
$desc = str_ireplace($searchString, '<b><font color="green">'.$searchString.'</font></b>', $desc);
$desc = substr($desc, 0, 300)."...";

$result .= "<font color='red'><b>".$row['title']."</b></font><br>".$desc."<br><br>";

}
}

$count++;
}

最佳答案

感谢@dan08。我在 foreach 中添加了一个 if ($row['score'] != null) {。还发现查询中的 SUM() 删除了本应是结果的内容。

现在正在处理更改。

关于php - Foreach 嵌套在带有 PDO 查询的 while 循环中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31521998/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com