PHP外文翻译文献.docx

上传人:牧羊曲112 文档编号:3062076 上传时间:2023-03-10 格式:DOCX 页数:21 大小:46.98KB
返回 下载 相关 举报
PHP外文翻译文献.docx_第1页
第1页 / 共21页
PHP外文翻译文献.docx_第2页
第2页 / 共21页
PHP外文翻译文献.docx_第3页
第3页 / 共21页
PHP外文翻译文献.docx_第4页
第4页 / 共21页
PHP外文翻译文献.docx_第5页
第5页 / 共21页
亲,该文档总共21页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述

《PHP外文翻译文献.docx》由会员分享,可在线阅读,更多相关《PHP外文翻译文献.docx(21页珍藏版)》请在三一办公上搜索。

1、PHP外文翻译文献PHP外文翻译文献 PHP外文翻译文献 (文档含中英文对照即英文原文和中文翻译) 原文: Getting PHP to Talk to MySQl Now that youre comfortable using the MySQL client tools to manipulate data in the database, you can begin using PHP to display and modify data from the database. PHP has standard functions for working with the databas

2、e.First, were going to discuss PHPs built-in database functions. Well also show you how to use the The PHP Extension and Application Repository (PEAR) database functions that provide the ability to use the same functions to access any supported database. This type of flexibility comes from a process

3、 called abstraction. In programming interfaces, abstraction simplifies a complex interaction. It works by removing any nonessential parts of the interaction, allowing you to concentrate on the important parts. PEARs DB classes are one such database interface abstraction. The information you need to

4、log into a database is reduced to the bare minimum. This standard format allows you to interact with MySQL, as well as other databases using the same functions. 1 PHP外文翻译文献 Similarly, other MySQL-specific functions are replaced with generic ones that know how to talk to many databases. For example,

5、the MySQL-specific connect function is: mysql_connect($db_host, $db_username, $db_password); versus PEARs DB connect function: $connection = DB:connect(mysql:/$db_username:$db_password$db_host/$db_database); The same basic information is present in both commands, but the PEAR function also specifies

6、 the type of databases to which to connect. You can connect to MySQL or other supported databases. Well discuss both connection methods in detail. In this chapter, youll learn how to connect to a MySQL server fromPHP, how to use PHP to access and retrieve stored data, and how to correctly display in

7、formation to the user. The Process The basic steps of performing a query, whether using the mysql command-line tool or PHP, are the same: Connect to the database. Select the database to use. Build a SELECT statement. Perform the query. Display the results. Well walk through each of these steps for b

8、oth plain PHP and PEAR functions. Resources When connecting to a MySQL database, you will use two new resources. The first is the link identifier that holds all of the information necessary to connect to the database for an active connection. The other resource is the results resource. It contains a

9、ll information required to retrieve results from an active database querys result set. Youll be creating and assigning both resources in this chapter. Querying the Database with PHP Functions In this section, we introduce how to connect to a MySQL database with PHP. Its quite simple, and well begin

10、shortly with examples, but we should talk briefly about what actually happens. When you try connecting to a MySQL database, the MySQL server authenticates you based on your username and password. PHP handles connecting to the database for you, and it allows you to start performing queries and gather

11、ing data immediately. As in Chapter 8, well need the same pieces of information to connect to the database: The IP address of the database server The name of the database 2 PHP外文翻译文献 The username The password Before moving on, make sure you can log into your database using the MySQL command-line cli

12、ent. Figure 9-1 shows how the steps of the database interaction relate to the two types of resources. Building the SELECT statement happens before the third function call, but it is not shown. Its done with plain PHP code, not a MySQL-specific PHP function. Figure 9-1. The interaction between functi

13、ons and resources when using the database Including Database Login Details Youre going to create a file to hold the information for logging into MySQL. Storing this information in a file you include is recommended. If you change the database password, there is only one place that you need to change

14、it, regardless of how many PHP files you have that access the database. You dont have to worry about anyone directly viewing the file and getting your database login details. The file, if requested by itself, is processed as a PHP file and returns a blank page. Troubleshooting connection errors One

15、error you may get is: Fatal error: Call to undefined function mysql_connect( ) in C:Program FilesApache Software FoundationApache2.2htdocsdb_test.php on line 4 This error occurs because PHP 5.x for Windows was downloaded, and MySQL support was not included by default. To fix this error, copy the php

16、_mysql.dll file from the ext/ directory of the PHP ZIP file to C:php, and then C:WINDOWSphp.ini. Make sure there are two lines that are not commented out by a semicolon (;) at the beginning of the line like these: extension_dir = c:/PHP/ext/ extension=php_mysql.dll This will change the extension to

17、include the directory to C:/php and include the MySQL extension, respectively. You can use the Search function of your text editor to check whether the lines are already there and just need to be uncommented, or whether they need to be added completely. Youll need to restart Apache, and then MySQL s

