This is intended as a very basic guide to get you started on using PHP with MySQL in your HTML code. All of the PHP functions used below are fully documented on the PHP site at http://www.php.net. First, create a database table using the phpMyAdmin database administration tool and create the desired fields in the table for your data. For the example below, we've assumed you have created a table named "students" within your database. The name of the database should be the same as your username. In your HTML page code, you will put the following PHP code examples. All PHP code should be enclosed in <% %> tags within your HTML. You will need to connect to your database so we will use the
following statement: Now that you are connected, you will need to select a database with
the following statement: Retrieving Data From A Database Lets write a simple query: $result = mysql_query(SELECT * FROM students, $link); The result of the query will be stored in the variable $result. As an alternative to the above command, to run a query on the database we can also use the mysql_db_query() function. The first parameter is the database name, second is the query string in SQL format, and the third is the link to the database. mysql_db_query(databaseName, "select * from students", $link) We can use an if/else control structure to display errors during execution, which can be helpful for debugging. if (mysql_db_query(databaseName,
"select * from students", $link)) <html><body> mysql_select_db("YOURUSERNAME",$link); echo "<table border=1>"; while ($Row =
mysql_fetch_row($Result)) Now you have a basic understanding of how to retrieve information from a MySQL database using PHP. Feel free to modify the PHP file and insert it into or delete it from the database. For more information on other options to try, theres plenty of online documentation available at http://www.php.net and other sites on the Web. You can easily see that you can execute any MySQL database query using the mysql_query PHP function. For full documentation on MySQL, please see http://www.mysql.com/documentation/. |