Welcome Guest ( Log In | Register)



2 Pages V   1 2 >  
Reply to this topicStart new topic
> Php Search Engine Script For Mysql Database
ivenms
post Aug 8 2006, 03:30 PM
Post #1


Member [Level 1]
****

Group: Members
Posts: 53
Joined: 6-August 06
Member No.: 27,912



A search engine is provided to facilitate the user with undemanding and clear-cut search options. The search facility includes simple search, search by title, search by word/phrase, ect… Thus, the user is at a safe distance from the risk of selecting files/folders ambiguously. In addition, a history of recent searches can be preserved for future perusal.


Now day’s visitors have a large option for his needs on internet and so visitors are not vesting their time by following the dead links on your site. So a search engine is essential for your site to attract your visitors and hand around on your site by directly deviating them to their interesting topics. This also make the visitors happy and the probability of next visit by them also been increased.


In here, I am trying to give some tips on how to create simple search engines on your database. Normally a large amount of webmasters now using open source and free database and programs like MySql with PHP scripting. So my search engine considered on the basis of MySql database and using PHP scripting.


For Database Tables a SQL query called LIKE is used for finding matching parts on your text fields on your table. This is normally used for search engines. An example for LIKE query used in search engine is as follows:

********************************************************************************

SELECT FIELDS FROM TABLE WHERE TEXTFIELD LIKE ' %keyforsearch%’
********************************************************************************


Here this query will select all rows from the table named TABLE on which its column TEXTFIELD containing search keyword. You can search on multiple column by using AND query. An example query for multiple search is as follows:



********************************************************************************

SELECT FIELDS FROM TABLE WHERE TITLE LIKE ' %keyforsearch%’ AND
TEXTFIELD LIKE '%keyforsearch%’
********************************************************************************



The FIELDS denotes all fields you want to select as a results. Multiple fields can be selected by using comma operator: FIELD1, FIELD2.


Now I am going to tell about the PHP script which makes use of this query for its database search. I am giving the PHP codes I used on my sites for database search. This code is so simple and optimized. You just want to change some parts and put the entire code to your site for working.
The 1st part is information about your site database. Put this part to the top of your PHP page. Change this part with your information about your database for connection.


CODE
**********************************************************
            $db_server = "localhost";
     $db_name = "database_name";
     $db_username = "database_username";
     $db_password = "database_password";
**********************************************************



The next part is a function which used for establish connection of database. No need of changing this part. Just put this codes to the page.


CODE
**********************************************************
function db_connect(){

    global $db_server;
    global $db_name;
    global $db_username;
    global $db_password;

    $con =@ mysql_connect($db_server,$db_username,$db_password);
    if ($con!=false){
        if (!(mysql_select_db($db_name,$con))){
            // cannot find database
            die("Cannot find Database");
        } else {
            // fine
        }
    } else {
        die ("Cannot connect to database");
    }
    return $con;
}
***********************************************************

The next part of search engine script is SQL generating and running function which retrieves the results of search. You have to change some portions on the function which denoted with <> change that part replacing with your information. Don’t forget to replace < > symbols because that makes syntax error. For changing this part, refer the previously explained portions on SQL QUERY.


CODE
***********************************************************
function db_search($key) {
        $con = db_connect();
    $key = mysql_escape_string($key);
    $sql = "SELECT DISTINCT <FIELDS RETRIVED> FROM <TABLE NAME> WHERE <FIELDNAME FOR SEARCH> LIKE '%".$key."%'";
        $result = mysql_query($sql,$con);
        mysql_close($con);
    return $result;
}
***********************************************************


The next part is for retrieving the posted search key posted on an html form. This part catches the search key and passes it to previous function db_search(). It also convert the result to an array for displaying it on your page as the result.



