I'm probably missing something basic... I'm fairly new to php's OOP.
I'm trying to use an instance of PDO I created in a method of a class but for some reason (and this is where your help will be appreciated) I keep getting: Call to undefined method db::query()
Here's my code:
// db.inc.php
try {
$db = new PDO("mysql:host=localhost;dbname=database",user, pass);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch(PDOException $e) {
echo $e->getMessage();
}
// my class in question
class myClass
{
protected function myFunction() {
$query = "SELECT * FROM somewhere";
if ($result = db::query($query)) {
return $result->fetch(PDO::FETCH_ASSOC);
} else {
// Some Stuff
}
}
Any thoughts on why my class isn't seeing the $db object? I've tested my connection, it's good. If I take the offending code out of the class and use it procedurally, it works :bemused:
I've also tried referencing it as $db->query but I'm pretty sure that's wrong.
Any help or suggestions would be greatly appreciated! Been racking my brain all night on this 🙁