puccavn
Learn how to make a search engine using the power of PHP and mySQL.
Here is an useful script that anyone can use to search a mySQL database and spit out the results as you want them. They have to be broken down into two separate files. One is an HTML form with action leading to the script PHP file. Lets work on the form first because it is the easiest.
Here is what we have to write in English for the Form:
Dec
Insert the values into the correct database.
Get a conformation that it has been uploaded
Here it is in PHP:
Search:
<form method="post" action="search.php">
<input type="text" name="search" size=25 maxlength=25>
<input type="Submit" name="Submit" value="Submit">
</form>
And that is basically all the code we have to write for the form. Now the hard part, writing the processing, which isn't really as hard as it seems. Here is what we have to write in English for the Processing:
Connect to the server and select the right database.
Grab the result, as you can see, its a fairly obvious line of code.
Run a while loop running the results we got.
Define each result a variable and spit it out.
Else say "Nothing Found"
Here it is in PHP:
<?
//connect to server
mysql_pconnect("server","server","password");
//select the db
mysql_select_db("database");
//get the results and store them in $result
$result = mysql_query ("SELECT * FROM whatevertable WHERE
whatevercolumn LIKE '%$search%'");
//run the while loop
while($r=mysql_fetch_array($result))
{
//grab the result
$whatevercolumn=$r["whatevercolumn"];
//print it out
echo $whatevercolumn;
echo "<br>";
}
//else nothing is found
else
{
echo "Nothing found";
}
?>
Couple of things: You have to rename the the name and password for the connection to the mySQL. You have to change the database name. Also change the whatevertable and whatevercolumn to the right table and column you want to search from. That's about it!
Here is an useful script that anyone can use to search a mySQL database and spit out the results as you want them. They have to be broken down into two separate files. One is an HTML form with action leading to the script PHP file. Lets work on the form first because it is the easiest.
Here is what we have to write in English for the Form:
Dec
Insert the values into the correct database.
Get a conformation that it has been uploaded
Here it is in PHP:
Search:
<form method="post" action="search.php">
<input type="text" name="search" size=25 maxlength=25>
<input type="Submit" name="Submit" value="Submit">
</form>
And that is basically all the code we have to write for the form. Now the hard part, writing the processing, which isn't really as hard as it seems. Here is what we have to write in English for the Processing:
Connect to the server and select the right database.
Grab the result, as you can see, its a fairly obvious line of code.
Run a while loop running the results we got.
Define each result a variable and spit it out.
Else say "Nothing Found"
Here it is in PHP:
<?
//connect to server
mysql_pconnect("server","server","password");
//select the db
mysql_select_db("database");
//get the results and store them in $result
$result = mysql_query ("SELECT * FROM whatevertable WHERE
whatevercolumn LIKE '%$search%'");
//run the while loop
while($r=mysql_fetch_array($result))
{
//grab the result
$whatevercolumn=$r["whatevercolumn"];
//print it out
echo $whatevercolumn;
echo "<br>";
}
//else nothing is found
else
{
echo "Nothing found";
}
?>
Couple of things: You have to rename the the name and password for the connection to the mySQL. You have to change the database name. Also change the whatevertable and whatevercolumn to the right table and column you want to search from. That's about it!