CODE
***********************************************************
if(isset($_REQUEST['key']))
{
   $key = $_REQUEST['key'];
   $result = db_search_bible($key);
  if($result == FALSE)
    $content = 'No Match Found';

else
    while ($row = mysql_fetch_row($result)) {
            $content .=  $row[0].', '.$row[1].', '.$row[2].’,  '.$row[3].'…<br>';
}

echo $content;
***********************************************************



The $content is the variable which stores the result. The selected fields will come in the format of $row[0], $row[1], $row[2],…… according to your selection. If you selected two fields named title and body then you can display them as selecting $row[0] for title and $row[1] for body.

If you want to see the result on a formatted style then ad some more code and replace a last part of the code by this. This part is for displaying the two selected fields TITLE and BODY. Change this display property with your structure.


CODE
**********************************************************
if(isset($_REQUEST['key']))
{
   $key = $_REQUEST['key'];
   $result = db_search_bible($key);
   $html = ‘<html><head><title>Search Results</title></head><body><table border=0 width=”100%”><!-contentŕ’</table></body></html>’;

  if($result == FALSE)
    $content = '<tr><td>No Match Found</td></tr>';

else
   {
    $content = “”;
    while ($row = mysql_fetch_row($result)) {
            $content .=  ‘<tr><td><h2>’.$row[0].'</h2> <br><br>'.$row[1].'</td></tr>’;
   }
}

$html = str_replace("<!--content-->",$content,$html);

echo $html;
**********************************************************


Your search engine script is ready. You now want an html form for submitting search words. Make sure that the form contains the text field naming “key” where to submit search words. An example for search form is given below:


CODE
********************************************
<form type=”GET” action=”filename.php”>
<input type=”text” name=”key”>
<input type=”submit” value=”Search”>
</form>
********************************************


That’s all. You now were owning a search engine which is capable of searching your database. Enjoy yourself by using this searching script. For more technical support, visit php.net.

By Iven Mathew Simon
--- Site Administrator of WebMasters-Forums.Com



This post has been edited by ivenms: Oct 1 2007, 03:56 PM
Go to the top of the page
 
+Quote Post
ivenms
post Aug 9 2006, 02:02 PM
Post #2


Member [Level 1]
****

Group: Members
Posts: 53
Joined: 6-August 06
Member No.: 27,912



Post your satisfactions here on my code for SQL DATABASE SEARCH SCRIPT. If there any error, Please mention that on here.
Go to the top of the page
 
+Quote Post
electron
post Aug 10 2006, 03:17 AM
Post #3


Premium Member
********

Group: Members
Posts: 162
Joined: 10-May 06
Member No.: 23,375



Well if this search script is for your site than quite satisfactory.
But it does lack quite a lot of security POST handling.

Also you have named the search function "db_search()" but while using it you referred to it as "db_search_bible()"
People will get confused.

Another thing is that your result page wont work at all.
As you have put in the '$html' variable a comment ' <!-contentŕ’ ' and while replacing it you are using '<!--content-->'.

The replace wont take place dude(I am sorry if i am rude).

One last thing this is a search script that would search within a small database.

You cant call it as a SEARCH ENGINE.
For the real tough part of a search engine is to get and INDEX the result of a link it has and then find new links again and then store them.

That is a real SEARCH ENGINE dude.There you got to consider storing the whole of the WEB and you require PB's(Peda Bytes) of space.

So alot has to be done on the script that you have written and then rightly call it a search engine.

Sorry if i am being very rude.
Go to the top of the page
 
+Quote Post
ivenms
post Aug 10 2006, 07:12 AM
Post #4


Member [Level 1]
****

Group: Members
Posts: 53
Joined: 6-August 06
Member No.: 27,912



Thanks for your valuable informations given. The error happened because of careless typing. The code after replacing bugs is as follows:

CODE

******************************************************
     $db_server = "localhost";
     $db_name = "database_name";
     $db_username = "database_username";
     $db_password = "database_password";
******************************************************

**********************************************************
function db_connect(){

    global $db_server;
    global $db_name;
    global $db_username;
    global $db_password;

    $con =@ mysql_connect($db_server,$db_username,$db_password);
    if ($con!=false){
        if (!(mysql_select_db($db_name,$con))){
            // cannot find database
            die("Cannot find Database");
        } else {
            // fine
        }
    } else {
        die ("Cannot connect to database");
    }
    return $con;
}
***********************************************************

***********************************************************
function db_search($key) {
        $con = db_connect();
    $key = mysql_escape_string($key);
    $sql = "SELECT DISTINCT <FIELDS RETRIVED> FROM <TABLE NAME> WHERE <FIELDNAME FOR SEARCH> LIKE '%".$key."%'";
        $result = mysql_query($sql,$con);
        mysql_close($con);
    return $result;
}
***********************************************************

***********************************************************
if(isset($_REQUEST['key']))
{
   $key = $_REQUEST['key'];
   $result = db_search($key);
  if($result == FALSE)
    $content = 'No Match Found';

else
    while ($row = mysql_fetch_row($result)) {
            $content .=  $row[0].', '.$row[1].', '.$row[2].’,  '.$row[3].'…<br>';
}

echo $content;
***********************************************************

**********************************************************
if(isset($_REQUEST['key']))
{
   $key = $_REQUEST['key'];
   $result = db_search_bible($key);
   $html = ‘<html><head><title>Search Results</title></head><body><table border=0 width=”100%”><!-content--></table></body></html>’;

  if($result == FALSE)
    $content = '<tr><td>No Match Found</td></tr>';

else
   {
    $content = “”;
    while ($row = mysql_fetch_row($result)) {
            $content .=  ‘<tr><td><h2>’.$row[0].'</h2> <br><br>'.$row[1].'</td></tr>’;
   }
}

$html = str_replace("<!--content-->",$content,$html);

echo $html;
**********************************************************

********************************************
<form type=”GET” action=”filename.php”>
<input type=”text” name=”key”>
<input type=”submit” value=”Search”>
</form>
********************************************


The mentioned script is only a path which leads you to built your own database search. This is not a full script which can installed for your site. I am giving you an idea. I just explaining what I done for searching my database and calling it as my search engine. Any problem with that?
Go to the top of the page
 
+Quote Post
electron
post Aug 11 2006, 03:05 AM
Post #5


Premium Member
********

Group: Members
Posts: 162
Joined: 10-May 06
Member No.: 23,375



No that is good now.
But I think u could improve this greatly.
For instance if the user typed inmany keywords you are not taking them seperately but as one whole string and trying to look for that as one string.

You might consider by exploding the string and then search for the results.

Best of luck
Go to the top of the page
 
+Quote Post
ivenms
post Aug 11 2006, 04:35 AM
Post #6


Member [Level 1]
****

Group: Members
Posts: 53
Joined: 6-August 06
Member No.: 27,912



Ya I know that works well by parsing the searching key and divide it into seperate keywords. If you have other ideas, make it into executable programming language and post it. So the other can see your ideas and use it. Make this script as base and develop more advanced scripts.

Good Luck.
Go to the top of the page
 
+Quote Post
electron
post Aug 16 2006, 03:26 AM
Post #7


Premium Member
********

Group: Members
Posts: 162
Joined: 10-May 06
Member No.: 23,375



Well i am working on some program now and it would be ready soon for beta testing to the public.
There are others who provide the same software BUT i want to be better than them and I can do it better than them if not in the first BETA Release.

If you know PHP , MySQL or JavaScript you could be one of its developers later after it goes public.

Go to the top of the page
 
+Quote Post
SpinCode
post Jan 24 2007, 06:58 PM
Post #8


Newbie
*

Group: Members
Posts: 1
Joined: 24-January 07
Member No.: 37,774