您的位置:首页 > 编程语言 > PHP开发

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:

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!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: