PHP外文翻译文献.docx
《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
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- PHP 外文 翻译 文献
链接地址:https://www.31ppt.com/p-3062076.html