Web Programming  Home Web Programming PHP PHP Navigation and Connecting to a MySQL Database
rss

PHP Navigation and Connecting to a MySQL Database

Author: Akpro.net More by this author


PHP Navigation and Connecting to a MySQL DatabasePHP Navigation

Code

<?php
switch($page) {
default: include('main.php');
break; case "tutorials":
include('tutorials.php');
break; case "resources":
include('resources.php');
break; case "contact":
include('contact.php');
break; case "SOMETHING":
include('something.php');
break; case "SOMETHING":
include('something.php');
}
?>

Explained

Now Lets Explain. Set the Default page to any page you want. So lets start, Case "Tutorials", means if ?page= Tutorials than show tutorials.php. We can change that to anything. So we can make it,

break; case "thetutorialsection":
include('tutorials.php');

That means if the section is ?page=thetutorialpage, than it will show Tutorials.php.

You can add as many pages your site has. If you wish to change the ? page =, simply go to the top of the code and change switch($page).

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!

 



Author's URL: www.akpro.net

Rate this Material: Bad 1 2 3 4 5 Excellent
print this page tell a friend subscribe to newsletter subscribe to rss

Read/Add comments to "PHP Navigation and Connecting to a MySQL Database"

comments  alfa66 May 25, 2006 says:
PHP Navigation and Connecting to a MySQL Database
Poor explanations about system navigation and DB conn. both