开发者

PHP error - cannot redeclare function [duplicate]

开发者 https://www.devze.com 2023-03-12 14:45 出处:网络
This question already has answers here: "Fatal error: Cannot redeclare <function>" (18 answers)
This question already has answers here: "Fatal error: Cannot redeclare <function>" (18 answers) Closed 4 years ago.

I have a JavaScript funct开发者_JAVA百科ion 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.


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


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.


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


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.


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;
}
0

精彩评论

暂无评论...
验证码 换一张
取 消