Search code examples
phpmacosapachefunction-declaration

PHP Complaing on redeclare in same fil, but only on declare. Bug?


I am getting following error message:

   [Wed Sep 21 21:19:46 2011] [error] [client 127.0.0.1] PHP Fatal error:  Cannot redeclare db_get_groups() 
(previously declared in /Library/WebServer/Documents/SMICAdmin/databasescripts/db_get_groups.php:4) 
in /Library/WebServer/Documents/SMICAdmin/databasescripts/db_get_groups.php on line 24, 
referer: http://localhost/SMICAdmin/index.php

As you can see, the error message complains that the same function, db_get_groups(), twice in the same script. But it's not, the entire file is included here (db_get_groups.php):

<?php

    function db_get_groups($dbconnection){  //Line 4

        $query = "SELECT id FROM groups";
        $result = mysqli_query($dbconnection, $query);

        $rows = mysqli_fetch_all($result, MYSQLI_ASSOC);
        $grouplist = Array();

        foreach ($rows as $key => $value) {
            error_log(" Value -> ".$value['id']);
            $grouplist[] = $value['id'];

        }

    return $grouplist;
    } //Line 24
    ?>

When doing a search for "db_get_groups" in the entire project I can only find the only declaration below and the file is inluded in two other files for usage.

I did try to find if I done some import of the file more than once in some way, couldn't find any.

What's the problem and how do I fix it? Feels really wired this one...


Solution

  • The bottom line is that the only way you would ever see that error message is if the function is defined twice. There's two common ways this could happen. You should double check both of these:

    1. Another file declares the function db_get_groups.
    2. db_get_groups.php is included multiple times. Consider using include_once / require_once instead of just include / require.