8

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.

hakre
  • 193,403
  • 52
  • 435
  • 836
Genadinik
  • 18,153
  • 63
  • 185
  • 284
  • You seem to be including the file that declares `hassuspicioushackerstrings()`. I don't think this has to do with mysql_real_escape_string() – Pekka Jun 10 '11 at 16:45

5 Answers5

13

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

afarazit
  • 4,907
  • 2
  • 27
  • 51
  • the include_once thing did the trick - thank you! – Genadinik Jun 10 '11 at 16:50
  • 1
    @Genadinik you should also read what @Fosco wrote about your error. Try to fix your error by redoing your program logic, `include_once` and `required_once` aren't ideal as both consume valuable system resources to figure out if a file is already included/required or not. – afarazit Jun 10 '11 at 16:58
  • In some cases there may be an extra include statement within one of the children php files, that is where I found my error. The incude_once doesn't help in these situations. – grant zukowski Jul 20 '16 at 02:56
  • @grantzukowski no it doesn't as `include_once()` checks to see if the file you're trying to load is already included, it doesn't search your code recursively (it makes no sense to search recursively) – afarazit Jul 20 '16 at 09:32
4

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.

Fosco
  • 38,138
  • 7
  • 87
  • 101
2

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

datasage
  • 19,153
  • 2
  • 48
  • 54
1

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.

Joshua - Pendo
  • 4,331
  • 6
  • 37
  • 51
0

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;
}
K_B
  • 3,668
  • 1
  • 19
  • 29
machd
  • 1