-2

This is my function:

public function showDataall($result) 
    {
        $q = $this->conn->prepare($result) or die("failed!");
        $q->execute();
        while ($r = $q->fetch(PDO::FETCH_ASSOC)) 
        {
            $data[] = $r;
        }
        return $data;
    }

This function perfectly work in old xampp but new xampp return a Notice:

Undefined variable: data in /opt/lampp/htdocs/live/demo/model/config.php on line 152

Cœur
  • 34,719
  • 24
  • 185
  • 251
Dasans
  • 15
  • 1
  • 8

1 Answers1

3

Declare variable before using it :

If your query returns no data, your current code will never actually create the $data array, and therefore when you try and return it, this error will happen.

public function showDataall($result) 
    {
        $q = $this->conn->prepare($result) or die("failed!");
        $q->execute();
        $data = array();
        while ($r = $q->fetch(PDO::FETCH_ASSOC)) 
        {
            $data[] = $r;
        }
        return $data;
    }
RiggsFolly
  • 89,708
  • 20
  • 100
  • 143
Ranjit Shinde
  • 1,114
  • 1
  • 7
  • 22