Jump to content

redeclared functions


ShorStew

Recommended Posts

As part of a database connection script (located in unique file in a directory with all includes), I have a function defined to escape retrieved data:

 

<?php # Database Connect Script

//Establishes connection to the Registration Databse

//Set the access information as constants
if (!defined('DB_USER')) {DEFINE ('DB_USER', '**********');}
if (!defined('DB_PASSWORD')) {DEFINE ('DB_PASSWORD', '**********');}
if (!defined('DB_HOST')) {DEFINE ('DB_HOST', '***********');}
if (!defined('DB_NAME')) {DEFINE ('DB_NAME', '**********');}

//Make the Connection
$dbc = @mysql_connect (DB_HOST, DB_USER, DB_PASSWORD) OR die ('Could not connect to MySQL: ' .mysql_error() );

//Select the database
@mysql_select_db (DB_NAME) OR die ('Could not select the database: ' .mysql_error() );

// Create a function for escaping the data.
function escape_data ($data) {

// Address Magic Quotes.
if (ini_get('magic_quotes_gpc')) {
	$data = stripslashes($data);
}

// Check for mysql_real_escape_string() support.
if (function_exists('mysql_real_escape_string')) {
	global $dbc; // Need the connection.
	$data = mysql_real_escape_string (trim($data), $dbc);
} else {
	$data = mysql_escape_string (trim($data));
}

// Return the escaped value.	
return $data;

} // End of function.
?>

 

 

There are many cases where I will require this file more than once on a single page (multiple queries to the database), and I am now getting a "Fatal Error: cannot redeclare..."

 

I understand why I get this error, but what I do not understand is why I don't get the error in another directory on the same server with exactly the same files and codes. To shed some light, this is a registration system that renews every six months. To begin the new round of registration, I create a new directory (by copying the most previous round), create a new database, and go. Until now... I get the fatal error. I am trying to avoid rewriting all the code so that each page only opens and closes the database connection once.

 

Any help is appreciated in advance.

Link to comment
https://forums.phpfreaks.com/topic/67888-redeclared-functions/
Share on other sites

Both good and correct answers, but I am looking to understand why in one case I get the error, and in the other I do not. I was also looking for a way to not rewrite 100 files of code.

 

I did figure out a fix, which is probably obvious to those that are experienced, but I will pass it along to other novices as myself: Using the command if(!function_exists) allowed me to bypass any repetitive call to the function. The revised code is below:

<?php # Database Connect Script

//Establishes connection to the Registration Databse

//Set the access information as constants
if (!defined('DB_USER')) {DEFINE ('DB_USER', 'shorste3_dbuser');}
if (!defined('DB_PASSWORD')) {DEFINE ('DB_PASSWORD', 'dbpassword');}
if (!defined('DB_HOST')) {DEFINE ('DB_HOST', 'remotemysqlhost');}
if (!defined('DB_NAME')) {DEFINE ('DB_NAME', 'shorste3_briregsys17');}

//Make the Connection
$dbc = @mysql_connect (DB_HOST, DB_USER, DB_PASSWORD) OR die ('Could not connect to MySQL: ' .mysql_error() );

//Select the database
@mysql_select_db (DB_NAME) OR die ('Could not select the database: ' .mysql_error() );

// Create a function for escaping the data.
if(!function_exists('escape_data')){
function escape_data ($data) {

// Address Magic Quotes.
if (ini_get('magic_quotes_gpc')) {
	$data = stripslashes($data);
}

// Check for mysql_real_escape_string() support.
if (function_exists('mysql_real_escape_string')) {
	global $dbc; // Need the connection.
	$data = mysql_real_escape_string (trim($data), $dbc);
} else {
	$data = mysql_escape_string (trim($data));
}

// Return the escaped value.	
return $data;

}
} // End of function.
?>

Link to comment
https://forums.phpfreaks.com/topic/67888-redeclared-functions/#findComment-341303
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.