MySQL full-text search Chinese solution and example code

MySQL full-text search Chinese solution and example code

MySQL full text search Chinese solution

     Recently, the company project requires such a function, which is to search for Chinese in the database. It is very tricky. I searched the Internet for information and found similar articles. I record them here and hope they can help everyone.

Example code:

<?php
/*
MySQL full-text search Chinese solution!
*/
error_reporting(E_ERROR | E_WARNING | E_PARSE);
ini_set('display_errors', '1');
//Database support class SaeMysql{
//phpmysql operation class}
$DBS=new SaeMysql;
//Add data to echo '2';
/*Create a data table*/
$DBS->runSql('CREATE TABLE IF NOT EXISTS `ces_articles` (
`id` mediumint(8) unsigned NOT NULL AUTO_INCREMENT,
`title` varchar(255) COLLATE utf8_unicode_ci NOT NULL DEFAULT \'\',
`url` varchar(255) CHARACTER SET utf8 COLLATE utf8_bin DEFAULT NULL,
PRIMARY KEY (`id`),
FULLTEXT KEY `url` (`url`)
)ENGINE=MyISAM AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci ROW_FORMAT=DYNAMIC');
/*Add data*/
$title='I love you all, hello everyone';
$DBS->runSql('INSERT INTO `ces_articles` (id,title,url) VALUES (0,\''. $title.'\',\''.str_replace('\u','u',trim(json_encode($title))).'\')');
$title='What is China';
$DBS->runSql('INSERT INTO `ces_articles` (id,title,url) VALUES (0,\''. $title.'\',\''.str_replace('\u','u',trim(json_encode($title))).'\')');
$title='http://ask.1912news.com';
$DBS->runSql('INSERT INTO `ces_articles` (id,title,url) VALUES (0,\''. $title.'\',\''.str_replace('\u','u',trim(json_encode($title))).'\')');
$title='Question and Answer System';
$DBS->runSql('INSERT INTO `ces_articles` (id,title,url) VALUES (0,\''. $title.'\',\''.str_replace('\u','u',trim(json_encode($title))).'\')');
$title='1912.com';
$DBS->runSql('INSERT INTO `ces_articles` (id,title,url) VALUES (0,\''. $title.'\',\''.str_replace('\u','u',trim(json_encode($title))).'\')');
$title = '09 Network';
$DBS->runSql('INSERT INTO `ces_articles` (id,title,url) VALUES (0,\''. $title.'\',\''.str_replace('\u','u',trim(json_encode($title))).'\')');
//search:
$_GET['q']="中国";
echo 'q';
if(isset($_GET['q'])){$sql=' match(url) against (\''.str_replace('\u','u',trim(json_encode($_GET['q']))).'\' IN BOOLEAN MODE)';}
$query = $DBS->getData('SELECT * FROM `ces_articles` where '.$sql.' LIMIT 10');
echo 'q';
if($query){
foreach ($query as $article){
echo $article['id'];
}
}

?>

Thank you for reading, I hope it can help you, thank you for your support of this site!

You may also be interested in:
  • In-depth analysis of Chinese full-text search in MySQL 5.7
  • Mysql implementation of full-text search and keyword scoring method example
  • MySQL full-text search usage examples
  • MySQL 5.7.25 full-text search tutorial

<<:  Detailed explanation of the installation commands and usage of Docker and FastDFS

>>:  Summary of Vue 3 custom directive development

Recommend

Pure CSS to add style to select (no script) implementation

Change the default style of select, usually throug...

Detailed explanation of JavaScript's garbage collection mechanism

Table of contents Why do we need garbage collecti...

Perfect solution to Google Chrome autofill problem

In Google Chrome, after successful login, Google ...

CSS Sticky Footer Implementation Code

This article introduces the CSS Sticky Footer imp...

How to rename the table in MySQL and what to pay attention to

Table of contents 1. Rename table method 2. Notes...

Introduction to html form control disabled attributes readonly VS disabled

There are two ways to disable form submission in ...

Detailed explanation of simple snow effect example using JS

Table of contents Preface Main implementation cod...

Install Docker on CentOS 7

If you don't have a Linux system, please refe...

How to visualize sketched charts in Vue.js using RoughViz

introduce A chart is a graphical representation o...

Compile CPP files using G++ in Ubuntu

When I used g++ to compile the cpp file for the f...

An in-depth summary of MySQL time setting considerations

Does time really exist? Some people believe that ...

Docker image export, import and copy example analysis

The first solution is to push the image to a publ...