Need some help with setting up a general query method using PDO

Hi All,

I would appreciate some help on why my general query method is not working. Help would be greatly appreciated. What I mean by general is instead of “SELECT * FROM” it’s using an argument to be passed in instead—see below: (also added code in txt files)

This find_all() query method works:
[php]
public static function find_all() {
global $database;
$query = $database->query(“SELECT * FROM users”);
$result_set = $database->resultall();
return $result_set;
}
[/php]

Methods (query, execute and resultall) called from database class
[php]
public function resultall() {
$this->execute();
return $this->stmt->fetchAll(PDO::FETCH_ASSOC);
}

public function query($query) {
$this->stmt = $this->dbh->prepare($query);
}

public function execute() {
return $this->stmt->execute();
}
[/php]

General query method: find_by_sql code to be used in place of the find_all() code
[php]
public static function find_by_sql($sql = “”) {
global $database;
$stmt = $database->query($sql);
$result = $stmt->execute();
return $result->resultall();
}
[/php]

Find_all code now using the find_by_sql method is not working with the “Fatal error: Call to a member function execute() on a non-object in C:\xampp\htdocs\projects\photo_gallery\includes\user.php on line 61” error
[php]
public static function find_all() {
return self::find_by_sql(‘SELECT * FROM users’);
}
[/php]


database_forum_post.txt (1.64 KB)

user_forum_post.txt (925 Bytes)

Sponsor our Newsletter | Privacy Policy | Terms of Service