Speedsoft Home Page
 
Order Hosting Services Services and Pricing Support Home Page Search Speedsoft Contact Information Site Map
: MySQL Database / PHP Example
See Also
PHP
Consulting

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:
$link = mysql_connect (“localhost”, “YOURUSERNAME”, "YOURPASSWORD");

Now that you are connected, you will need to select a database with the following statement:
mysql_select_db (“YOURUSERNAME”, $link);

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))
{
printThe query was successfully executed!<BR>”);
}

else
{
printThe query could not be executed!<BR>”);
}

Below is the content of the full example.

<html><body>

<%

$link = mysql_connect (“localhost”, “YOURUSERNAME”, "YOURPASSWORD");

mysql_select_db("YOURUSERNAME",$link);

$Result = mysql_query("SELECT * FROM students",$link);

echo "<table border=1>";
echo "<tr><td><b>Name</b></td><td><b>Email</b></tr>";

while ($Row = mysql_fetch_row($Result))
{
printf ("<tr><td>%s %s</td><td>%s</td></tr>",
$Row[1], $Row[2], $Row[3]);
}

echo "</table>";

%>
</body></html>

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, there’s 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/.

Home | Order | Services | Support | CGI Central | E-Commerce | Search | Contact Us | Site Map | Links
Copyright © 1995-2008 Speedsoft, LLC. All rights reserved.