For male power search php author id. How to make a multi-word search algorithm in PHP from a database. Detailed contact search

In today's lesson we will look at searching php mysql. There are a huge variety of search scripts, some use a database, others do without it, there are advanced searches with precise sorting. We will focus on the usual simple search, which looks for information on two tables from the database MySQL.

First step. MySQL database.

Creating a database search_lite, it has two tables news- news and vac- vacancies.

Set privileges:

Login - " root",

Password - "",

Host - " localhost".

Dump the news table.

Table structure for table `news` -- CREATE TABLE IF NOT EXISTS `news` (`id` int(2) NOT NULL, `title` varchar(255) NOT NULL, `text` text NOT NULL, PRIMARY KEY (`id `)) ENGINE=MyISAM DEFAULT CHARSET=cp1251;

Dump table vac - vacancies.

Table structure for table `vac` -- CREATE TABLE IF NOT EXISTS `vac` (`id` int(2) NOT NULL AUTO_INCREMENT, `title` varchar(255) NOT NULL, `text` text NOT NULL, PRIMARY KEY (` id`)) ENGINE=MyISAM DEFAULT CHARSET=cp1251 AUTO_INCREMENT=3 ;

Second step. HTML and PHP.

Connecting to the database. Set a condition on a variable $search, wherein search query is not entered, or it is less than 4 characters, and we also check for slashes and truncate the variable. We create two queries that look for two tables in the database news And vac by search condition by field text. (You can already choose how many tables you will need for the search. For example, you will have tables lessons, materials, articles etc.. You can also change the search conditions, instead of the field text, you can select a field title or another field.) Then we indicate how many characters are in the text when displaying the search in the variable $symbols. If in the tables news And vac Nothing was found in the search, we display a message. If found, display data from two tables news And vac for a given search query.




Top