【问题标题】:Find how many times every word is repeated in db找出每个单词在db中重复多少次
【发布时间】:2015-12-16 08:56:51
【问题描述】:

我正在使用 drupal 来管理我的内容。我想搜索我所有的内容标题和正文,并找出每个单词在整个内容中重复了多少次。

可能是通过sql查询,但我没有使用sql的经验。

有什么想法吗?

【问题讨论】:

  • 你能告诉我们你的努力吗?
  • 然后使用查询搜索并统计单词
  • 如果你没有sql经验,这对你来说并不容易,我建议你阅读基本的sql语法专门分组
  • 这是我的一些 php 代码。 SELECT id, title, body FROM table $items = array(); $result = mysql_query($sql); while(($row = mysql_fetch_assoc($result))) { $items = $row['title']+$row['body']; $final_results = explode(" ",$items[]); while($final_results) { INSERT INTO tags VALUES ($final_results[i]) } }

标签: mysql sql drupal


【解决方案1】:

此代码在 ANY 内容类型的正文字段和 ALL 字段中搜索特定字符串。您可以通过命令行运行它。假设您将其保存为“fieldsearch.php”,然后您可以将其运行为:

php fieldsearch.php "myStringForWhichToSearch"

您需要填写您的连接数据和数据库名称。它输出匹配节点的数组,但您可以将该输出格式化为您想要的任何内容(我推荐 csv)。

<?php


//Set Parameters here
$env = "dev"; // Options [dev|prod] - Defaults to dev

$prodConnection = array(
                        "host" => "",
                        "user" => "",
                        "pass" => ""
                        );


$devConnection = array(
                        "host" => "",
                        "user" => "",
                        "pass" => ""
                        );

//Use the selected env settings
if($env == "prod"){
  $connection = $prodConnection;
} else {
  $connection = $devConnection;
}



function getTables($con, $database){
  //Get the set of field tables
  $sql = "SHOW TABLES FROM $database";
  $result = mysqli_query($con, $sql);

  if (!$result) {
      echo "DB Error, could not list tables\n";
      echo 'MySQL Error: ' . mysql_error();
      exit;
  }

  $tables = array();
  while ($row = mysqli_fetch_row($result)) {
      $tables[] = $row[0];
  }

  mysqli_free_result($result);

  return $tables;
}

function getFieldTables($con,$database){
  $allTables = getTables($con, $database);

  $fieldTables = array();
  foreach($allTables as $key => $table){

    if( isFieldTable($table) ){
      $fieldTables[] = $table;
    }
  }

  //add the common tables
  $fieldTables[] = "field_data_body";
  $fieldTables[] = "field_data_comment_body";

  return $fieldTables;
}

function isFieldTable($table){
  //echo $table . "\n";
  if( stripos($table, "field_data_field") !== FALSE){
    return TRUE;
  }
}

//Set the search term here:
if (array_key_exists(1, $argv)) {
  $searchString = $argv[1];
}
else {
  die('usage: php fieldsearch.php "search string"' . "\n");
}

  $databaseName = "myDatabaseName";

  $outArray = array();

  //Connect
    $con=mysqli_connect($connection['host'],$connection['user'],$connection['pass'],$databasePrefix.$databaseNum);
  // Check connection
  if (mysqli_connect_errno()) {
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

  //getFieldTables
  $fieldTables = getFieldTables($con, $databaseName);

  //Query each field tables data for the string in question
  foreach($fieldTables as $key => $table){

    //get Field value column name
    $valueCol = str_replace("field_data_field_", '', $table);

    $result = mysqli_query($con,"SELECT 
                                      entity_id 
                                  FROM
                                      $table
                                  WHERE 
                                    field_" . $valueCol . "_value
                                  LIKE 
                                    '%$searchString%';");

    if($result){
      while($row = mysqli_fetch_assoc($result)){
        $dataArray[$table][$row['entity_id']]['nid'] = $row['entity_id'];
      }
    }
  }

  //Add the body table
  $result = mysqli_query($con,"SELECT 
                                    entity_id 
                                FROM
                                    field_data_body
                                WHERE 
                                  body_value
                                LIKE 
                                  '%$searchString%';");

  if($result){
    while($row = mysqli_fetch_assoc($result)){
      $dataArray['field_data_body'][$row['entity_id']]['nid'] = $row['entity_id'];
    }
  }

  var_dump($dataArray);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-10-06
    • 1970-01-01
    • 1970-01-01
    • 2023-02-04
    • 1970-01-01
    • 1970-01-01
    • 2021-07-25
    相关资源
    最近更新 更多