14 February 2011

How to write data in your database in Languages other than English?

Sometimes while using some website like Orkut ,  you might have seen a dictionary , using which you can translate your scrap into hindi language and send it to your friend.
Now a question might have struck into your mind, as all these scraps must be stored in some database which only allows only english language then how is it possible to store data in language other than English???
Well this post of mine will answer your question
Let us start , we all know that all major DB’s support UTF8. Using this feature, we will insert a word  हेल्लो that is in Hindi language in our database table.
For that let us create a table named post in database manish.
In order to create database and table you have to execute below mentioned queries first:-
Create database manish;
Create table post(name varchar(20));
Now if you try to insert this word in database without specifying UTF8 ,you should see some error like this:-
Incorrect string value: '\xE0\xA4\xB9\xE0\xA5\x87...'
Now in order to remove this error, you have to execute below mentioned sql query
ALTER TABLE post MODIFY name VARCHAR(20) CHARACTER SET UTF8;

Now, try to insert the hindi value and save it. You shud see the hindi text 

06 February 2011

How to develop a search engine using PHP-Part 1?

Today almost all websites have search capability, Using which you can easily search the contents of website by entering some keywords in a text box .So through this post, I will show you how to build a search engine using a keyword based approach. For this, We will use MySQL's extremely powerful full-text search capabilities.

Now you might be wondering about what is Full-Text searching actually?Following points will give you a clear idea about it.

1:-  A Full-Text search makes use of full text indexes. In MySQL it  is an index of type Full-Text.These indexes are set up in specific fields of a table.
2:-  Full-text indexes can be used only with MyISAM tables, and can be created only for CHAR, VARCHAR, or TEXT columns.
3:-  A Full Text index definition can be given in the CREATE TABLE statement when a table is created, or added later using ALTER TABLE or CREATE INDEX.

Basic Features
1:- Full-text searches are faster than other search methods such as wildcard or character based searches, which are commonly performed using MySQL's LIKE command. 
2:- It is ideal for extremely large databases that contain thousands or even millions of rows. Computations are performed faster and rows can be ranked based on search relevance, which is returned as a decimal number by MySQL.


How it is performed?
Full-text searching is performed using MATCH() ... AGAINST syntax. MATCH() takes a comma-separated list that names the columns to be searched. AGAINST takes a string to search for, and an optional modifier that indicates what type of search to perform.


Let us take a simple example
First of all we will create a simple database using command create database ;
Create database manish;
Use manish
Now create a table named data with 3 attributes


CREATE TABLE data (
    ->   id INT UNSIGNED AUTO_INCREMENT NOT NULL PRIMARY KEY,
    ->   title VARCHAR(200),
    ->   body TEXT,
    ->   FULLTEXT (title,body)
    -> );

Query OK, 0 rows affected (0.00 sec)Here we have set an index on title and body attribute


mysql> INSERT INTO data (title,body) VALUES
    -> ('MySQL Tutorial','DBMS stands for DataBase ...'),
    -> ('How To Use MySQL Well','After you went through a ...'),
    -> ('Optimizing MySQL','In this tutorial we will show ...');

Now let us perform some search using Match()..Against() Keyword. Here there is an important point to be consider,Because full-text searching was designed for larger databases, it is possible for MySQL to return incorrect results when it's used on tables containing smaller amount records

mysql> SELECT * FROM articles
    -> WHERE MATCH (title,body) AGAINST ('database');

+----+-------------------+------------------------------------------+
| id | title             | body                                     |
+----+-------------------+------------------------------------------
|  1 | MySQL Tutorial    | DBMS stands for DataBase ...             |

So here i will  complete the first part of this tutorial ,in next part we will try our hands on some php scripting and will try to implement the actual search engine