gpt4 book ai didi

php - 如何让MySQL表中的项目可搜索 "tags"

转载 作者:行者123 更新时间:2023-11-29 08:19:51 25 4
gpt4 key购买 nike

我有一个以下样式的 MySQL 表:

名称标签技术 PC、电脑、游戏电脑游戏、键盘、显示器

等等

我想知道是否可以使这些内容可搜索。例如,如果有人搜索“PC”,那么结果就会是技术。如果有人搜索“游戏”,那么结果将是技术和计算机。到目前为止我的代码如下:

<?php
//Include the connection file
include "subconnect.php";

$sql = "SELECT * FROM subs";



if(isset($_POST['search'])) {

$search_term = mysql_real_escape_string($_POST['search_box']);


$sql .= " WHERE tags = '{$search_term}'";

}


$query = mysql_query($sql) or die(mysql_error());

?>

<form name="search_form" method="POST" action="subsearch.php">

Search: <input type="text" name="search_box" value="" />

<input type="submit" name="search" value="Enter a category to find Subreddits!">
</form>
<table width="70%" cellpadding="5" cellspace="5">

<tr>
<td><h1>Name</h1></td>

</tr>
<?php while($row = mysql_fetch_array($query)) { ?>
<tr>
<td><?php echo $row['name']; ?></td>
</tr>
<?php } ?>

</table>

最佳答案

您可以执行string comparisons o a regex match ,但我认为这不是最好的方法

最好的方法是创建一个支持表“Tags”并与原始表创建 n -> n 关系。

关于php - 如何让MySQL表中的项目可搜索 "tags",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19719127/

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