如何检查数据库中出现的单词? [英] How to check words for occurence in database?

查看:39
本文介绍了如何检查数据库中出现的单词?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将禁用词存储在数据库中,以便以后确定是否存在某个词(因此是禁用词,应予以审查).

I'd like to store banned words in a database to later determine if some word is present (hence being a banned word, which should be censored).

单词输入表单(ban.php):

Word input form (ban.php) :

<form name="form" method="post" action="add.php">
    <input type="text" name="bad" id="bad">
    <input type="submit" name="submit" id="submit" size="12" value="submit">
</form>

PHP 代码 (add.php) :

PHP code (add.php) :

<?PHP
require_once("config.php"); // db conn

$bad = $_POST['bad'];
$bad = mysql_real_escape_string($bad);
$sql = "insert into my_table set bad='$bad'";

mysql_query($sql, $conn) or die(mysql_error());
echo "Done bad word added";
?>

假设我们禁止了ugly 这个词.现在我想这样做:

Let's say we banned the word ugly. Now I want do this :

<?PHP
require_once("config.php"); // db conn

$qry    = "select * from my_table";
$result = mysql_query($qry) or die($qry);
$test   = "ugly"; // Example

if ($test == any of the words in the db my_table){
    echo "banned";
}else{
    echo "passed";
{
?>

如何做到这一点?my_table 中添加了很多词:

How to do this? There are many added words in my_table :

id,bad (1,'ugly')
id,bad (2,'sick')
id,bad (3,'manal')
id,bad (4,'fog')

推荐答案

您应该尝试从数据库中选择单词,这比必须遍历数组要快得多.

You should attempt to select the word from the database, this is much faster than having to go through an array.

这样的事情应该可以工作.

Something like this should work.

<?php
require_once("config.php"); // db conn

$test = "ugly"; // remember to use mysql_real_escape_string in the implementation

$qry = "SELECT * FROM `my_table` WHERE bad='{$test}'"; // select by value
$result=mysql_query($qry);

if( mysql_num_rows( $result ) ){ // we have a row with the 'bad' word
    echo "banned";
}else{
    echo "passed";
}
?>

这篇关于如何检查数据库中出现的单词?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