简体   繁体   English

如何使用 PHP 和 html 检查句子的 SQL 中单词的出现

[英]How to check the occurrence of the words in the SQL of a sentence using PHP and html

I'm getting a sentence from user input then chopping it into words and listing them in a array.我从用户输入中得到一个句子,然后将其切成单词并将它们列在一个数组中。

After that I want to pass this array depending on number of words in the sentence to a SQL query which will check for the occurrence of each word separately and return then number.之后,我想根据句子中的单词数将此数组传递给 SQL 查询,该查询将分别检查每个单词的出现,然后返回数字。

The first part is done by using regex:第一部分是通过使用正则表达式完成的:

preg_match_all('/<[^>]++>|[^<>\s]++/', $sent, $tokens);
print_r($tokens);

The output is like this: output是这样的:

在此处输入图像描述

But for the SQL query loop I'm stuck.但是对于 SQL 查询循环,我被卡住了。 I don't know where to start with my database table is like this:我不知道从哪里开始我的数据库表是这样的:

在此处输入图像描述

And my expected output is something like:我预期的 output 是这样的:

在此处输入图像描述

PS:- I think the PHP code should know the length of the user input sentence first, then tokenize it to pass the words to the SQL loop query to be searched separately. PS:-我认为PHP代码首先应该知道用户输入句子的长度,然后对其进行标记以将单词传递给SQL循环查询以单独搜索。

$words = array();
while (...) {
    $word = ...;  // extract the word
    $words[] = "'$word'";
}
$in_list = implode(', ', $words);  //  'hi', 'how', 'are', 'you'
$sql = "SELECT  word,
                COUNT(*) as freq   -- COUNT(*) is the common syntax
            FROM tbl_words 
            WHERE word IN ($in_list)";
... $sql ...   // perform the query and deal with the results

To do the update, generate this and execute:要进行更新,请生成并执行:

$sql = "UPDATE tbl_words
            SET freq = freq + 1
            WHERE word IN ($in_list)";

If a sentence can have a word twice, do you want the table to incremented twice?如果一个句子可以有一个单词两次,你想让表格增加两次吗? Whether or not you want that, I might build a hash instead of an array:不管你是否想要,我可能会构建一个 hash 而不是一个数组:

$words = array();
while (...) {
    $word = ...;  // extract the word
    $words[$word] = (isset($words[$word]) ? $words[$word] + 1 : 1);
}
$in_list = implode(', ', array_keys($words));  //  'hi', 'how', 'are', 'you'

That assumes multiple copies of a word count as only 1.假设一个字数的多个副本仅为 1。

For the incrementing by the repeated count, things get messier.对于重复计数的递增,事情变得更加混乱。

Based on your parsing code:根据您的解析代码:

$words = array();
preg_match_all('/<[^>]++>|[^<>\s]++/', $sent, $tokens);
foreach($tokens as $token) {
    $words[$word] = (isset($words[$word]) ? $words[$word] + 1 : 1);
}
$in_list = implode(', ', array_keys($words));
echo $in_list;

Try this.尝试这个。 I'll explain the code stepwise.我将逐步解释代码。

<?php
    $sentence = '';
    $result = '';

    if (isset($_GET['sentence'])) {
        $sentence = $_GET['sentence'];
        $tokens = tokenize($sentence);

        $mysqli = new mysqli('localhost', 'user', 'pass', 'dbname');
        $sql = getSQL($tokens, $mysqli);
        $result = $mysqli->query($sql);
    }

    function tokenize($sent) {
        preg_match_all('/<[^>]++>|[^<>\s]++/', $sent, $tokens);
        return $tokens[0];
    }

    function getSQL($tokens, $mysqli) {
        $sql = array();
        foreach ($tokens as $token) {
            $sql[] = sprintf("select '%s', ifnull(min(freq), 0) from test where word = '%s' ",
                $mysqli->real_escape_string($token),
                $mysqli->real_escape_string($token)
            );
        }
        return implode(' union all ', $sql);
    }
?>
<!doctype html>
<form method="get">
sentence: <input type="text" name="sentence" value="<?php echo $sentence; ?>"/>
</form>

<?php
if ($result !== ''):
?>

