Jump to content

Returns empty array on fetch (PDO)


akosibumblebee

Recommended Posts

Can anyone please help me te resolve my issue.

 

I have a parent class which is my database config.

A child class which I am calling on my script.

 

Did I miss something ?

 

 

Here is my code sir:

 

The parent class

class myPDO{
    private $dbh;
    private $error;        
    private $stmt; 
public function connect(){
        $dsn = "mysql:host=localhost;dbname=testdb;";
       $options = array(
PDO::ATTR_EMULATE_PREPARES => false, 
            PDO::ATTR_PERSISTENT  => true,
            PDO::ATTR_ERRMODE       => PDO::ERRMODE_EXCEPTION
        );
        try{
         $this->dbh = new PDO($dsn, 'username', 'passwd', $options);
        }
        catch(PDOException $e){
            $this->error = $e->getMessage();
return null;
        } 
} 
    public function query($sql){
$this->stmt = $this->dbh->prepare($sql);
    }
    public function fetch_assoc(){
return $this->stmt->fetchAll(PDO::FETCH_ASSOC);
    }
}
 

child class

class myChild extends myPDO {
 public function getMe($myId){
  myPDO::connect(); 
  $sql = "SELECT * FROM table WHERE id = '$myId'"; 
  myPDO::query($sql); 
  $row = myPDO::fetch_assoc(); 
  return $row;
 }
}

$myId = 1;
$myChild = new myChild;
$row = $myChild->getMe($myId);
print_r($row);   // returns empty

Am I missing something?

 

It gives me the result as empty but there is a date on the table with that id.

 
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.