Changes

Jump to: navigation, search

Using PHP with MySQL

29 bytes added, 18:30, 13 May 2009
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 webinternet. Without databases many of the web sites we rely on would quickly cease to function. It is not an 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 seamless 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.
Before beginning it is important to note that MySQL is an advanced Relatation Relational Database Management System (RDCMSRDBMS), and as such, may be more sophisticated than is needed for every data storage need. In the next chapter ([[PHP and SQLite]]) we will look at a more lightweight solution that may meet less demanding needs.
This chapter assumes that you have MySQL installed and running on your server. There are many resources on the internet that can help you achieve this. The first place to start is the [http://www.linuxtopia.org/online_books/database_guides/mysql_5.1_database_reference_guide/index.html MySQL documentation].
== 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 using an account that has suitable privilagesprivileges. 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>
</pre>
Having logged into MySQL we can now create a new user called ''phptest''. In the example below we assign the password ''mypassword''. It is advised you use your own passowrd password in place of this one:
<pre>
== Inserting Data into a MySQL Database Table ==
As the final step in setting our sample database we need to add some data to the table. This is achieved using tyhe the SQL INSERT command. We will add three initial rows to the table:
<pre>
Now that we have set up our MySQL 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 generates 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 from the database use the ''msql_close()'' function which takes as a sole argument the database resource handle returned by ''mysql_connect()''.
We can now write a script which that will connect us to our sample database (remember to modify the password to match the one you specified when creating the user account):
<pre>
== Adding Records to MySQL Database using PHP ==
To add records to a database using PHP we simply need to construct a new query string suing using the SQL INSERT command and execute it using the mysql_query() function:
<pre>
== Using PHP to get Information about a MySQL Database ==
PHP provides a number of usefuil useful functions for obtaining information about a MySQL database. It is possible to obtain a list of fields in a table using the ''mysql_list_fields()'' function. This function accepts three arguments, the database name, the table name and datbase database handle returned by ''mysql_connect()''.
The number of fields in a table can be obtained using the ''mysql_num_fields()'' function. This function takes the resource identifier returned by ''mysql_list_fields()'' as an argument.
Once you have obtained the resource identifier from ''mysql_list_fields()'' you can use ''mysql_field_name()'', ''mysql_field_type()'', and ''''mysql_field_len()'' functions to get information about each field. All of these functions take the handle returned by ''mysql_list_fields()'' as the first argument and offset into the table of the field you wish to inspect. A ''for'' loop can be constructed using the result from ''mysql_num_fields()'' to iterate through all feilds fields as follows:
<pre>
</pre>
When loaded into a browser the above exmaple example will generate the following output:
<tt>
== Summary ==
Probably one of the most powerful features of PHP (next to ease of use) is the ease with which it is possible to access and manipluate manipulate 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 database powered web applications would be formiddable formidable 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 MySQL and PHP does the rest. <google>BUY_PHP_BOTTOM</google>

Navigation menu