I am using PDO in PHP … I have the following problem.
The following code does not work.
class A {
private $getUsersQuery = "SELECT * FROM users";
...
public function getUsers() {
$DBH = A::getDatabaseConnection();
try {
$query = $DBH->prepare($this->getUsersQuery);
...
} catch(PDOException $e) {}
}
}
But if I use the string it works.
$DBH->prepare("SELECT * FROM users");
Even if I use the echo outside the prepare() it works …
echo $this->getUsersQuery; // Outputs the sql string.
Can someone point out what the problem might be.
Update :
Error :
SQLSTATE[42000]: Syntax error or access violation: 1065 Query was empty
Thanks
As mentionned in your comment, you make a static call to a method that is not static.
Using
$thisin a method that is called like this makes no sense:Instanciate your class, and then call the method on the class object.
Or make your method and SQL query static.