Home php教程 php手册 百宝箱之介绍PHP Web查询数据库基本步骤

百宝箱之介绍PHP Web查询数据库基本步骤

Jun 13, 2016 am 11:05 AM
php web introduce dynamic Basic develop database yes Inquire step programming First choice

PHP是开发WEB动态页面的首选编程,最近看了一本书收获很多,现在和大家一起分享一下PHP Web查询数据库的知识,下面我们就一起来看看吧。从PHP Web查询数据库的基本步骤:

1. 检查并过滤来自用户的数据 首先,我们将过滤用户可能在其搜索条件的起始或结束位置不小心输入的空白字符,这是用函数trim()来实现。 我们这么麻烦的检查用户输入数据的原因是防止多个接口连接数据库,因为用户从不同的界面进入,这样可能导致安全问题。

然后,当准备使用用户输入的任何数据时,也要适当的过滤一些控制字符,当用户输入数据到数据库时必须转义数据,,此时使用盗的函数有 addslashes()函数、stripslashes()函数和get_magic_qutoes_gpc()函数。 addslashes()函数为了数据库查询语句等的需要在某些字符前加上了反斜线;stripslashes()函数去掉字符串中的反斜线字符;get_magic_qutoes_gpc()函数 魔术添加转义字符 “”,获取当前活动配置magic_quotes_runtime设置,如果运行时关闭魔术引号,返回0,否则返回1。我们也可以使用 htmispecialchars()对HTML中的特殊意义字符警醒编码,htmispecialchars()函数把一些预定义的字符转换为 HTML 实体 预定义的字符是:& (和号) 成为 & " (双引号) 成为 " ' (单引号) 成为 ' (大于) 成为 >

2. 建立一个到适当数据库的连接 PHP为连接MySQL提供了函数库mysqli(i表示改进)。

当在PHP中使用mysqli函数库是可以使用面向对象或面向过程的语法:

1)面向对象, @ $db = new mysqli('hostname','username','password','dbname');返回一个对象

2)面向过程: @ $db = mysqli_connect('hostname','username','password','dbname');返回一个资源,这个资源表示数据库的连接,而且 如果使用过程方法,必须将这个资源传递到mysqli的所有其它函数。

这与处理函数非常类似mysqli的大多数函数都有面向对象接口和过程接口,二者的差异则在于过程版本的函数名称以mysqli_开头,同时要求传入 mysqli_connect()函数获得的资源句柄。对于这个规则来说,数据可连接是一个异常,因为它是由mysqli对象的构造函数来创建的。因此尝试连接时需要进行检查,mysqli_connect_errno()函数将在出现连接 错误时返回一个错误号,如果成功,则返回0.

请注意:当连接到数据库是,通常会议错误抑制符@作为第一含代码。这样可以巧妙的处理任何错误,也可以通过异常来处理。另外,MySQK对同时连接数据库的连接数量有一定的限制。MySQLi参数max_connections决定了同时连接的个数,该参数和相关的Apache参数 MaxClients的作用是告诉服务器拒绝新的连接请求,从而保证系统资源不会再系统忙碌时或系统瘫痪时被请求或使用。要设置Apache中的 MaxClients参数可以编辑系统中的httpd.conf文件。要为MySQLi设置max_connections参数可以编辑文件my.conf。

选择使用的数据库: 在MySQL命令行使用 use dbname;命令;在php中可以用$db->select_db(dbname);或mysqli_select_db(db_resource,dbname)。

3. 查询数据库 要执行数据库查询,首先应构造查询语句:$query = "select  from user";然后运行 $result = $db->query($query);或者$result = mysqli_query($db,$query); 面向对象版本将返回一个结果对象;过程版本将返回一个结果资源。无论何种方法都将结果保存在$result变量中工以后使用。如果函数运行失败将返回 false。

4. 获取查询结果 使用不同的函数以不同的方式将查询结果从结果对象或标识符中取出来,结果对象或标识符是访问查询返回行的关键。

通常我们要得到结果集中记录行的行数,并且使用mysqli_fetch_assoc()函数。返回行数:$num_results = $result->num_rows;(行数保存在对象的num_rows成员变量中)或$num_results = mysqli_num_rows($result);然后使用循环遍历每一行,在循环中调用 $row = $result->fectch_assoc();或者 $row = mysqli_fetch_assoc($result);返回该行的信息。如果是对象返回行则每个关键词为一个属性名,每个值为属性中的相应的值;如果以资源返回则返回数组。

还有其他从结果标识符中获取结果的方法,例如:使用$row = $result->fecth_row($result);或者$row = mysqli_fetch_row($result);将结果取回到一个列举数组中;也可以使用$row = $result->fecth_object();或者 $row = mysqli_fecth_object($result);江一行去回到一个对象中。

5. 从数据库断开 先释放结果集:$result->free();或 mysqli_free_result($result);然后关闭数据库连接:$db->close()或者 mysqli_close($db); 严格的说,这并不必须,因为脚本执行完毕的时候他们将被自动关闭。

以上就是PHP Web查询数据库的基本步骤,不知道大家学会了吗?赶快试试吧。


Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1669
14
PHP Tutorial
1273
29
C# Tutorial
1256
24
PHP and Python: Different Paradigms Explained PHP and Python: Different Paradigms Explained Apr 18, 2025 am 12:26 AM

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.

Choosing Between PHP and Python: A Guide Choosing Between PHP and Python: A Guide Apr 18, 2025 am 12:24 AM

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP and Python: A Deep Dive into Their History PHP and Python: A Deep Dive into Their History Apr 18, 2025 am 12:25 AM

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

PHP's Impact: Web Development and Beyond PHP's Impact: Web Development and Beyond Apr 18, 2025 am 12:10 AM

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

PHP vs. Python: Use Cases and Applications PHP vs. Python: Use Cases and Applications Apr 17, 2025 am 12:23 AM

PHP is suitable for web development and content management systems, and Python is suitable for data science, machine learning and automation scripts. 1.PHP performs well in building fast and scalable websites and applications and is commonly used in CMS such as WordPress. 2. Python has performed outstandingly in the fields of data science and machine learning, with rich libraries such as NumPy and TensorFlow.

The Continued Use of PHP: Reasons for Its Endurance The Continued Use of PHP: Reasons for Its Endurance Apr 19, 2025 am 12:23 AM

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

Oracle's Role in the Business World Oracle's Role in the Business World Apr 23, 2025 am 12:01 AM

Oracle is not only a database company, but also a leader in cloud computing and ERP systems. 1. Oracle provides comprehensive solutions from database to cloud services and ERP systems. 2. OracleCloud challenges AWS and Azure, providing IaaS, PaaS and SaaS services. 3. Oracle's ERP systems such as E-BusinessSuite and FusionApplications help enterprises optimize operations.

MySQL: Structured Data and Relational Databases MySQL: Structured Data and Relational Databases Apr 18, 2025 am 12:22 AM

MySQL efficiently manages structured data through table structure and SQL query, and implements inter-table relationships through foreign keys. 1. Define the data format and type when creating a table. 2. Use foreign keys to establish relationships between tables. 3. Improve performance through indexing and query optimization. 4. Regularly backup and monitor databases to ensure data security and performance optimization.

See all articles