Creating a Code Search Engine with PHP and MySQL(2)
2007-04-15 15:27
666 查看
原文:http://www.developer.com/db/article.php/10920_3671021_2
Using the search form to search for code consisting of the keyword "array" would produce output similar to this:
Finally, the displaycode.php script is used to display the script contents. It looks like this:
Finally, the displaycode.php script is used to display the script contents. It looks like this:
Clicking on the first result produces output similar to the following:
I hope this tutorial sheds some insight into how you can not only use MySQL's fulltext search capabilities to perform powerful searches against your database, but also introduces some of PHP's interesting text-related functions (nl2br(), htmlentities(), and ucfirst(), to name a few). Of course, one could easily extend what was demonstrated here to implement far more powerful search capabilities, boolean searches for instance. Be sure to check out the MySQL manual for a complete accounting of what's possible!
GET['id']);
$query = "SELECT id, title, chapter, code FROM code WHERE id='$id'";
$result = mysql_query($query);
// If results were found, output them
if (mysql_num_rows($result) > 0) {
$row = mysql_fetch_array($result);
printf("<h4>Chapter %s - %s</h4>", $row['chapter'], ucfirst($row['title']));
// Convert the newline characters and HTML entities before displaying
printf("%s", ^nl2br^(htmlentities($row['code'])));
} else {
printf("No results found");
}
?>
[/code]
Clicking on the first result produces output similar to the following:
I hope this tutorial sheds some insight into how you can not only use MySQL's fulltext search capabilities to perform powerful searches against your database, but also introduces some of PHP's interesting text-related functions (nl2br(), htmlentities(), and ucfirst(), to name a few). Of course, one could easily extend what was demonstrated here to implement far more powerful search capabilities, boolean searches for instance. Be sure to check out the MySQL manual for a complete accounting of what's possible!
Using the search form to search for code consisting of the keyword "array" would produce output similar to this:
Results: <br /> Chapter 5: <a href='displaycode.php?id=65'>Retrieving array keys</a> Chapter 5: <a href='displaycode.php?id=54'>Creating an array</a> Chapter 9: <a href='displaycode.php?id=97'>Converting an array to a delimited string</a>
Finally, the displaycode.php script is used to display the script contents. It looks like this:
<?php
mysql_connect("localhost","gilmore","secret");
mysql_select_db("beginningphpandmysqlcom");
$id = mysql_real_escape_string(原文:http://www.developer.com/db/article.php/10920_3671021_2
Using the search form to search for code consisting of the keyword "array" would produce output similar to this:
[code]Results: <br /> Chapter 5: <a href='displaycode.php?id=65'>Retrieving array keys</a> Chapter 5: <a href='displaycode.php?id=54'>Creating an array</a> Chapter 9: <a href='displaycode.php?id=97'>Converting an array to a delimited string</a>
Finally, the displaycode.php script is used to display the script contents. It looks like this:
___FCKpd___1
Clicking on the first result produces output similar to the following:
Chapter 5 - Retrieving array keys <?php $state["Delaware"] = "December 7, 1787"; $state["Pennsylvania"] = "December 12, 1787"; $state["New Jersey"] = "December 18, 1787"; $keys = array_keys($state); print_r($keys); ?>
I hope this tutorial sheds some insight into how you can not only use MySQL's fulltext search capabilities to perform powerful searches against your database, but also introduces some of PHP's interesting text-related functions (nl2br(), htmlentities(), and ucfirst(), to name a few). Of course, one could easily extend what was demonstrated here to implement far more powerful search capabilities, boolean searches for instance. Be sure to check out the MySQL manual for a complete accounting of what's possible!
GET['id']);
$query = "SELECT id, title, chapter, code FROM code WHERE id='$id'";
$result = mysql_query($query);
// If results were found, output them
if (mysql_num_rows($result) > 0) {
$row = mysql_fetch_array($result);
printf("<h4>Chapter %s - %s</h4>", $row['chapter'], ucfirst($row['title']));
// Convert the newline characters and HTML entities before displaying
printf("%s", ^nl2br^(htmlentities($row['code'])));
} else {
printf("No results found");
}
?>
[/code]
Clicking on the first result produces output similar to the following:
___FCKpd___2
I hope this tutorial sheds some insight into how you can not only use MySQL's fulltext search capabilities to perform powerful searches against your database, but also introduces some of PHP's interesting text-related functions (nl2br(), htmlentities(), and ucfirst(), to name a few). Of course, one could easily extend what was demonstrated here to implement far more powerful search capabilities, boolean searches for instance. Be sure to check out the MySQL manual for a complete accounting of what's possible!
相关文章推荐
- Creating a Code Search Engine with PHP and MySQL(1)
- Creating a real-time search engine with IndexTank and Heroku
- Core Web Application Development with PHP and MySQL
- Installing Nginx With PHP5 (And PHP-FPM) And MySQL Support (LEMP) On Ubuntu 12.04 LTS [repost]
- How to install Nginx, PHP, PHP-FPM and MySQL under Windows with Cygwin
- php install with mysql on w2k3 and IIS mannually
- AngularJS CRUD Example with PHP, MySQL and Material Design
- [PHP] Reprinted - Database Sharding at Netlog, with MySQL and PHP
- How to Integrate angular 2 with php and mysql db
- Android Login and Registration with PHP, MySQL and SQLite
- Using Memcache with MySQL and PHP
- Code Search Engine Krugle Announces Partnership With Amazon
- Setting up a PHP 5 with Apache 2 and MySQL 4.1.3
- Fatal Error: ezSQL_mysql requires mySQL Lib to be compiled and or linked in to the PHP engine
- Ubuntu 16.04 LAMP server tutorial with Apache 2.4, PHP 7 and MariaDB (instead of MySQL)
- Google Merchant How To Setup A Live XML Data Feed With MySql and PHP
- Remote Syslog with MySQL and PHP
- How to Do Everything with PHP and MySQL
- Creating Database Web Applications with PHP and ASP
- Search Engine Optimization -Building Traffic and Making Money with SEO