18、upport will be enabled. Selecting the Database 3 PHP外文翻译文献 Now that youre connected, the next step is to select which database to use with the mysql_select_db command. It takes two parameters: the database name and, optionally, the database connection. If you dont specify the database connection, th

19、e default is the connection from the last mysql_connect: / Select the database $db_select=mysql_select_db($db_database); if (!$db_select) die (Could not select the database: . mysql_error( ); Again, its good practice to check for an error and display it every time you access the database. Now that y

20、ouve got a good database connection, youre ready to execute your SQL query. Building the SQL SELECT Query Building a SQL query is as easy as setting a variable to the string that is your SQL query. Of course, youll need to use a valid SQL query, or MySQL returns with an error when you execute the qu

21、ery. The variable name $query is used since the name reflects its purpose, but you can choose anything youd like for a variable name. The SQL query in this example is SELECT * FROM books. You can build up your query in parts using the string concatenate (.) operator: Executing the Query To have the

22、database execute the query, use the mysql_query function. It takes two parametersthe query and, optionally, the database linkand returns the result. Save a link to the results in a variable called, you guessed it, $result! This is also a good place to check the return code from mysql_query to make s

23、ure that there were no errors in the query string or the database connection by verifying that $result is not FALSE: When the database executes the query, all of the results forma result set. These results correspond to the rows that you saw upon doing a query using the mysql command-line client. To

24、 display them, you process each row, one at a time. Fetching and Displaying Use mysql_fetch_row to get the rows from the result set. Its syntax is: array mysql_fetch_row ( resource $result); It takes the result you stored in $result fromthe query as a parameter. It returns one row at a time from the

25、 query until there are no more rows, 4 PHP外文翻译文献 and then it returns FALSE. Therefore, you do a loop on the result of mysql_fetch_row and define some code to display each row: The columns of the result row are stored in the array and can be accessed one at a time. The variable $result_row2 accesses

26、the second attribute (as defined in the querys column order or the column order of the table if SELECT * is used) in the result row. Fetch types This is not the only way to fetch the results. Using mysql_fetch_array, PHP can place the results into an array in one step. It takes a result as its first

27、 parameter, and the way to bind the results as an optional second parameter. If MYSQL_ASSOC is specified, the results are indexed in an array based on their column names in the query. If MYSQL_NUM is specified, then the number starting at zero accesses the results. The default value, MYSQL_BOTH, ret

28、urns a result array with both types. The mysql_fetch_ assoc is an alternative to supplying the MYSQL_ASSOC argument. Closing the Connection As a rule of thumb, you always want to close a connection to a database when youredone using it. Closing a database with mysql_close will tell PHP and MySQL tha

29、t you no longer will be using the connection, and will free any resources and memory allocated to it: mysql_close($connection) Installing PEAR uses a Package Manager that oversees which PEAR features you install. Whether you need to install the Package Manager depends on which version of PHP you ins

30、talled. If youre running PHP 4.3.0 or newer, its already installed. If yourerunning PHP 5.0, PEAR has been split out into a separate package. The DB package that youre interested in is optional but installed by default with the Package Manager. So if you have the Package Manager, youre all set. Unix

31、 You can install the Package Manager on a Unix systemby executing the following from the shell (command-line) prompt: lynx -source http:/go-pear.org/ | php This takes the output of the go-pear.org site (which is actually the source PHP code) to install PEAR and passes it along to the php command for

32、 execution. Windows The PHP 5 installation includes the PEAR installation script as C:phpgo-pear.bat. In case you didnt install all the files in Chapter 5 PHP外文翻译文献 2, go ahead and extract all the PHP files to C:/php from the command prompt, and execute the .bat file. Creating a connect instance The

33、 DB.php file defines a class of type DB. Refer to Chapter 5 for more information on working with classes and objects. Well principally be calling the methods in the class. The DB class has a connect method, which well use instead of our old connect function, mysql_connect. The double colons (:) indi

34、cate that were calling that function from the class in line 4: $connection = DB:connect(mysql:/$db_username:$db_password$db_host/$db_database); When you call the connect function, it creates a new database connection that is stored in the variable $connection. The connect function attempts to connec

35、t to the database based on the connect string you passed to it. Connect string The connect string uses this new format to represent the login information that you already supplied in separate fields: dbtype:/username:passwordhost/database This format may look familiar to you, as its very similar to

36、the connect string for a Windows file share. The first part of the string is what really sets the PEAR functions apart fromthe plain PHP. The phptype field specifies the type of database to connect. Supported databases include ibase, msql, mssql, mysql, oci8, odbc, pgsql, and sybase. All thats requi

37、red for your PHP page to work with a different type of database is changing the phptype! The username, password, host, and database should be familiar from the basic PHP connect. Only the type of connection is required. However, youll usually want to specify all fields. After the values from db_logi

38、n.php are included, the connect string looks like the following: mysql:/test:testlocalhost/test If the connect method on line 6 was successful, a DB object is created. It contains the methods to access the database as well as all of the information about the state of that database connection. Queryi

39、ng One of the methods it contains is called query. The query method works just like PHPs query function in that it takes a SQL statement. The difference is that the arrow syntax (-) is used to call it fromthe object. It also returns the results as another object instead of a result set: $query = SEL

40、ECT * FROM books $result = $connection-query($query); 6 PHP外文翻译文献 Based on the SQL query, this code calls the query function fromthe connection object and returns a result object named $result. Fetching Line 22 uses the result object to call the fetchRow method. It returns the rows one at a time, si

41、milar to mysql_fetch_row: while ($result_row = $result-fetchRow( ) echo Title: .$result_row1 . ; echo Author: .$result_row4 . ; echo Pages: .$result_row2 . ; Use another while loop to go through each row from fetchRow until it returns FALSE. The code in the loop hasnt changed from the non-PEAR examp

42、le. Closing Youre finished with the database connection, so close it using the object method disconnect: $connection-disconnect( ); PEAR error reporting The function DB:isError will check to see whether the result thats been returned to you is an error. If it is an error, you can use DB:errorMessage

43、 to return a text description of the error that was generated. You need to pass DB:errorMessage, the return value from your function, as an argument. Here you rewrite the PEAR code to use error checking: query( $sql) echo DB:errorMessage($demoResult); else while ($demoRow = $demoResult-fetchRow( ) e

44、cho $demoRow2 . ; ? Theres also a new version of the PEAR database interface called PEAR:MDB2. The same results display, but there are more functions available in this version of the PEAR database abstraction layer. 7 PHP外文翻译文献 Now that you have a good handle on connecting to the database and the va

45、rious functions of PEAR。 译文: 通过PHP访问MySQL 现在你已经可以熟练地使用MySQL客户端软件来操作数据库里的数据,我们也可以开始学习如何使用PHP来显示和修改数据库里的数据了。PHP有标准的函数用来操作数据库。 我们首先学习PHP内建的数据库函数,然后会学习PHP扩展和应用程序库中的数据库函数,我们可以使用这些函数操作所有支持的数据库。这种灵活性源自于抽象。对于编程接口而言,抽象简化了复杂的交互过程。它将交互过程中无关紧要的部分屏蔽起来,让你关注于重要的部分。PEAR的DB类就是这样一种数据库接口的抽象。你登录一个数据库所需要提供的信息被减少到最少。这种标准

46、的格式可以通过同一个函数来访问MySQL以及其他的数据库。同样,一些MySQL特定的函数被更一般的、可以用在很多数据库上的函数所替代。比如,MySQL特定的连接函数是: mysql_connect($db_host, $db_username, $db_password); 而PEAR的DB提供的连接函数是: $connection = DB:connect(mysql:/$db_username:$db_password$db_host/$db_database); 两个命令都提供了同样的基本信息,但是PEAR的函数中还指定了要连接的数据库的类型。你可以连接到MySQL或者其他支持的数据库。

47、我们会详细讨论这两种连接方式。 本章中,我们会学习如何从PHP连接到MySQL的服务器,如何使用PHP访问数据库中存储的数据,以及如何正确的向用户显示信息。 步骤 无论是通过MySQL命令行工具,还是通过PHP,执行一个查询的基本步骤都是一样的: 8 PHP外文翻译文献 连接到数据库 选择要使用的数据库 创建SELECT语句 执行查询 显示结果 我们将逐一介绍如何用PHP和PEAR的函数完成上面的每一步。 资源 当连接到MySQL数据库的时候,你会使用到两个新的资源。第一个是连接的标识符,它记录了一个活动连接用来连接到数据库所必需的所有信息。另外一个资源是结果资源,它包含了用来从一个有效的数据

48、库查询结果中取出结果所需要的所有信息。本章中我们会创建并使用这两种资源。 使用PHP函数查询数据库 本节我们会介绍如何使用PHP连接MySQL数据库。这非常简单,我们会用一些例子说明。但是之前我们应该稍微了解一下幕后发生的事情。当你试图连接一个MySQL数据库的时候,MySQL服务器会根据你的用户名和密码进行身份认证。PHP为你建立数据库的连接,你可以立即开始查询并得到结果。 我们需要同样的信息来连接数据库: 数据库服务器的IP地址 数据库的名字 用户名 密码 在开始之前,首先使用MySQL的命令行客户端确认你登录到数据库。 图9-1显示了数据库交互过程的各个步骤和两种类型资源之间的关系。创建SELECT语句发生在第三个函数调用之前,但是在图中没有显示出来。它是通过普通的PHP

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 生活休闲 > 在线阅读


备案号:宁ICP备20000045号-2

经营许可证:宁B2-20210002

宁公网安备 64010402000987号