《外文文献-基于php的网上购物系统》由会员分享,可在线阅读,更多相关《外文文献-基于php的网上购物系统(20页珍藏版)》请在金锄头文库上搜索。
1、外文文献原文及译文Getting PHP to Talk to MySQl通过PHP访问MySQL电子与计算机科学技术系网络工程系 别: 专 业: 2015年5月14日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 function
2、s for working with the database.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 fl
3、exibility comes from a process 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
4、. The information you need to 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. Similarly, other MySQL-specific functions are replaced with generic ones that know how to talk to many databa
5、ses. For example, 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 funct
6、ion also specifies 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 co
7、rrectly display 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 all information re
8、quired 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 shortly with exam
9、ples, 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 gathering data immediat
10、ely. 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 The username The password Before moving on, make sure you can log into your database using the MySQL command-line client. Figure 9-1 shows how the
11、 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 functions and resources when using
12、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 it, regardless of how many PH
13、P 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 error you may get is: informa
14、tion 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 both p
15、lain PHP and PEAR functions. 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_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 th