MySQL Access

The MySQL® database has become the world's most popular open source database because of its consistent fast performance, high reliability and ease of use. It's used in more than 6 million installations ranging from large corporations to specialized embedded applications on every continent in the world.

PHP provides support for MySQL through an array of functions that can be used to manipulate MySQL data. The purpose of this tutorial is to introduce those functions commonly used in PHP data-driven applications for retrieving, updating, inserting, and deleting data.

The common PHP MySQL functions are described below:

mysql_connect(MySQL server name,username,password) - opens a connection to a MySQL server.

mysql_select_db(database name,connection id) - selects a database residing on the MySQL server. The database name parameter referes to an active database on the MySQL server that was opened with the mysql_connect function. The connection identifier is a reference to the current MySQL connection.

mysql_query(sql query) - sends a query to the currently active database.

mysql_fetch_array(recordset id) - Returns an array that corresponds to the fetched row and moves the internal data pointer ahead.

mysql_num_rows(recordset id) - determines the number of rows contained in a recordset returned by the previous SQL SELECT operation. The function returns a value of FALSE if the operation fails.

mysql_affected_rows(connection id) - determines the number of rows affected by the previous SQL INSERT, DELETE, or UPDATE operation. The function returns a value of -1 if the operation fails.

mysql_close(connection id) - closes MySQL connection.

These functions will be demonstrates in sections 10-2 through 10-5.

Before you can use these functions to create data-driven applications using MySQL, you will need appropriate access to a MySQL server. This includes a user account and password with permissions to the database and tables containing your web data, and the MySQL server's host name or IP address.

When working with MySQL server, it is also helpful to use a GUI management tool, which provides an easy to use interface to the data. Popular tools include: SQLyog (available at http://www.webyog.com) and the MySQL Administrator (available at http://www.mysql.com/products/tools/).