Fatal error: Cannot redeclare dbconnection()
Posted: Thu Aug 27, 2009 4:13 am
Hi,
i have a small project in that there are 3 files one is db.php, functions.php and index.php. I have kept all the 3 files in 3 different folders. But when i run my project i.e index.php the connection is succesful but the following error coming. can anyone give solution.
Connection Successfull
Data appended to file & Status changed from 0->1
Fatal error: Cannot redeclare dbconnection() (previously declared in C:\xampp\htdocs\phoneupload\functions\functions.php:5) in C:\xampp\htdocs\phoneupload\functions\functions.php on line 26
Code:
index.php
db.php
functions.php
i have a small project in that there are 3 files one is db.php, functions.php and index.php. I have kept all the 3 files in 3 different folders. But when i run my project i.e index.php the connection is succesful but the following error coming. can anyone give solution.
Connection Successfull
Data appended to file & Status changed from 0->1
Fatal error: Cannot redeclare dbconnection() (previously declared in C:\xampp\htdocs\phoneupload\functions\functions.php:5) in C:\xampp\htdocs\phoneupload\functions\functions.php on line 26
Code:
index.php
Code: Select all
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title></title>
</head>
<body>
<?php
include("include/db.php");
include("functions/functions.php");
?>
</body>
</html>
Code: Select all
<?php
include("functions/functions.php");
//include("functions.php");
dbConnection();
//Main logic Code
$result = fetchRecord();
// Passing the fetched data as a parameter
processData($result);
?>Code: Select all
<?php
function dbConnection(){
//connection parameters
$username = "root";
$password = "";
$dbhost = "localhost";
//connection to database
$dbconnection = mysql_connect($dbhost, $username, $password) or die("Could not connect to MySQL Database". mysql_error());
//selecting a database
$dbselect = mysql_select_db("test", $dbconnection);
//$dbselect = mysql_select_db("test");
//checking whether the database is selected
if(!$dbselect){
echo "Error in Connection String";
}
else
{
echo "Connection Successfull";
}
echo "<br/>";
}
//user defined functions
//
//function to fetch records
function fetchRecord(){
$query = "SELECT * FROM phone_dummy LIMIT 0,7";
$result = mysql_query($query);
//$num=mysql_numrows($result);
if (!$result)
{
$message = 'Invalid query: ' . mysql_error() . "\n";
$message .= 'Whole query: ' . $query;
die($message);
}
return($result);
}
//function to open a file
function fileOpen($fileheader)
{
$myFile = "phonedetails.txt";
//$myFile = "phonedetails.xls";
$fh = fopen($myFile, 'w') or die("can't open file");
$stringData = "$fileheader"."\r\n";
fwrite($fh, $stringData);
fclose($fh);
}
//function to write to a file
function fileWrite($export)
{
$myFile = "phonedetails.txt";
//$myFile = "phonedetails.xls";
$fh = fopen($myFile, 'a') or die("can't open file");
$stringData = "$export";
fwrite($fh, $stringData);
fclose($fh);
}
//function to process data
function processData($result)
{
//$fileheader = "ID|Phone|Name|Address|Status"."\r\n";
//$fileheader = "ID|Phone|Name|Address|Status"."\r\n";
fileOpen($fileheader);
while ($row = mysql_fetch_assoc($result))
{
$phone_id = $row['phone_id'];
$phone_number = $row['phone_number'];
$name = $row['name'];
$address = $row['address'];
$status = $row['status'];
$export = "$phone_id|$phone_number|$name|$address|$status"."\r\n";
fileWrite($export);
if($status == 0)
{
$updatequery = "UPDATE phone_dummy SET status = 1 WHERE phone_id = $phone_id";
mysql_query($updatequery);
}
}
echo "Data appended to file & Status changed from 0->1";
//echo "Status changed from 0->1";
}
?>