Get Adobe Flash player
  • 509This post:
  • 12Visitors today:
  • 15Visitors yesterday:
  • 14Visitors per day:
  • 0Visitors currently online:
  • May 19, 2010Counter starts on:

How to Create Dynamic Web Pages Using PHP & MySQL

You can use the programming language PHP and the database management system MySQL to build dynamic Web content that changes to suit the needs of the user. This tutorial shows how to create a simple page that displays how many times a user has previously visited the page. 

Instructions

    Set Up the MySQL Database

  • Create a database on your MySQL server if you don’t already have one that you are planning to use. The following MySQL command can be used to create a database named “examples”:
    CREATE DATABASE `examples`

     

  • Create a table on the database to store the number of times a user has visited. Use a field named “id” as the primary key to identify individual users. Use a field named “visits” to keep track of how many times the user has visited the page. The following command creates a table named “dynamicphpmysql.”  

    CREATE TABLE `examples`.`dynamicphpmysql` (
    `id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
    `visits` INT NOT NULL
    ) ENGINE = MYISAM

     

  • Create, if you haven’t already done so, a MySQL user on the server that has the necessary privileges in the database. For this example, you will need as least SELECT, UPDATE and INSERT privileges.

Build the PHP Code

  • Create a PHP file. Name it anything you like as long as it ends with “.php.”. For example, you could name the file “dynamic.php.” Open it with your text editor.
  • Call the session_start() function. You will be using a session to keep track of users over multiple visits, so call this function before sending any other data to the user’s Web browser. Remember to enclose all PHP within the tags ”
    <?php session_start(); ?>
  • Create a function that opens a connection to your MySQL server and selects the proper database to manipulate (in this case “examples”).
    In this example, the server’s address is “localhost,” and the username and password to access the server are “username” and “password,” respectively.
    The function returns a link to the MySQL connection.

    function dbConnect()
    
    {
    
    $dbc = mysql_connect("localhost","username","password");
    
    mysql_select_db("examples");
    
    return $dbc;
    
    }
  • Create a function that returns the highest value for “id” that currently exists in the table.
    function getMaxId(){
    $dbc = dbConnect();
    $query = mysql_query("SELECT MAX(id) FROM `dynamicphpmysql`");
    $results = mysql_fetch_array($query);
    $new_id = $results['MAX(id)'];
    mysql_close($dbc);
    return $new_id;}
  • Create a function that adds a row to the table, thus creating a new user within the database.
    You also want this function to return the ID of the new user.
  • function newId()
    {
    $dbc = dbConnect();
    mysql_query("INSERT INTO dynamicphpmysql(visits) VALUES(0)");
    mysql_close($dbc);
    $new_id = getMaxId();
    return $newid;
    }
  • Create a function to increment the number of visits for the currently visiting user.
  • function newVisit($id)
    {
    $dbc = dbConnect();
    mysql_query("UPDATE dynamicphpmysql SET `visits` = `visits` + 1 WHERE id = $id");
    $result = mysql_query("SELECT `visits` FROM dynamicphpmysql WHERE `id` = $id");
    $new_count = mysql_result($result,0);
    mysql_close($dbc);
    return $new_count;
    }
  • Write an “if” structure that checks whether the user has already visited the page before.
    If the user has not visited the page, create an ID for the new user and display to the page that this is the user’s first visit.
    If the user has visited the page, tell the user how many times he has already visited the page.
  • if(!isset($_SESSION['id']))
    {
    newid();
    $_SESSION['id'] = getMaxId();
    echo "This is your first visit to this page.";
    }else{
    $num_visits = newVisit($_SESSION['id']);
    echo "You have visited this page $num_visits times before.";
    }
    }
  • Check your code for errors. Upload the PHP file to your server and navigate to it in your browser.
    Each time you refresh the page, it should tell you how many times you have previously visited the Web page.
    If something isn’t right, double-check your code against the complete code sample:

    <?
    session_start();
    function dbConnect()
    {
    $dbc = mysql_connect("localhost","username","password");
    mysql_select_db("examples");
    return $dbc;
    }
    function getMaxId()
    {
    $dbc = dbConnect();
    $query = mysql_query("SELECT MAX(id) FROM `dynamicphpmysql`");
    $results = mysql_fetch_array($query);
    $new_id = $results['MAX(id)'];
    mysql_close($dbc);
    return $new_id;
    }
    function newId()
    {
    $dbc = dbConnect();
    mysql_query("INSERT INTO dynamicphpmysql(visits) VALUES(0)");
    mysql_close($dbc);
    $new_id = getMaxId();
    return $newid;
    }
    function newVisit($id)
    {
    $dbc = dbConnect();
    mysql_query("UPDATE dynamicphpmysql SET `visits` = `visits` + 1 WHERE id = $id");
    $result = mysql_query("SELECT `visits` FROM dynamicphpmysql WHERE `id` = $id");
    $new_count = mysql_result($result,0);
    mysql_close($dbc);
    return $new_count;
    }
    if(!isset($_SESSION['id']))
    {
    newid();
    $_SESSION['id'] = getMaxId();
    echo "This is your first visit to this page.";
    }else{
    $num_visits = newVisit($_SESSION['id']);
    echo "You have visited this page $num_visits times before.";
    }
    ?>

Leave a Reply