Welcome, Guest

Please login or register

TUTORIALS SUBMENU

PHOTOSHOP    FLASH    ILLUSTRATOR    BLENDER    CINEMA 4D    WEB-CODING    [SUBMIT]

Related Links

MySQL Database Integration

pages (2): [1] 2


Now that you’ve learned the basics of PHP, you'll want to start back-ending your website with database connectivity. The most common PHP-connective database server is called mySQL and, thankfully, it is as easy to use as PHP itself! In this tutorial you will learn how to connect to a database, extract data and print it on a page, order data, insert data into a database, edit data and delete data.

Connecting and selecting the database
PHP has its own function to connect to mySQL databases which, ironically enough, is called mysql_connect(). The arguments contained within its brackets are defined as host, user, password. I remember this by the acronym: HUP (because it sounds cool). The host is “localhost” by default. Most web servers have this host added if not defaulted, so we need not worry about this argument too much. The second is the username that has permissions to access the database you are connecting to...  but at the moment we are just connecting to the server, so this username is a username that has permissions to just connect to the mySQL server. Fairly obviously, the password is the password for the username; it is not encrypted in any way.

For those who don’t understand this, here’s an example of a PHP script connecting to a database server:

<?
mysql_connect(“localhost” , “joeblog” , “opensesame”);
?>

Now we need to select the individual database, and for this we use a function called mysql_select_db() whose sole argument is the database name as it appears on the server (this is case sensitive!).

Here’s some example PHP code showing us how to select a database:

<?
mysql_select_db(“joes_database”);
?>

Now that we are successfully connected to a database, we now can start managing our data! The 4 main mySQL commands are SELECT, INSERT, UPDATE, and DELETE. I think it’s self explanatory what these commands do…

Selecting Data
All of the mySQL commands use the PHP function mysql_query() to execute them. Selecting is probably one of the most commonly used mySQL commands, used to retrieve (and often display) data from your chosen database.  To enable us to do this we will have to venture a little bit into mySQL language, but don't worry - its not too hard!

The basic format of mySQL SELECT command is as follows:
SELECT columnname FROM tablename

You can select all of the columns by using a * instead of the column name (this is commonly called a wildcard). You cannot just echo a mySQL command like this, however, because if you do it will output something along the lines of 'Array #1'. We need to play with the function mysql_fetch_array() to actually use the data. Usually this function is used in a while() PHP statement, which cycles through each row in a mySQL table and loads the contents of the selected column.

You can add ORDER BY commands into the end of SELECT commands to make them order the data that mySQL will select. The format is thus:
ORDER BY ColumnName ASC/DESC

Of course you don’t write ASC/DESC, you just choose one - ASC means ascending, DESC means descending. Now, imagine we have a database called joeblog containing a table of data called JoeBlog. Here’s an example of how we cycle through the 4 rows of data that are in the table JoeBlog and column Joe:

<?
Mysql_connect(“localhost”,”joeblog”,”opensesame”) or die(mysql_error());
Mysql_select_db(“joes_database”);
$query = mysql_query(“SELECT * FROM JoeBlog ORDER BY Joe ASC”) or die(“There was an error!”);

while( $row = mysql_fetch_array ( $query ) ) {
echo $row[‘Joe’].’ this is one of the columns.<br>’;
}
echo ‘Finished!’;
?>

In the variable named “row” the name in the square brackets and apostrophes is the name of the column we are printing data from. It is necessary to use all the variables, because it makes the whole process a lot smoother, and tidier - we don’t want a line of code with 3 or 4 functions inside each other!  

And that's it - we’ve just successfully printed 4 lines of data from a database! I didn’t introduce our friend mysql_error() before, but it tells us of the nature of a mySQL bug if one occurs.  Its a nice little debug feature included as standard in PHP.

- Tutorial written by Scrowler

Pages (2): [1] 2 Next>
Automatic Translations: Translate Into French Translate Into German Translate Into Italian Translate Into Spanish Translate Into Portuguese

Last 5 User Comments


There are no comments for this tutorial yet.
You can place a comment by clicking here.
Amazing Font Pack!

Featured Tutorialsmore

Artistic Color Iso...
Artistic Color Iso...
- Adobe Photoshop -
StarBurst Effects
StarBurst Effects
- Adobe Photoshop -
Thick Diagonal Lines
Thick Diagonal Lines
- Adobe Photoshop -
Painting A Wooden ...
Painting A Wooden ...
- Adobe Photoshop -
Membership

Username:
Password:  
Remember Me

Lost Password? || Register

Related Links



Special Options
Printer Friendly Version
Forum Threads

 Deactivate Account
Author: jerinian
Posted: Oct 02nd, 11:16am
Activity: 1 replies, 867 views
 changes....
Author: supertackyman
Posted: Sep 12th, 2:56am
Activity: 2 replies, 1037 views
Back again and with free webhosting :)
Author: ngz
Posted: Aug 14th, 3:50pm
Activity: 0 replies, 1042 views
Cartoon Crab 6 Legs Walk Run created in Blender
Author: patricia3d
Posted: Jun 19th, 12:58pm
Activity: 0 replies, 1919 views
HTML Form Post Array to PHP
Author: Space Cowboy
Posted: May 25th, 2:18pm
Activity: 0 replies, 1816 views
My blog where i create Digi Scrapbook
Author: claudya07
Posted: May 11th, 2:33pm
Activity: 0 replies, 14417 views
Blood Dripping from Letters
Author: patricia3d
Posted: Apr 05th, 3:37am
Activity: 0 replies, 2712 views
A New Designer has joined the ranks
Author: skates1
Posted: Mar 28th, 2:19pm
Activity: 2 replies, 2752 views
The hole in Photoshop
Author: Mars
Posted: Feb 13th, 9:28pm
Activity: 2 replies, 3427 views
Colour Swatch
Author: ebz7350
Posted: Jan 15th, 11:18am
Activity: 0 replies, 2344 views
 BioRUST Forums - Reply to Topic
Author: inonShozy
Posted: Jan 11th, 11:32am
Activity: 8 replies, 2482 views
 Version 2 of my portfolio site.
Author: andrewnleon
Posted: Jan 08th, 6:18am
Activity: 6 replies, 2792 views
Forum Threads

--- Site Resources ---
Total Tutorials:212
Total Downloads:    441
Total Fonts:    4673