1、 原文: 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 database.First, were going to discuss PHPs built-in data
2、base 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 called abstraction. In programming interfaces, a
3、bstraction 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 log into a database is reduced to the bare minimu
4、m. 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 databases. For example, the MySQL-specific connect function is: mysql_connect($db_hos
5、t, $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 the type of databases to which to connect. You can connect t
6、o 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 information to the user. 1 The Process The basic steps of perfo
7、rming 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 plain PHP and PEAR functions. 2 Resources When connecti
8、ng 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 required to retrieve results from an activ
9、e database querys result set. Youll be creating and assigning both resources in this chapter. 3 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 examples, but we should talk briefly about
10、 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 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 The username The password Before moving on, make sure you can log into your database using the MySQL command-line client.