Data retrieval from Database
Title
Question
Can anyone tell me how the search function is done in websites??? i.e Some websites provide online dictionary, I think they use database like MySQL , to store words and meanings in tables , if a user enters a word in browser and hits "search" the corresponding word appears with meaning , how the "search function" above is done??? do they use any algorithm ? or simply storing and retrieving using the function like mysql_fetch_array()????
I messed up in this please help me to get cleared!
PHP-and-MySQL MySQL-Part-2
Answers:
<span style="line-height: 15px; font-family: arial, sans-serif;"><font size="3">
</font></span>
</font></span>
<span style="line-height: 15px; font-family: arial, sans-serif;"><font size="3">Hi Velu,</font></span>
<span style="line-height: 15px; font-family: arial, sans-serif; font-size: small;">
</span>
</span>
<span style="line-height: 15px; font-family: arial, sans-serif; font-size: small;">Consider you have a simple MySQL table named "</span>dictionary<span style="line-height: 15px; font-family: arial, sans-serif; font-size: small;">" with the following structure and some entries.</span>
<font face="arial, sans-serif" size="2"><span style="line-height: 15px;">
</span></font>
</span></font>
<font face="arial, sans-serif" size="2"><span style="line-height: 15px;">------------------------------------------</span></font>
<font face="arial, sans-serif" size="2"><span style="line-height: 15px;">Column | Type</span></font>
<font face="arial, sans-serif" size="2"><span style="line-height: 15px;">------------------------------------------</span></font>
<font face="arial, sans-serif" size="2"><span style="line-height: 15px;">word | varchar(200)</span></font>
<font face="arial, sans-serif" size="2"><span style="line-height: 15px;">meaning | text</span></font>
<font face="arial, sans-serif" size="2"><span style="line-height: 15px;">------------------------------------------</span></font>
<font face="arial, sans-serif" size="2"><span style="line-height: 15px;">
</span></font>
</span></font>
<font face="arial, sans-serif" size="2"><span style="line-height: 15px;">Now create a search.phpfile with the following content.</span></font>
<font face="arial, sans-serif" size="2"><span style="line-height: 15px;">
</span></font>
</span></font>
<font face="arial, sans-serif" size="2"><span style="line-height: 15px;"><!DOCTYPE html></span></font>
<font face="arial, sans-serif" size="2"><span style="line-height: 15px;"><html lang="en"></span></font>
<font face="arial, sans-serif" size="2"><span style="line-height: 15px;"><head></span></font>
<font face="arial, sans-serif" size="2"><span style="line-height: 15px;"><meta charset="UTF-8"></span></font>
<font face="arial, sans-serif" size="2"><span style="line-height: 15px;"><title>Search</title></span></font>
<font face="arial, sans-serif" size="2"><span style="line-height: 15px;"></head></span></font>
<font face="arial, sans-serif" size="2"><span style="line-height: 15px;"><body></span></font>
<font face="arial, sans-serif" size="2"><span style="line-height: 15px;">
</span></font>
</span></font>
<font face="arial, sans-serif" size="2"><span style="line-height: 15px;"><h3>Please enter your search term.</h3></span></font>
<font face="arial, sans-serif" size="2"><span style="line-height: 15px;"></span></font>
<font face="arial, sans-serif" size="2"><span style="line-height: 15px;"><!-- form to enter the search term --></span></font>
<font face="arial, sans-serif" size="2"><span style="line-height: 15px;"><form method="post" action=""></span></font>
<font face="arial, sans-serif" size="2"><span style="line-height: 15px;"><input type="text" name="term"></span></font>
<font face="arial, sans-serif" size="2"><span style="line-height: 15px;"><input type="submit" value="Search"></span></font>
<font face="arial, sans-serif" size="2"><span style="line-height: 15px;"></form></span></font>
<font face="arial, sans-serif" size="2"><span style="line-height: 15px;">
</span></font>
</span></font>
<font face="arial, sans-serif" size="2"><span style="line-height: 15px;"><?php</span></font>
<font face="arial, sans-serif" size="2"><span style="line-height: 15px;">/* check if the method is POST */</span></font>
<font face="arial, sans-serif" size="2"><span style="line-height: 15px;">if($_SERVER["REQUEST_METHOD"] === "POST") {</span></font>
<font face="arial, sans-serif" size="2"><span style="line-height: 15px;">$term = $_POST["term"];</span></font>
<font face="arial, sans-serif" size="2"><span style="line-height: 15px;"></span></font>
<font face="arial, sans-serif" size="2"><span style="line-height: 15px;">/* create a mysql connection */</span></font>
<font face="arial, sans-serif" size="2"><span style="line-height: 15px;">$connection = mysql_connect("localhost", "username", "password");</span></font>
<font face="arial, sans-serif" size="2"><span style="line-height: 15px;">if(!$connection) {</span></font>
<font face="arial, sans-serif" size="2"><span style="line-height: 15px;">die("Database connection failed" . mysql_error());</span></font>
<font face="arial, sans-serif" size="2"><span style="line-height: 15px;">}</span></font>
<font face="arial, sans-serif" size="2"><span style="line-height: 15px;"></span></font>
<font face="arial, sans-serif" size="2"><span style="line-height: 15px;">/* select a mysql database */</span></font>
<font face="arial, sans-serif" size="2"><span style="line-height: 15px;">$mysql_select = mysql_select_db("database_name", $connection);</span></font>
<font face="arial, sans-serif" size="2"><span style="line-height: 15px;">if(!$mysql_select) {</span></font>
<font face="arial, sans-serif" size="2"><span style="line-height: 15px;">die("Database selection failed" . mysql_error());</span></font>
<font face="arial, sans-serif" size="2"><span style="line-height: 15px;">}</span></font>
<font face="arial, sans-serif" size="2"><span style="line-height: 15px;"></span></font>
<font face="arial, sans-serif" size="2"><span style="line-height: 15px;">/* use the mysql query with LIKE clause */</span></font>
<font face="arial, sans-serif" size="2"><span style="line-height: 15px;">$result = mysql_query(</span></font>
<font face="arial, sans-serif" size="2"><span style="line-height: 15px;">"SELECT word, meaning FROM dictionary WHERE word LIKE '%{$term}%'", $connection</span></font>
<font face="arial, sans-serif" size="2"><span style="line-height: 15px;">);</span></font>
<font face="arial, sans-serif" size="2"><span style="line-height: 15px;"></span></font>
<font face="arial, sans-serif" size="2"><span style="line-height: 15px;">/* displaying results if any */</span></font>
<font face="arial, sans-serif" size="2"><span style="line-height: 15px;">echo "Your search term = " . $term . "<br>";</span></font>
<font face="arial, sans-serif" size="2"><span style="line-height: 15px;">echo "<h3>Results:</h3>" . "<hr>";</span></font>
<font face="arial, sans-serif" size="2"><span style="line-height: 15px;">while($row = mysql_fetch_array($result)) {</span></font>
<font face="arial, sans-serif" size="2"><span style="line-height: 15px;">echo "Word = " . $row["word"] . "<br>";</span></font>
<font face="arial, sans-serif" size="2"><span style="line-height: 15px;">echo "Meaning = " . $row["meaning"] . "<br>";</span></font>
<font face="arial, sans-serif" size="2"><span style="line-height: 15px;">echo "<hr>";</span></font>
<font face="arial, sans-serif" size="2"><span style="line-height: 15px;">}</span></font>
<font face="arial, sans-serif" size="2"><span style="line-height: 15px;">}</span></font>
<font face="arial, sans-serif" size="2"><span style="line-height: 15px;">?></span></font>
<font face="arial, sans-serif" size="2"><span style="line-height: 15px;"></body></span></font>
<font face="arial, sans-serif" size="2"><span style="line-height: 15px;"></html></span></font>
<font face="arial, sans-serif" size="2"><span style="line-height: 15px;">
</span></font>
</span></font>
<font face="arial, sans-serif" size="2"><span style="line-height: 15px;">Output:</span></font>
<font face="arial, sans-serif" size="2"><span style="line-height: 15px;">
</span></font>
</span></font>
<img align="none" alt="" src="http://i.imgur.com/1djeaHD.png">
Note (! Important):
1) change the username, password, and database_name to match your settings.
2) if you want exact term search, you can replace '%{$term}%'to '{$term}' in the query.
3) custom algorithms/ filters can be implemented on the retrieved data, in the above example inside the while loop.
4) in case you are in a doubt where to put the above submit.phpfile.
4.a) In GNU/Linux, please install apache2 web-server, and php5.
I hope you already have mysql installed by now.
Put the submit.php in /var/www/folder. (Grant it required permissions.)
4.b) In Macintosh, install <a href="http://www.mamp.info/en/index.html" target="_blank" title="MAMP">MAMP</a>,
Its default serving directory will be /Applications/MAMP/htdocs.
4.c) In Windoze install <a href="http://www.wampserver.com/en/" target="_blank" title="WAMP">WAMP</a>.
Its default serving directory will be c:\\wamp\\www.
Thanks,
Jayaram
Thank you sir,
<html>
<head>......</head>
<body>
<?php
echo "welcome to the php learning site!";
?>
Actually I am using XAMPP server in windows 7 OS. But before that I tried php in BOSS GNU/LINUX (this linux is provided with the Government Laptops for school and college students ,by Govt. of Tamil Nadu). I installed all the packages Apache, php-common ,php5..... etc, before that I installed MySQL packages which is running on 127.0.0.1 (localhost) .
Problem is , when I saved my sample.php file like,
<html>
<head>......</head>
<body>
<?php
echo "welcome to the php learning site!";
?>
</body>
</html>
and opened in browser, instead of opening as web page , the sample.php is downloaded and automatically stored in Downloads directory.(I tried many times with different php code but doesn,t work!)
Finally, I wanted to clarify whether my apache is working? so in Terminal I typed a command
Finally, I wanted to clarify whether my apache is working? so in Terminal I typed a command
<code>/etc/init.d/apache2 stop </code>
<font color="#c7254e" face="Monaco, Menlo, Consolas, Courier New, monospace"><span style="font-size: 14px; line-height: 19px; white-space: nowrap;">
</span></font><code>/etc/init.d/apache2 start and found an error message </code>
</span></font><code>/etc/init.d/apache2 start and found an error message </code>
<code>"could not successfully generate fully qualified domain name for 127.0.0.0,</code>
<code>port 80 is used by some other process"</code>
<code>
<span style="background-color: rgb(250, 250, 250); color: rgb(34, 34, 34); font-family: 'Varela Round', sans-serif; font-size: 15px; line-height: 1.428571429;">I assumed myself that the MySQL which I previously installed is using this port and running on 127.0.0.1. </span>
<span style="background-color: rgb(250, 250, 250); color: rgb(34, 34, 34); font-family: 'Varela Round', sans-serif; font-size: 15px; line-height: 1.428571429;">I was unable t</span><span style="background-color: rgb(250, 250, 250); color: rgb(34, 34, 34); font-family: 'Varela Round', sans-serif; font-size: 15px; line-height: 1.428571429;">o solve this and totally gave up!!!!</span>
<span style="background-color: rgb(250, 250, 250); color: rgb(34, 34, 34); font-family: 'Varela Round', sans-serif; font-size: 15px; line-height: 1.428571429;">
</span>
</span>
<font color="#222222" face="Varela Round, sans-serif"><span style="font-size: 15px; line-height: 21px;">I think u can understand </span></font><span style="background-color: rgb(250, 250, 250); color: rgb(34, 34, 34); font-family: 'Varela Round', sans-serif; font-size: 15px; line-height: 1.428571429;">the problem if so please help me I like working with LINUX , I will be happy</span><span style="background-color: rgb(250, 250, 250); color: rgb(34, 34, 34); font-family: 'Varela Round', sans-serif; font-size: 15px; line-height: 1.428571429;"> if this is solved! </span>
<span style="background-color: rgb(250, 250, 250); color: rgb(34, 34, 34); font-family: 'Varela Round', sans-serif; font-size: 15px; line-height: 1.428571429;">please help me!</span>
</code>Hi again,
First check whether the package libapache2-mod-php5 is installed in BOSS Linux.
Command to run in terminal ( to check whether package is installed ):
dpkg -s libapache2-mod-php5
If it is not installed, you might get an output somewhat similar like this:
------------------------------------------------------------------<span style="line-height: 1.428571429;">----------------</span>
Package `libapache2-mod-php5' is not installed and no info is available.
Use dpkg --info (= dpkg-deb --info) to examine archive files,
and dpkg --contents (= dpkg-deb --contents) to list their contents.
----------------------------------------------------------------------------------
Now install libapache2-mod-php5:
Command:
apt-get install libapache2-mod-php5
After installation restart your apache2 web server:
Command:
/etc/init.d/apache2 restart
Note:
1) You need to have required permissions to install packages.
2) Since BOSS is Debain based Operating System, you can easily google your issues as if you are using a Debian OS.
<span style="line-height: 1.428571429;">You will get better results since the Debian community is larger than BOSS.</span>
Let me know the if you need any more help.
Thanks.
Login to add comment