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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.