-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
  • 37,241
  • 25
  • 195
  • 267
Sambhu M R
  • 297
  • 5
  • 23

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
  • 93,638
  • 21
  • 103
  • 149
Ranjit Shinde
  • 1,121
  • 1
  • 7
  • 22