Home » Php » PHP error – cannot redeclare function

PHP error – cannot redeclare function

Posted by: admin July 12, 2020 Leave a comment

Questions:

I have a JavaScript function making a call to a PHP script. So far so good. A problem happens when I try to do this:

$hike_id = mysql_real_escape_string($_GET['hike_id']);

When I import my connection file, it gives me an error that the functions in that file have already been defined and the error is this:

[Fri Jun 10 12:34:43 2011] [error] [client 75.24.105.18] PHP Fatal error:  Cannot redeclare hassuspicioushackerstrings() (previously declared in /home/webadmin/comehike.com/html/connect.php:16) in /home/webadmin/comehike.com/html/connect.php on line 40

The error it is referring to is a function that is in the connect script.

But if I remove the

include '../connect.php';

Then it will just tell me that I can not use the mysql_real_escape_string function. So I am kind of stuck between not being able to use either option.

How to&Answers:

try include_once '../connect.php'; it’ll make sure you’re only including once this file

Answer:

Take a look at your files and your includes… You are declaring that function twice, that is the error. It has nothing to do with MySQL, database connections, or mysql_real_escape_string().

I.e. You may be including file A and file B, but file A already includes file B… You can either figure out where your includes are going wrong, or use include_once or require_once to prevent it from double-loading.

Answer:

Likely you are including the file multiple times. Use require_once instead of include.

Answer:

you cant use mysql_real_escape_string() because connect.php is most likely setting up your database connection. My guess is there is another include (perhaps ‘functions.php’) that has the same function.

You probably have something like this:

function hassuspicioushackerstrings($input) {
}

in your connect.php, you could add if(!function_exists('hassuspicioushackerstrings')) { and } around the function.

Answer:

Never create or declare a function inside another function. But you can still use other functions inside a function. For example the following is not right

function addition($a, $b)
{
    function subtraction(){
    }
    return $a+$b;
}