<div>
    <table border="1">
        <tr>
            <th>word</th>
            <th>freq</th>
        </tr>
<?php
    while ($row = $result->fetch_row()):
?>
        <tr>
            <td><?php echo $row[0]; ?></td>
            <td><?php echo $row[1]; ?></td>
        </tr>
<?php
    endwhile;
?>
    </table>

<?php
endif;
?>

</div>
</form>

PHP code above the HTML code PHP 代码高于 HTML 代码

We set the sentence and result variable to empty.我们将sentenceresult变量设置为空。 We'll store what the user typed into the variable sentence.我们将用户输入的内容存储到变量句中。 The result will be the result from MySQL.结果将是 MySQL 的结果。

if (isset($_GET['sentence'])) {... } block checks whether the webpage had query string named sentence . if (isset($_GET['sentence'])) {... }块检查网页是否有名为sentence的查询字符串。 If we received that query string, then do something.如果我们收到了那个查询字符串,那就做点什么。 Otherwise don't do anything.否则什么都不做。

In that block, we sequentially do this:在该块中,我们按顺序执行此操作:

  • store sentence supplied by the user in a variable将用户提供的句子存储在变量中
  • use your preg_match_all method to tokenize the sentence使用你的 preg_match_all 方法来标记句子
  • create a SQL dynamically动态创建 SQL
  • execute the the SQL and store the output in results variable执行 SQL 并将 output 存储在结果变量中

The function tokenize is self-explanatory. function tokenize是不言自明的。 It takes the sentence and outputs an array just like you noticed.就像你注意到的那样,它接受句子并输出一个数组。

SQL creation SQL 创建

You can ask MySQL for frequency like so您可以向 MySQL 询问频率,如下所示

select freq from test where word = 'bogus';

If the word 'hi' is not present, you will get no results.如果没有“hi”这个词,您将得不到任何结果。 In order to force a result, you can ask for a summary like count, min, max, etc.为了强制结果,您可以要求提供一个摘要,如计数、最小值、最大值等。

select min(freq) from test where word = 'bogus';

will result将导致

+-----------+
| min(freq) |
+-----------+
|      NULL |
+-----------+

If we asked MySQL to substitute NULL with zero like this:如果我们要求 MySQL 用零替换 NULL,如下所示:

select ifnull(min(freq), 0) from test where word = 'bogus';

you'll get:你会得到:

+----------------------+
| ifnull(min(freq), 0) |
+----------------------+
|                    0 |
+----------------------+

So, we'll take advantage of that and ask:因此,我们将利用这一点并询问:

select 'hi', ifnull(min(freq), 0) from test where word = 'hi';

If 'hi' doesn't exist, you'll get如果'hi'不存在,你会得到

+----+----------------------+
| hi | ifnull(min(freq), 0) |
+----+----------------------+
| hi |                    0 |
+----+----------------------+

Now, you could combine multiple queries like this:现在,您可以像这样组合多个查询:

select 'hi', ifnull(min(freq), 0) from test where word = 'hi'
union all
select 'how', ifnull(min(freq), 0) from test where word = 'how';

to get要得到

+-----+----------------------+
| hi  | ifnull(min(freq), 0) |
+-----+----------------------+
| hi  |                    0 |
| how |                    5 |
+-----+----------------------+

Great.伟大的。 So, let's try taking all the tokens and create a UNION ALL query.因此,让我们尝试获取所有标记并创建一个UNION ALL查询。

That's what function getSQL does.这就是function getSQL所做的。 It goes through each token and stores each select... query in the array.它遍历每个令牌并将每个select...查询存储在数组中。

Notice that I use real_escape_string functionality within mysqli to escape special characters and make the query safer.请注意,我在 mysqli 中使用real_escape_string功能来转义特殊字符并使查询更安全。

Then, we join all the select queries together and put union all between them using the implode function.然后,我们将所有 select 查询连接在一起,并使用implode function 在它们之间进行union all

Once we receive the query back, we execute it using $mysqli->execute($sql) .一旦我们收到查询回来,我们使用$mysqli->execute($sql)执行它。 That returns for us the results.这为我们返回了结果。

