AK Productions Studios  
Home Forum Downloads Submit
Tutorials
Welcome Guest!
Using PHP learn how to code the most common piece of code in PHP. This tutorial also explains some very basic codes for beginners!
Author: fastrack
Difficulty: 1/10
 
Connecting to a MySQL Database
   

Code

<?php
$host = 'host';
$user = 'username';
$pass = 'password';
$db = 'database';

$conn = mysql_connect($host, $user, $pass) or die("Could not connect to database: " . mysql_error());
$dbselect = mysql_select_db($db, $conn) or die("Could not select database '" . $db . "': " . mysql_error());
?>

Explained


Basically <?php is telling the server that the it is a PHP script and that this is the start of the script and you probably guessed what ?> means, this means it is the end of the PHP script.
Next $host and $user, these are connection details to the mysql server, you can find out your details by asking your web host provider. $host is normally localhost.

$conn is a function in this case its defined as connect to the mysql database or die("Could not connect to database: " . mysql_error()); This means that if the PHP file cannot connect using the details you provided it will display a message saying Could not connect to database.

$dbselect = mysql_select_db
The last part, this is selecting the database which you provided, the number of mysql databases will depend on your host, you may have an unlimited amount of databases so it selects a database you want it to connect to.

To make this work all you need to do is change the variables which i have colored in yellow then you insert that into your html code (i.e. dreamweaver or just open note pad) and make sure you save it as a .php file otherwise this will not work when you upload it to your web server. This concludes our tutorial!

Comment
Tell A Friend
 
       
       
Home | Tutorials | Forum | Submit | Articles | Downloads | Affiliates | Topsites | Contact | Site Map
Copyright © 2004 - 2006 AK Productions Studios. All Rights Reserved. hit tracker