ShorStew Posted September 4, 2007 Share Posted September 4, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/67888-redeclared-functions/ Share on other sites More sharing options...
vijayfreaks Posted September 4, 2007 Share Posted September 4, 2007 Hi.. dont do like that.. do necessary settings related to path only.. and use the same file for ur purpose.. -vijay Quote Link to comment https://forums.phpfreaks.com/topic/67888-redeclared-functions/#findComment-341247 Share on other sites More sharing options...
trq Posted September 4, 2007 Share Posted September 4, 2007 Use include_once instead of include. Quote Link to comment https://forums.phpfreaks.com/topic/67888-redeclared-functions/#findComment-341294 Share on other sites More sharing options...
ShorStew Posted September 4, 2007 Author Share Posted September 4, 2007 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. ?> Quote Link to comment https://forums.phpfreaks.com/topic/67888-redeclared-functions/#findComment-341303 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.