MySQL Text Searching with PHP
Posted On September 20, 2007 by Rose Mary filed under Internet
When we start design a website with PHP and MySQL database and we really desire to have search functionality. To create -text search with PHP you need PHP, MySQL and a Web Server capable of parsing PHP pages, like Apache.
This search script does not spider all your pages by crawling the links, so the content you want to be searchable must be within the MySQL database. Knowledge of PHP and MySQL is also necessary because this script is just the bones of a working solution.
Now we will write the script how to build the search.
Open the Text editor or the PHP compatible editor, add the following code in the <body> tag of the HTML code. This is the form which is contain the textfield to enter the string.
| ................................................................................................................................................................. <form name="form" action="searchscript.php" method="get"> <input type="text" name="query" /> <input type="submit" name="Submit" value="Search" /> </form> |
Now we will add the actual PHP script for searching the test.
add the following code in the Seatchscript.php page.
| ................................................................................................................................................................. $limit=10; // check for an empty string and display a message. we're using $numresults=mysql_query($query); // If we have no results, offer a google search as an alternative if ($numrows == 0) // google // next determine if s has been passed to script, if not use 0 //break before paging // calculate number of pages needing links if ($numrows%$limit) { // not last page so give NEXT link $news=$s+$limit; echo " <a href=\"$PHP_SELF?s=$news&q=$var\">Next 10 >></a>"; } $a = $s + ($limit) ; |
The above code first accepts the string from the User and check for the string. If the string find in the column then it will display total number of similar values found in the column. Suppose if the value not fond in the particular column then it will display “Sorry, your search: "asp.net" returned zero results” Click here to try the search on Google. Then it will take you to the Google search.
Later you can figure out how to expand it to something more complicated yourself according to your requirements.
