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

CSS3 achieves cool 3D rotation perspective effect

CSS3 achieves cool 3D rotation perspective 3D ani...

How to import Chinese data into csv in Navicat for SQLite

This article shares with you the specific method ...

HTML code to add quantity badge to message button

HTML code: <a onclick="goMessage();"...

Implementation steps for setting up the React+Ant Design development environment

Basics 1. Use scaffolding to create a project and...

Summary of HTML Hack Tags in IE Browser

Copy code The code is as follows: <!--[if !IE]...

Detailed explanation of the installation and use of Vue-Router

Table of contents Install Basic configuration of ...

HTML Tutorial: Collection of commonly used HTML tags (4)

These introduced HTML tags do not necessarily ful...

How to set up remote access to a server by specifying an IP address in Windows

We have many servers that are often interfered wi...

About Zabbix custom monitoring items and triggers

Table of contents 1. Monitoring port Relationship...

How to use dl(dt,dd), ul(li), ol(li) in HTML

HTML <dl> Tag #Definition and Usage The <...

A brief discussion on the problem of forgotten mysql password and login error

If you forget your MySQL login password, the solu...

Running PostgreSQL in Docker and recommending several connection tools

1 Introduction PostgreSQL is a free software obje...

How to modify iTunes backup path under Windows

0. Preparation: • Close iTunes • Kill the service...

Complete steps to configure IP address in Ubuntu 18.04 LTS

Preface The method of configuring IP addresses in...