WDDB - Web Design Database

Web Design Resources for the Common Man

Login here · Register · Lost your password?

In: Resources / Tutorials / PHP

Uses of PHP/Mysql Databases

how to connect to a database, and use the information stored.

Print
Author: Dyllon
Submitted by: Dyllon   Date: 2008-03-08 21:54
Comments: (0)   Ratings:
This tutorial will show you how to connect to a MySQL database and use the information stored and display/edit/delete it on your website.

Connecting the to database
Code:
mysql_connect("your.hostaddress.com", "username", "password") or die(mysql_error());
mysql_select_db("Database_Name") or die(mysql_error());
First add this code to your website and replace the information in the "s.

Inserting information into your database
Code:
mysql_query("INSERT INTO example 
(name, age) VALUES('Name', 'Age' ) ") 
or die(mysql_error());
Using this code will insert the the following fields into the example table in your database: Name/Age. This is used most of the time when you need to store information such as news, pages, forum posts, and most other things around on your website.

Displaying information from your database

Well this part is pretty simple. Most of the work is done by just a simple MySQL query. Use the following code to select which information you would like to get from your database:
Code:
$result = mysql_query("SELECT * FROM example")
or die(mysql_error());  
$row = mysql_fetch_array( $result );
Now that you have contacted the database and are requesting the information from the example database you can now display in on your website. Use the following code to display this information (note: along with this entire tutorial, names of your tables may vary and you will have to edit them.)
Code:
echo "Name: ".$row['name'];
echo " Age: ".$row['age'];
Removing information from your database

If you need something to be removed after a certain period of time, the use of the delete command comes in handy. Most of the times this is used for when things are to be expired, or just when a webmaster needs to clear up there database. Use the following code to delete information from your database.
Code:
mysql_query("DELETE FROM example WHERE age='15'") 
or die(mysql_error());
That code will delete any columns in the table 'example' in which the age is entered as 15. You can edit those to match any table/column in your database.

Updated information in your database

If you accidentally entered some information wrong, or you just need to edit something this is the code you will need to use. Mostly used for news, pages, forums and such so users/administrators can edit their work. Use the following code to change information in your database.
Code:
$result = mysql_query("UPDATE example SET age='22' WHERE age='21'") 
or die(mysql_error());
This code will update the example table in your database, and will affect the row AGE. It will set anyone in the table with the age of 21, and update them to 22.

Closing Statements

The use of PHP and MySQL are a must to creating a new, and fresh website. There is a lot more to learn, so make sure you check out some of the future tutorials to come.