Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/261.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jquery-ui/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 计算数据库行中有多少特定单词_Php_Mysql_Database_Count - Fatal编程技术网

Php 计算数据库行中有多少特定单词

Php 计算数据库行中有多少特定单词,php,mysql,database,count,Php,Mysql,Database,Count,我有两个单词列表和一个数据库,里面有上千篇新闻文章 我想计算一下数据库中每篇文章中有多少来自列表$baddwords和$goodwords的单词。接下来,我想将每行的两个结果($baddwords和$goodwords)保存在baddwords和goodwords列中。我将使用cronjob运行此脚本 我当前的表结构最后两行是空的 我想要的表格结构 最后两列中的$badwords和$goodwords数 我当前的PHP代码 <?php //the wordlists $badwords =

我有两个单词列表和一个数据库,里面有上千篇新闻文章

我想计算一下数据库中每篇文章中有多少来自列表$baddwords$goodwords的单词。接下来,我想将每行的两个结果($baddwords和$goodwords)保存在baddwordsgoodwords列中。我将使用cronjob运行此脚本

我当前的表结构最后两行是空的

我想要的表格结构 最后两列中的$badwords和$goodwords数

我当前的PHP代码

<?php
//the wordlists
$badwords = "depressive horrible";
$goodwords = "great";

//connection to the database
$servername = "localhost";
$username = "user";
$password = "pass";
$dbname = "db";

$conn = new mysqli($servername, $username, $password, $dbname);

// here is my sql query

$sql = " UPDATE news
set badwords = (SELECT count (*) from news
where newscontent LIKE '.%$badwords%.')";    

//close the connection
$conn->close();
?>

如果我正确理解了您的问题,您希望检查您的数据库中是否存在某个单词列表。在这种情况下,您正在寻找这样的查询(根据您使用的db类,在查询中也使用转义,例如()):

如果要显示每个单词列表在数据库中存在的次数,则需要:

SELECT    COUNT(*) AS `count`
         ,`newscontent` 
FROM      `news`
GROUP BY  `newscontent`
如果您想显示给定字数的字符串数量,这就是您要查找的内容:

<?php
  $sql = new mysqli($host, $user, $password, $database);
  $query = $sql->query('select * from `news`');
  $summary = [];

  while($record = $query->fetch_object()) { 
    $summary[count(explode(' ', $record->newscontent))]++;
  }

  echo '<pre>';
  print_r($summary);
  echo '</pre>';

你好,里昂,欢迎来到StackOverflow。请仔细阅读“.I'msorry Leon,但它仍然不清楚:即,您想在mysql或php中执行计数?“基于带单词的字符串”是什么意思?请添加更多细节并提供具体示例:表结构、字段内容示例、字符串示例、所需结果示例等…非常感谢@Ben!我重新措辞了我的问题,事实上,我在寻找一个稍微不同的解决方案。你的方法的组合应该可以做到这一点。。但是,到目前为止,我还没有找到解决方案。
SELECT    COUNT(*) AS `count`
         ,`newscontent` 
FROM      `news`
WHERE     `newscontent` = '" . $wordlist . "'
SELECT    COUNT(*) AS `count`
         ,`newscontent` 
FROM      `news`
GROUP BY  `newscontent`
<?php
  $sql = new mysqli($host, $user, $password, $database);
  $query = $sql->query('select * from `news`');
  $summary = [];

  while($record = $query->fetch_object()) { 
    $summary[count(explode(' ', $record->newscontent))]++;
  }

  echo '<pre>';
  print_r($summary);
  echo '</pre>';
<?php
  // your db connection ...

  // array with good and bad words
  $good = [
    'awesome',
    'neat',
    'fantastic',
    'great',
    // and so on
  ];

  $bad = [
    'horrible',
    'worst',
    'bad',
    'terrific',
    // and so on
  ];

  // if you keep using your string approach you can set $good and $bad with $good = explode(' ', $goodwords); and $bad = explode(' ', $badwords);

  // fetch the record you need
  $query = $sql->query('select * from `news` where `ID` = 1'); // insert parameter for your ID here instead of just 1
  $newsitem = $query->fetch_object();

  // set up good and bad word counters
  $totalGood = 0;
  $totalBad = 0;

  // check how many times each word is mentioned in newscontent
  foreach($good as $word) { 
    // add spaces arround the word to make sure the full word is matched, not a part
    $totalGood += substr_count($newsitem->newscontent, ' ' . $word . ' ');
  }

  // check how many times each word is mentioned in newscontent
  foreach($bad as $word) { 
    // add spaces arround the word to make sure the full word is matched, not a part
    $totalBad += substr_count($newsitem->newscontent, ' ' . $word . ' ');
  }  

  // update the record
  $sql->query("
    update `news` 
    set `badwords` = " . $totalBad . ",
        `goodword` = " . $totalGood . "
    where `ID` = " . $newsitem->ID);