PHP files: Creating the table

I will not go into the basics of MySQL. However in the files to download there is a php script, create_counter_table.php, which creates the table we need for our site counter. You need to first create a database with username and password on your host. Then we add a file, which holds all the refernce variables to the database. I call it 'restric_data.inc'.

<?php

/*****************************************

restric_data.inc

******************************************/

/*
/ This file contains all the variables for the MySQL database and also the password
/ to enter data is created here and encrypted. Create different user and passwords 
/ for getting the data and adding data to the database.
*/

$dbHost = "yourhost";// this is often the localhost
$dbName = "database name";// name of the database
$tableName = "table name";// name of the table

// username and password

$dbUser = "username";
$dbPass = "password";
$cookName = "cookiename";// Name of the cookie we set with Javascript

?>
				

Now we are ready to write the php file for the movie. Again I will skip explaining MySQL commands and only explain some of the php. It is important that you are a little bit familiar with the basics of php. In the first part of the script we connect to the database. The variable $uniqueCounts is coming from the Flash movie butonly when we want unique visitors.

<?php

/****************************************

* receiveData.php
* copyright 2009: flashscript.biz

****************************************/

// This file contains all the names and references to the database.

include("restric_data.inc");

/*************** NO CHANGES REQUIRED FROM HERE ON *************************/

// This variable is coming from the Flash movie

$uniqueCounts=$_POST['unique'];

// Connecting to the database.

$link = mysql_connect($dbHost, $dbUser, $dbPass);

// If there is no connection we send an error and exit.

if (!$link)
{
    fail ("ERROR: Could not connect to database");
    exit();
}

// If there was no selection of db we break up.

if (!mysql_select_db($dbName))
{
    fail("ERROR: Could not select dbName database.");
    exit();
}

Once we have done that we need to get data from the database. We use the 'SELECT' comand to access the cells from the table. Then we create a variable for the array, which holds all the cells in the row.

// Selecting all parameters from the database.

$query = "SELECT * FROM $tableName";
$result = mysql_query($query);

// We get the row array, which is associative.

$row = mysql_fetch_array($result);

// We get values from the individual rows.

$alcounts = $row['allcounts'];

/* 
* If the row containing all counts is empty, we add rows. This allows 
* making a table empty and immediately a row is formed again without 
* touching the hardcopy database.
*/

if(!isset($alcounts))
{
	$alcounts=1;
	$query = "INSERT INTO `$tableName` (`id` ,`allcounts`) VALUES ('0', '$alcounts');";
	$result = mysql_query($query);
}
echo "myResult=". urlencode($xmlNodes);

The following lines send an XML node to the flash movie, which holds the unique and the total number of visits.

// We create a XML node with the data, which is sent back to Flash.

$xmlNodes = "<alcounts>".$alcounts."</alcounts>";
echo "myResult=". urlencode($xmlNodes);

We then need to increment the values by one. In case of unique visitors we do that only if there is no cookie with the cookiename we gave our cookie and when the variable $uniqueCounts has a value.

/* 
* If we want unique visitors we use the cookie information and increase only
* when the cookie is missing. We increase the value for either unique or All
* visitors by 1.
*/

if($uniqueCounts)
{
	if (isset($_COOKIE[$cookName]))
	{
		$alcounts=$alcounts;
	}
	else
	{
		$alcounts=$alcounts+1;
	}
}
else
{
	$alcounts=$alcounts+1;
}

$query = "UPDATE $tableName set allcounts='$alcounts'";
$result = mysql_query($query);

The rest of the script are functions in case something went wrong.

// These are functions in case of failure to notify. They are optional.

if($result)
{
	success();
}
else
{
	fail("Could not update table.");
}

function success()
{
	exit();
}

function fail($errorMsg) 
{
    echo "myError=$errorMsg";
}

?>

previous