0

Essentially I have the following query that returns multiples rows - for each row I need to execute a function report() that is within another file I am calling at the top of my main file using include_once (because my intent is call the file once - but the function inside it multiple times)

The error I get is:

Fatal error: Cannot redeclare report() (previously declared in...

This is how I call the function within the loop:

    try
    {

        $pdo->beginTransaction();
        $stmt = $pdo->prepare("

   SELECT DISTINCT
    name,
    address
   FROM t1
     ");
        $stmt->execute();
        $GLOBALS['rowCount'] = $stmt->rowCount();
        $row = $stmt->fetchAll();
        $rowCount = $stmt->rowCount();
        foreach ($row as $rowvalue)
        {
           
            $GLOBALS['var1'] = $rowvalue["name"];
            $GLOBALS['var2'] = $rowvalue["address"];
            report();
        }

        $pdo->commit();

    }
    catch(PDOException $e)
    {
        //roll back the changes on errors
        $pdo->rollback();
        echo $e->getMessage();

    }

How can this be resolved?

John
  • 965
  • 8
  • 16
  • 2
    Can you confirm that the function is not declared multiple times or you included the file that houses the function multiple times in the entire flow (not just the current context). What have you done to debug this? – Hisham Jul 18 '21 at 20:13
  • 1
    What @Hisham wrote, you were that "clever" to cut away with the `...` the name of the file where the `report` function _is_ defined first .... ? These error messages require reading ;) - see as well: [Reference - What does this error mean in PHP?](https://stackoverflow.com/questions/12769982/reference-what-does-this-error-mean-in-php) – hakre Jul 18 '21 at 20:16
  • 1
    Not necessarily related to the problem, but always keep in mind that using superglobal variables can cause undesired [side effects](https://en.m.wikipedia.org/wiki/Side_effect_(computer_science)). – digijay Jul 18 '21 at 20:30

0 Answers0