matching text with data in database?
i am new to php, html and js.I am trying to make a website using php, html and js and what i want to is search. actually i have a database which is storing name of videos and their URL and i want to match entered text with name of video in db. for example if someone enters arith in search text box all names having arith word in them should be searched like ar开发者_Python百科ithmetic etc. how can i do this??
If you're using a MySQL database as I assume you are, you might start with looking at MySQL fulltext search. I started out with examples from the comments and developed my fulltext searches from there.
Assuming your DB is MySQL you're looking for the LIKE command: http://dev.mysql.com/doc/refman/5.0/en/string-comparison-functions.html
Your query would be something like:
$res = mysql_query("SELECT id, name, url FROM videos WHERE name LIKE '%arith%'");
The %
character is a wildcard pattern matcher saying "anything can be here". If you wanted to match anything beginning with the search term just remove the first wildcard matcher:
$res = mysql_query("SELECT id, name, url FROM videos WHERE name LIKE 'arith%'");
The first thing you need to do is tell us what database server you're using, as you're working with PHP I'll guess that your web hosting is "LAMP" (Linux, Apache, MySql and PHP) so here's a selection of links to tutorials for using MySql from PHP:
- Tutorial Introduction
- Selecting data from a MySql database in PHP
- PHP/MySql Tutorial
And specifically you're looking for the LIKE/% operator to use in your query, so take a look at:
- The MySql Documentation
- MySQL - LIKE and NOT LIKE
An example of the query in PHP would be:
$matchingvidoes = mysql_query("SELECT `name`, `url` FROM `videos` WHERE `name` LIKE '%arith%'");
精彩评论