gpt4 book ai didi

php - 使用完整或部分在表中搜索

转载 作者:行者123 更新时间:2023-11-29 21:46:35 26 4
gpt4 key购买 nike

我遇到一个小问题,我尝试搜索产品,而用户可以选择单击完整搜索或部分搜索。问题是,当用户单击完整搜索并输入某些内容时,整个表格都会打印出来,而产品的单词则不会(我想打印产品而不是整个表格)并且使用部分搜索,它只是打印结果它找不到匹配项。下面是到目前为止我的代码:

// DB configuration //
if (!isset($_POST["searchtype"])) {
echo "<form method='POST'>";
echo "Search for product:<br>";
# using html5 input type search
echo "<input type='text' name='searchtext' size='15' placeholder='search' results='5' autosave='saved-searches'>";
echo "<br><br>";
echo "Full search";
echo"<input type='radio' value='FULL' checked name='searchtype'><br>";
echo "Partial search ";
echo "<input type='radio' name='searchtype' value='PARTIAL'>";
echo "<br><br>";
echo "<input type='submit' value='Search' name='submit'>";
echo "</form>";


}
else {
$searchtext = $_POST["searchtext"]; # Retrieve from the form
$searchtype = $_POST["searchtype"]; # Retrieve from the form
$searchtext_san = sanitize_form_text($searchtext); # Prevents SQL injections!


try{
if($DBH == null)
$DBH = new PDO($dsn, $dbuser, $dbpass);


$sql = "select name, price, details from products where name='$searchtext_san'";


if ($searchtype == "FULL"){

$sql .= " = :searchtext_san";
$STH = $DBH->prepare($sql);
$STH->execute(array(':searchtext_san' => $searchtext_san));

}

if ($searchtype == "PARTIAL"){

$sql .= " LIKE ':searchtext_san'";
$STH = $DBH->prepare($sql);
$STH->execute(array(':searchtext_san' => '%'.$searchtext_san.'%'));
}


$STH->setFetchMode(PDO::FETCH_ASSOC);

$total = $STH->rowCount();
if ($total == 0){
echo "Sorry, no matches found!";
}
if ($total > 0){
while ($row = $STH->fetch()){

echo "{$row["name"]} {$row["price"]} {$row["details"]}<br>";
}
}

$DBH = null;
}
catch (PDOException $e){
echo '<b>PDOException: </b>',$e->getMessage();
die();
}
}

function sanitize_form_text($t)
{
$t = strip_tags($t);
$t = ereg_replace("[^A-Za-z0-9@._-]", "", $t);
return $t;
}

?>

最佳答案

尝试使用全文搜索

SELECT * FROM articles WHERE MATCH (title,body)
AGAINST ('+MySQL -YourSQL' IN BOOLEAN MODE);

Full text Search description

对于部分搜索,您可以喜欢

SELECT name FROM products WHERE name  RLIKE '[yourtext]'

关于php - 使用完整或部分在表中搜索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34063904/

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