PHP question: vars in classes?

I Heart

New member
(code below) I'e just started using classes in PHP and I'm kind of stuck. One method makes an object for the database connection and then another method uses that var to do a query but it says the variable is undefined ($dbh). How do I pass the var $dbh to the query method?

class mySql {

function __construct($hello) {
try{
$dbh = new PDO('mysql:host=localhost;dbname=pictobox','root','g0dzilla');

} catch(PDOException $e) {
return 'Error: '.$e->getMessage();
exit();
}
}

public function query($sql,$data){
$query = $dbh->prepare($sql);
$query->execute(array($data));
$result = $query->fetch(PDO::FETCH_ASSOC);
return $result;
}

}
 
Back
Top