HTML part HTML 零件

We create a form with a GET method so that sentence is returned as a query string.我们使用 GET 方法创建一个表单,以便将句子作为查询字符串返回。 If it is the first time the page is loaded, $sentence will be empty.如果是第一次加载页面, $sentence将为空。 We publish that in the value attribute.我们在value属性中发布它。 If the page was called with query string, $sentence will contain what the user typed.如果页面是用查询字符串调用的, $sentence将包含用户输入的内容。

<form method="get">
sentence: <input type="text" name="sentence" value="<?php echo $sentence; ?>"/>
</form>

Then,然后,

<?php
if ($result !== ''):
?>

<div>
...
</div>

<?php
endif;
?>

The will be published only if $result is not an empty string.仅当$result不是空字符串时才会发布。 $result is empty when the page loads (or query fails).当页面加载(或查询失败)时, $result为空。 So, that block will not be visible.因此,该块将不可见。 If the query succeeds, $result will not be empty and that block will be visible.如果查询成功, $result将不为空,并且该块将可见。

Table creation表创建

We create the table and put inline PHP.我们创建表并放入内联 PHP。 We loop through the records one by one and publish the token and the frequency.我们一一遍历记录并发布令牌和频率。 As previously noted, those words that don't have a frequency will show zero.如前所述,那些没有频率的词将显示为零。

Try it out.试试看。 Also note that there are several other improvements that can be done to this code, but it's a start.另请注意,可以对此代码进行其他几项改进,但这只是一个开始。

I'm not sure I understand the question, but I'll give it a shot and please tell me if it's not what you're asking for (English isn't my first language).我不确定我是否理解这个问题,但我会试一试,如果这不是你想要的,请告诉我(英语不是我的第一语言)。

PHP PHP

<?php
// Connects to DB
$conn = mysqli_connect ( 'server', 'username', 'password', 'db');

if ( isset ( $_GET['sentence'] ) ) {

  // Sets the table var
  $table = "";

  // Prevents SQL Injection
  $sentence = mysqli_real_escape_string ( $conn, $_GET['sentence'] );

  // Splits the sentence inputted by the user into an array of individual words
  $wordArr = explode (  " ", $sentence );

  // For loop to execute the SQL Query for each word
  for ( $x = 0; $x < count ( $wordArr ); $x++ ) {

    $word = $wordArr[$x];

    // SQL Query to information about the word (if it exists in the table) from DB
    $sqlFindWord = "SELECT * FROM `words` WHERE `word` = '$word'";

    // Executes the query
    $resultFindWord = mysqli_query ( $conn, $sqlFindWord );

    $resultFindWord = mysqli_fetch_assoc ( $resultFindWord );

    // If the word exists in the table...
    if ( $resultFindWord ) {

      $frequency = $resultFindWord['freq'] + 1;

      // SQL Query adds 1 to the word's frequency in the table
      $sqlUpdateFrequency = "UPDATE `words` SET `freq` = $frequency WHERE `word` = $word";

      // Executes SQL Query
      $resultUpdateFrequency = mysqli_query ( $conn, $sqlUpdateFrequency );

      // Adds word to HTML table
      $table .= "<tr><td>" . $word . "</td><td>" . $frequency . "</td></tr>";

    } else {

      // Word doesn't exist in the table, so it must be added with a frequency of 1
      $sqlAddWord = "INSERT INTO `words` (`word`, `freq`) VALUES ('$word', 1)";

      // Executes the query
      $resultAddWord = mysqli_query ( $conn, $sqlAddWord );

      // Adds word to the HTML table
      $table .= "<tr><td>" . $word . "</td><td>1</td></tr>";

    }
  }
}
?>

HTML HTML

<form action="#" method="GET" validate="">
  <input type="text" placeholder="Enter A Sentence" name="sentence" required />
  <input type="submit" value="Submit Sentence" />
</form>
<table>
  <tr>
    <th>Word</th>
    <th>Freq</th>
  </tr>
  <?php echo $table; ?>
</table>

If you have any questions about any part of the code, or if I misunderstood the question please tell me.如果您对代码的任何部分有任何疑问,或者我误解了问题,请告诉我。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM