Page 1 of 1

Fatal error: Cannot redeclare dbconnection()

Posted: Thu Aug 27, 2009 4:13 am
by phphunger
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

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>
 
db.php

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);
 
?>
functions.php

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";
           
}
 
?>
 
 

Re: Fatal error: Cannot redeclare dbconnection()

Posted: Thu Aug 27, 2009 4:32 am
by susrisha
use

Code: Select all

 
include_once();
 
instead of

Code: Select all

 
include();
 

Re: Fatal error: Cannot redeclare dbconnection()

Posted: Thu Aug 27, 2009 4:33 am
by phphunger
Oops!!!! i am a big dumb fellow.....i solved this...

Re: Fatal error: Cannot redeclare dbconnection()

Posted: Thu Aug 27, 2009 9:09 am
by phpfan
OF CORSE u r dumb fellow, hahahahahaha

Re: Fatal error: Cannot redeclare dbconnection()

Posted: Thu Aug 27, 2009 9:12 am
by jackpf
Learn to spell.