Sunday, July 25, 2010

Database handling in php.

 

first open phpmyadmin and then create a new database named “test”( for the specific purpose of this example).

now create a table inside it named “counter” with a field “count” in it. ( integer,NULL,unsigned)

Now, create a file named server_auth.php in (root)/config folder. this folder you must remove all accessing permissions.

in server_auth.php

<?php 
$db_host = "localhost";
$db_user = "root";
$db_password = "";
$db_name = "test";
?>



 



create another file in your webroot named “ counter.php



<?php 
require($_SERVER["DOCUMENT_ROOT"]."/config/server_auth.php");
$connection = @mysql_connect($db_host,$db_user,$db_password) or die("error connecting the database!");
mysql_select_db($db_name, $connection);
$query = "SELECT * FROM counter";
$result = mysql_query($query, $connection) or die(mysql_error());
$views = mysql_result($result, 0, "count");
$views++;
$query = "UPDATE counter SET count = $views";
mysql_query($query, $connection) or die(mysql_error());
echo "This page has been viewed ".$views. " times.";
?>



ALL DONE! just open that counter.php in your browser and it will display a hit count!

No comments:

Post a Comment