PHP Navigation and Connecting to a MySQL Database

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!

 


Related Articles

  • PHP Live Counter: Online Users
    This is a really great script to use for anyone who is wanting to gain more statistics regarding the number of people who visit your site and so forth. Take a look.
  • Alternating Backgrounds
    Alternate the backgrounds of table rows displayed from a MySQL database.
  • Alternating Rows: Mysql
    The first thing we need to do is connect to the database. I always put my db connection in a universal file, that way I can just change the variables once if I need to; but for the sake of the tutorial, im putting it in this file.
  • PHP/MySQL Tutorial System
    This Tutorial will run you through the basics of building your own Tutorial CMS!
  • Users Online
    First Run this SQL In your MYSQL database..
  • PHP User Stats
    In this tutorial I am going to teach you how to make a User Stats menu in PHP and MySQL which displays: Unique hits, Unique hits today, Total hits, Total hits today. If you dont have MySQL you can get one for free here.
  • Reading a Database
    Displaying data from a database is one of the most common uses of ASP.
    Heres some sample code:...
  • Hit Counter
    Hit counters are used to track the amount of visitors there have been to a cetain page. I have looked through the internet and I have come up with 2 different ways to produce a hit counter.
  • Alternating Row Colors
    Learn how to alternate row colors from database output using ASP.
  • Alternating Row Colors
    Theres two different ways this can be done. The first is by getting results from a mysql query, the second is going through the values of an array. Ill go over both. First the full files (ill break it up into two files, one using mysql, and one using an array)

Contact Web Design Outsource and get started today

Need Website Designing, Development, Redesigning, Maintenance and SEO services or help growing your company's web presence? Request a free Quote Now.