Changes

Using PHP with MySQL

No change in size, 20:22, 7 June 2007
no edit summary
There is a TV commercial that shows what would happen to the world if we didn't have plastic. In the commercial objects like telephones, chairs and computers disappear before our eyes. The objective is to make us appreciate what would happen to our infrastructure without plastic. A similar analogy can be drawn to the need for databases on the world wide web. Without databases many of the web sites we rely on would quickly cease to function. It is not an exageration exaggeration to suggest that databases form the heart of the web and the internet as we know it. Without some way to store and retrieve data the usefulness of the internet would be a greatly reduced.
One of the many advantages of PHP is the seemless way in which it integrates with the MySQL database. In this chapter we will take a close look at how to access information stored in a MySQL database from a PHP script and present that data to a user's web browser.
== Creating a MySQL User Account ==
The first task is to create a MySQL user account we can use for the purposes of this chapter. To set up as user account you will need to log into MySql MySQL using an account that has suitable privilages. If you are unsure about which user account to use speak to your system administrator or refer to the [http://www.linuxtopia.org/online_books/database_guides/mysql_5.1_database_reference_guide/index.html MySQL documentation]. In this example we will assume you have the root password:
<pre>
== Inserting Data into a MySQL Database Table ==
As the final step isn in setting our sample database we need to add some data to the table. This is achieved using tyhe INSERT command. We will add three initial rows to the table:
<pre>
== Connecting wiht PHP to a MySQL Server ==
Now that we have set upo up our MySQL databse database and entered some data it is time to look at using PHP to connect to the database so that we can start to query the database and add new data. The first step in our PHP script is to connect to our MySQL database server. This is achieved using the PHP ''mysql_connect()'' function. The ''mysql_connect()'' function creates a connection to the database server and returns a database resource handle. The function takes five optional arguments. The first is the address of the server hosting the database. This defaults to ''localhost:3306''. The second argument is the user name to be used to connect to the database. The third argument is the password associated with the user name.
If a second call is made to ''mysql_connect()'' the default behavior is to return the handle from the first call. Setting the fourth argument overrides this behavior and generate a new handle.
The final argument represents the flags for the PHP client which works in the background to connect to the database. Options are MYSQL_CLIENT_COMPRESS, MYSQL_CLIENT_IGNORE_SPACE and MYSQL_CLIENT_INTERACTIVE.
To disconnect form from the database use the ''msql_close()'' function which takes as a sole argument the datbase database resource handle returned by ''mysql_connect()''.
We can now write a script which will connect us to our sample database (remember to modify the password to match the one you specified when creating the user account):
== Selecting Records from a MySQL Database Using PHP ==
Now tghat that we have connected to our MySQL database we can begin accessing the data in our table. To achieve this we need to first select the database we wish to use by calling the ''mysql_select_db()'' function, passing through the database name as an argument. This will return a database handle. We then need to construct a SQL SELECT statement which we will pass to the PHP ''mysql_query()'' function. This function takes the database handle (returned by ''mysql_select_db()'') and the SQL query statement as arguments.
The ''mysql_query()'' function call places the results in an array which we can access using the ''mysql_fetch_array()'' function.
== Modifying and Deleting MySQL Records using PHP ==
Record can be similarly modified and deleted by constructing appropriate SQL DELETE and UPDATE commands and passing them through to the ''mysql_query()'' function. After the function has been called the ''mysql_affected_rows()'' function can cbe be called to identify the number of rows affected by the change. ''mysql_affected_rows()'' accepts a single argument, the handle returned by the ''mysql_connect()'' function.
== Using PHP to get Information about a MySQL Database ==
== Summary ==
Probably one of the most pwerful powerful features of PHP (next to ease of use) is the ease with which it is possible to access and manipluate MySQL databases from PHP scripts. Without database access many web sites would simply cease to function. Without the built in support for MySQL the task of developing datbase database powered web applications would be formiddable task. As we have demonstrated in this chapter, PHP makes MySQL database access fast and easy. PHP essentially does all the work of communicating with the database server for us. All we need to do is write the SQL commands to pass to PHP and PHP does the rest.