结合使用PHP与MYSQL 使用mysqli拓展外文翻译资料

 2022-10-22 04:10

Using PHP with MySQL

Using the mysqli Extension

PHPrsquo;s mysqli extension offers all of the functionality provided by its predecessor, in addition to new features that have been added as a result of MySQLrsquo;s evolution into a full-featured database server. This section introduces the entire range of features, showing you how to use the mysqli extension to connect to the database server, query for and retrieve data, and perform a variety of other important tasks.

Setting Up and Tearing Down the Connection

Interaction with the MySQL database is bookended by connection setup and teardown, consisting of connecting to the server and selecting a database, and closing the connection, respectively. As is the case with almost every feature available to mysqli, you can do this by using either an object-oriented approach or a procedural approach, although throughout this chapter only the object-oriented approach is covered.
If you choose to interact with the MySQL server using the object-oriented interface, you need to first instantiate the mysqli class via its constructor:

mysqli([string host [, string username [, string pswd
[, string dbname [, int port, [string socket]]]]]])

Those of you who have used PHP and MySQL in years past will notice this constructor accepts many of the same parameters as does the traditional mysql_connect() function.
Instantiating the class is accomplished through standard object-oriented practice:
$mysqli = new mysqli('localhost', 'catalog_user', 'secret', 'catalog_prod');
When instantiated with the parameters, you can start interacting with the database. If at one point you need to connect to another database server, or select another database, you can use the connect() and select_db() methods. The connect() method accepts the same parameters as the constructor, so letrsquo;s just jump right to an example:
// Instantiate the mysqli class
$mysqli = new mysqli();
// Connect to the database server and select a database
$mysqli-gt;connect('127.0.0.1', 'catalog_user', 'secret', 'catalog_prod');

You can choose a database using the $mysqli-gt;select_db method. The following
example connects to a MySQL database server and then selects the catalog_prod
database:
// Connect to the database server
$mysqli = new mysqli('localhost', 'catalog_user', 'secret');
// Select the database
$mysqli-gt;select_db('catalog_prod');

Once a database has been successfully selected, you can then execute database queries against it. Executing queries, such as selecting, inserting, updating, and deleting information with the mysqli extension, is covered in later sections.
Once a script finishes execution, any open database connections are automatically closed and the resources are recuperated. However, itrsquo;s possible that a page requires several database connections throughout the course of execution, each of which should be closed as appropriate. Even in the case where a single connection is used, itrsquo;s nonetheless good practice to close it at the conclusion of the script. In any case, close() is responsible for closing the connection. An example follows:

$mysqli = new mysqli();
$mysqli-gt;connect('127.0.0.1', 'catalog_user', 'secret', 'catalog_prod');
// Interact with the databasehellip;
// close the connection
$mysqli-gt;close()

Handling Connection Errors

Of course, if yoursquo;re unable to connect to the MySQL database, then little else on the page is likely to happen as planned. Therefore, you should be careful to monitor connection errors and react accordingly. The mysqli extension includes a few features that can be used to capture error messages, or alternatively you can use
exceptions (as introduced in Chapter 8). For example, you can use the mysqli_connect_errno() and mysqli_connect_error() methods to diagnose and display information about a MySQL connection error.

Retrieving Error Information

Developers always strive toward that nirvana known as bug-free code. In all but the most trivial of projects, however, such yearnings are almost always left unsatisfied. Therefore, properly detecting errors and returning useful information to the user is a vital component of efficient software development. This section introduces two functions that are useful for deciphering and communicating MySQL errors.

Retrieving Error Codes

Error numbers are often used in lieu of a natural-language message to ease software internationalization efforts and allow for customization of error messages. The errno() method returns the error code generated from the execution of the last MySQL function or 0 if no error occurred. Its prototype follows:
class mysqli {
int errno
}
An example follows:

lt;?php
$mysqli = new mysqli('localhost', 'catalog_user', 'secret', 'catalog_prod');
printf('Mysql error number generated: %d', $mysqli-gt;errno);
?gt;

This returns:
Mysql error number generated: 1045

Retrieving Error Messages

The error() method returns the most recently generated error message, or returns an empty string if no error occurred. Its prototype follows:
class mysqli {
string error
}

The message language is dependent upon the MySQL database server, because the target language is passed in as a flag at server startup. A sampling of the English-language messages follows:
Sort aborted
Too many connections
Couldnt uncompress communication packet

