PHP 简单的网站搜索功能
文章目录 创建数据库 搜索框 搜索参数处理 部分代码功能 结束语 随着网站内容的日益丰富,以及网络上内容的日益剧增,网站搜索功能已经成为一个网站必不可少的功能。试想,在一个资源繁多的网站,如果没有搜索功能,那用户要在众多网站内容中找到自己所需的
文章目录
- 创建数据库
- 搜索框
- 搜索参数处理
- 部分代码功能
- 结束语
随着网站内容的日益丰富,以及网络上内容的日益剧增,网站搜索功能已经成为一个网站必不可少的功能。试想,在一个资源繁多的网站,如果没有搜索功能,那用户要在众多网站内容中找到自己所需的内容,那是多么麻烦的事情。所以说,站内搜索不仅是一个简简单单的查询工具,他更是关涉到网站访客的用户体验。可以说,搜索已然是必不可缺了。
创建数据库
在开始正式写代码之前,我们需要创建一个数据库。下面的代码可以帮助我们完成。
CREATE TABLE users (fname VARCHAR(30), lname VARCHAR(30), info BLOB);
我们还需要插入一些简单的数据:
INSERT INTO users VALUES ( "Jim", "Jones", "In his spare time Jim enjoys biking, eating pizza, and classical music" ), ( "Peggy", "Smith", "Peggy is a water sports enthusiast who also enjoys making soap and selling cheese" ),( "Maggie", "Martin", "Maggie loves to cook itallian food including spagetti and pizza" ),( "Tex", "Moncom", "Tex is the owner and operator of The Pizza Palace, a local hang out joint" )
搜索框
接下来,我们创建一个搜索框。搜索框的功能主要是为用户搜索提供一个入口,让用户输入要查找的关键词。这个搜索框承担的作用还有向承担数据处理的PHP文件发送文本框或者隐藏的参数。我们在这里提交到自身文件:可以通过PHP_SELF ()函数返回自身路径。
<h2 id="Search">Search</h2>
搜索参数处理
提交了参数,我们自然要进行处理了:通过PHP与SQL语句查询数据库里面的资料。
//如果提交了数据 那就处理 if ($searching =="yes") { echo "<p></p><h2 id="搜索结果">搜索结果</h2>"; //没有搜索关键词 if ($find == ""){ echo "<p>没有输入关键词呢</p>"; exit; } // 否则我们连接到数据库 mysql_connect("mysql.yourhost.com", "user_name", "password") or die(mysql_error()); mysql_select_db("database_name") or die(mysql_error()); // 处理一下关键词 $find = strtoupper($find); $find = strip_tags($find); $find = trim ($find); //现在开始搜索了 $data = mysql_query("SELECT * FROM users WHERE upper($field) LIKE'%$find%'"); //循环输出结果 while($result = mysql_fetch_array( $data )) { echo $result['fname']; echo " "; echo $result['lname']; echo "<br>"; echo $result['info']; echo "<br>"; echo "<br>"; } //我们通过统计结果数量 给出提示 $anymatches=mysql_num_rows($data); if ($anymatches == 0){ echo "对不起,没有发现任何匹配关键词的搜索结果……"; } //提示搜索关键词 echo "<b>Searched For:</b> " .$find; } ?>
部分代码功能
if ($searching =="yes"):确定是否提交了
if ($find == ""):确定关键词是否为空
$find = strtoupper($find):转换关键词为大写(该搜索没有涉及汉字 纯英文)
$find = strip_tags($find):过滤HTML代码
$find = trim ($find):去掉一些特殊符号
$data = mysql_query("SELECT * FROM users WHERE upper($field) LIKE'%$find%'"):查询数据
while($result = mysql_fetch_array( $data )) :循环结果
$anymatches=mysql_num_rows($data):搜索结果数量
结束语
这是一个非常简单的PHP+MYSQL应用实例。大家可以作为入门教程学习,也可以尝试更多的搜索扩展:搜索过滤,搜索自动完成等等……
声明: 本文采用 BY-NC-SA 协议进行授权 | IT路人
转载请注明转自《PHP 简单的网站搜索功能》

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP uses MySQLi and PDO extensions to interact in database operations and server-side logic processing, and processes server-side logic through functions such as session management. 1) Use MySQLi or PDO to connect to the database and execute SQL queries. 2) Handle HTTP requests and user status through session management and other functions. 3) Use transactions to ensure the atomicity of database operations. 4) Prevent SQL injection, use exception handling and closing connections for debugging. 5) Optimize performance through indexing and cache, write highly readable code and perform error handling.