An example follows:
lt;?php
// Connect to the database server
$mysqli = new mysqli('localhost', 'catalog_user',

剩余内容已隐藏,支付完成后下载完整资料


结合使用PHP与MYSQL

使用mysqli拓展

PHP的mysqli扩展提供了其先行版本提供的所有功能,此外,由于MySQL已经是一个具有完整特性的数据库服务器,这为PHP又添加了一些新特性。本部分介绍了全部的特性,讲解了如何使用mysqli扩展来连接数据库服务器,査询和获取数据,以及执行其他的重要任务。

建立和断开连接

与MySQL数据库交互时,首先要建立连接,最后要断开连接,这包括与服务器连接并选择一个数据库,以及最后关闭连接。与mysqli几乎所有的特性一样,这一点可以使用一种面向对象的方法来完成,也可以采用一种过程化方法完成,不过本章只讨论面向对象方法。

如果选择使用面向对象接口与MySQL服务器交互, 首先需要通过其构造函数实例化mysqli类:

mysqli([string host [, string username [, string pswd
[, string dbname [, int port, [string socket]]]]]])

如果你使用PHP和MysQL 已经有很多年,就会注意到这个构造函数接收的参数与传统的mysql _ connect( )函数所接收的参数完全相同。
要实例化这个类, 可以通过标准的面向对象方式完成:
$mysqli = new mysqli('localhost', 'catalog_user', 'secret', 'catalog_prod');
如果用这些参数完成了类的实例化,就可以开始与数据库交互了。如果在某个时刻需要连接到另一个数据库服务器,或者需要选择另一个数据库,可以使用connect()和select db()方法。connect() 方法接收的参数与构造函数相同,所以我们来直接看一个例子:
// Instantiate the mysqli class
$mysqli = new mysqli();
// Connect to the database server and select a database
$mysqli-gt;connect('127.0.0.1', 'catalog_user', 'secret', 'catalog_prod');

可以使用$mlysqli -gt;select_db方法来选择数据库。下面的例子将连接到一个MySQL数据库服务器, 然后选择catalog_prod数据库:
// Connect to the database server
$mysqli = new mysqli('localhost', 'catalog_user', 'secret');
// Select the database
$mysqli-gt;select_db('catalog_prod');

一旦成功地选择了数据库,然后就可以对这个数据库执行数据库査询了。后面的小节将介绍使用rnysqli扩展包执行査询 (如选择、插入和删除) 的有关信息。

一旦脚本执行结束,所有打开的数据库连接都会自动关闭,并释放资源。不过,有可能一个页面在执行期间需要多个数据库连接,各个连接应当适当地加以关闭。即使只使用了一个连接,也应该在脚本的最后将其关闭,这是一种很好的实践方法。在任何情况下,都由close( )负责关闭连接。下面给出一个例子:

$mysqli = new mysqli();
$mysqli-gt;connect('127.0.0.1', 'catalog_user', 'secret', 'catalog_prod');
// Interact with the databasehellip;
// close the connection
$mysqli-gt;close()

处理连接错误

当然,如果无法连接MysQL数据库,那么这个页面不太可能继续完成预期的工作,因此,一定要注意监视连接错误并相应地做出反应。Mysqli扩展包包含很多特性可以用来捕获错误消息,也可以使用异常来做到这一点。例如,可以使用 mysqli_connect_errno()和mysqli_ connect_error( )方法诊断并显示一个MySQL连接错误的有关信息。

获得错误信息

开发人员总是希望能达到无bug代码的“理想境界”。不过,即使是最简单不过的项目,这种愿望也往往无法满足。因此,要适当地检测错误并向用户返回有用的信息,这对于高效的软件开发至关重要。这节将介绍可以用来解释和表示MySQL错误的两个函数。

获取错误码

错误码通常与自然语言消息结合使用,以简化软件的国际化工作,且允许完成错误消息的定制。errno()方法返回上一个MySQL函数执行期间生成的错误码,如果没有出现错误,则返回0。其形式如下:
class mysqli {
int errno
}
An example follows:

lt;?php
$mysqli = new mysqli('localhost', 'catalog_user', 'secret', 'catalog_prod');
printf('Mysql error number generated: %d', $mysqli-gt;errno);
?gt;

这会返回以下输出:
Mysql error number generated: 1045

获取错误消息

error()方法返回最近生成的错误消息,如果没有出现任何错误,则返回一个空字符串。其形式如下:
class mysqli {
string error
}

消息语言取决于MySQL数据库服务器,因为目标语言会作为一个标志在服务器启动传入。以下是一个英语的错误消息:
Sort aborted
Too many connections
Couldnt uncompress communication packet

下面给出一个例子:
lt;?php
// Connect to the database server
$mysqli = new mysqli('localhost', 'catalog_user', 'secret', 'catalog_prod');
if ($mysqli-gt;errno) {
printf('Unable to connect to the database:lt;br /gt; %s',
$mysqli-gt;error);
exit();
}
?gt;

例如,如果提供了不正确的口令,会看到以下消息:

Unable to connect to the database:
Access denied for user catalog_user@localhost (using password: YES)

当然,如果直接将 MySQL预置的错误消息显示给终端用户可能不太好看,所以可以考虑将这个错误消息发送到你的 E-mail地址,并且在这种情况下向用户显示一个更友好的消息.

在单独的文件中存储连接信息

本着安全编程实践的精神,通常最好经常修改密码。但是,需要访问指定数据库的每个脚本中都必须建立与MySQL服务器的连接,所以像mysql_connect( )和mysql_pconnect( )之类的函数可能会反复出现在大量文件中,这样一来就很难修改。对于这个难题,无疑有一种简単的解决方案一将这些信息存储在单独的文件中,然后在必要时将该文件包含到脚本中。例如,mysqli构造函数可能存储在名为mlysql.connect.php的头文件中, 如下:

lt;?php
// Connect to the database server
$mysqli = new mysqli('localhost', 'catalog_user', 'secret', 'catalog_prod');
?gt;

然后在必要时包含此文件如下:

lt;?php
include 'mysql.connect.php';
// begin database selection and queries.
?gt;

保护你的连接信息

如果刚开始在PHP中使用数据库,可能没有注意像MySQL连接参数之类重要的信息 (包括密码)都是以明文形式存储在文件中。尽管如此,还是可以采取一些步骤来确保不受欢迎的客户无法获取这些重要数据。

bull; 使用基于系统的用户权限来确保只有拥有Web服务器守护进程的用户能够读取此文件。在基于UNIX的系统中,这意味着将文件拥有者改为运行web进程的用户,并将连接文件的权限设为 -r--------。
bull; 如果连接远程MySQL服务器,记住此信息将以明文形式传送,除非采取必要的措施在传输中加密数据。最好使用安全套接字层(ssl)加密。
bull; 有一些脚本编码产品,,利用这些产品,,除了拥有必要解码权限的用户外,代码对其他用户都不可读,但不影响代码的可执行性。Zend Guard(http://www.zend.com/)和ionCube PHP Encoder(http//www.ioncube.com/) 可能是这方面最著名的解决方案,不过也有其他一些产品。记住,除非有其他特别的原因要对源代码编码,否则就应当考虑其他保护措施,例如操作系统目录安全性,因为它们在大多数情况下都非常有效。

.

与数据库交互

绝大多数査询都与创建(Creation)、获取(Retrieva1)、更新(Update)和删除(Deletion)任务有关,这些任务统称为CRUD。这一节将介绍如何建立并向数据库发送这些査询来具体执行。

.

向数据库发送查询

方法query( )负责将query发送给数据库。其形式如下:

class mysqli {
mixed query(string query [, int resultmode])
}

可选参数resultmode可以用于修改这个方法的行为,它接受两个可取值:
bull; MYSQLI STORE RESULT:将结果作为一个缓存集返回,这说明可以立即对整个结果集导航。这是默认设置,尽管这个选项有一定代价,会增加内存需求,不过这种情况下,你就能一次处理整个结果集,倘若你试图分析或管理这个结果集,这很有用。例如,你可能想确定一个特定査询返回多少行,或者希望立即跳到结果集中的某个特定行。
bull; MYSQLI_USE_RESULT: 将结果作为一个非缓存集返回,这说明会根据需要从服务器获取结果集。对于较大的结果集,非缓存结果集能提高性能,不过对结果集的许多操作都要受限制,如无法立即确定査询得到的行数,也无法直接跳到某个特定的行。如果要获取相当多的行,就可以考虑使用这个选项,因为这样需要的内存较少,而且响应时间更快。

获取数据

你的应用程序的大多数工作可能都是在获取和格式化所请求的数据。为此,要向数据库发送SELECT査询,再对结果进行迭代处理,将各行输出给浏览器,并以你喜欢的某种方式格式化。

以下示例从products表获取sku、name和price列,并按name对结果排序。结果中的各行再分别放在3个适当命名的变量中,然后输出到浏览器。

lt;?php
$mysqli = new mysqli('127.0.0.1', 'catalog_user', 'secret', 'catalog_prod');
// Create the query
$query = 'SELECT sku, name, price FROM products ORDER by name';

// Send the query to MySQL
$result = $mysqli-gt;query($query, MYSQLI_STORE_RESULT);
// Iterate through the result set
while(list($sku, $name, $price) = $result-gt;fetch_row())
printf('(%s) %s: $%s lt;br /gt;'

剩余内容已隐藏,支付完成后下载完整资料


资料编号:[152099],资料为PDF文档或Word文档,PDF文档可免费转换为Word

原文和译文剩余内容已隐藏,您需要先支付 30元 才能查看原文和译文全部内容!立即支付

以上是毕业论文外文翻译,课题毕业论文、任务书、文献综述、开题报告、程序设计、图纸设计等资料可联系客服协助查找。