-1

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

Fox
  • 9,384
  • 13
  • 42
  • 63
  • This should work fine. Check your code carefully for typos. – Michael Berkowski Nov 17 '12 at 22:29
  • 3
    Agreed it should work. What exactly do you mean by 'does not work'? Any error message? – Tchoupi Nov 17 '12 at 22:31
  • 1
    The most important question here is: How does it NOT work? What error message to you get, etc.? – markus Nov 17 '12 at 22:35
  • Your code looks Ok. why did you say it's not working? – codingbiz Nov 17 '12 at 22:35
  • 1
    Well, I wouldn't say the code looks perfect but we really need to know what errors you get. – markus Nov 17 '12 at 22:36
  • I get the following error SQLSTATE[42000]: Syntax error or access violation: 1065 Query was empty ... But echo $this->getUsersQuery; shows the query string.. – Fox Nov 17 '12 at 22:38
  • 1
    how do you call getUsers() ? – Dr.Molle Nov 17 '12 at 22:39
  • 2
    try-catch blocks are just useful if you are doing something with the exception (i. e. output the error message to a file). if you are using it to hide error messages you will never know why your code not work. – iRaS Nov 17 '12 at 22:39
  • @Dr.Molle I call getUsers() from another class like this $results = A::getUsers(); – Fox Nov 17 '12 at 22:42
  • what kind of database handle do you get? may be the method prepare gets a reference (i. e.: public function prepare(&$query) {...}) and the reference is not accessible for the database handle class. – iRaS Nov 17 '12 at 22:43
  • @iRaS I think the DB handle works fine coz ... $DBH->prepare("SELECT * FROM users"); does not give me any problem. – Fox Nov 17 '12 at 22:45
  • so you call a non static method statically? the instance variable is not available in there. you have to create an instance of A: "$a = new A(); $a->getUsers();" i always tell my team members they should enable strict warnings for development environment just because you can see your errors. – iRaS Nov 17 '12 at 22:45
  • How did this even pass parsing? If you a calling a non-static member function without an object? Something is very funky with this question. – Lightness Races in Orbit Nov 17 '12 at 22:48
  • thats right. i just tried this (does i remember correctly?). you have to get a fatal error: Using $this when not in object context – iRaS Nov 17 '12 at 22:51

2 Answers2

4

As mentionned in your comment, you make a static call to a method that is not static.

Using $this in a method that is called like this makes no sense:

$results = A::getUsers();

Instanciate your class, and then call the method on the class object.

$a = new A();
$a->getUsers();

Or make your method and SQL query static.

Tchoupi
  • 14,560
  • 5
  • 37
  • 71
  • How did the original code even parse? – Lightness Races in Orbit Nov 17 '12 at 22:50
  • @LightnessRacesinOrbit It seems that the OP stated otherwise :P But I do understand your point. I know that somehow PHP doesn't seem to care when you statically call a non static method, and most of the times will throw a warning but execute the code anyway. So why did it work with an `echo` and not in a method parameter is a mystery to me. – Tchoupi Nov 17 '12 at 22:51
  • 1
    @MathieuImbert: [Holy heck](http://ideone.com/iXz0Nn). What a sorry state of affairs... – Lightness Races in Orbit Nov 17 '12 at 22:52
  • @LightnessRacesinOrbit Yes I've noticed that in production while cleaning the error logs. My guess is that a simple `echo` PHP will just *convert* the non static variable to a static one. But in the case of a parameter, it needs to get a reference of it and fails. – Tchoupi Nov 17 '12 at 22:55
  • 1
    @MathieuImbert even the call to $this when called statically should throw a fatal error. at least in my php version (5.3.10) – iRaS Nov 17 '12 at 22:56
  • 1
    @iRaS It seems that this issue was discussed before: http://stackoverflow.com/questions/2439036/does-static-method-in-php-have-any-difference-with-non-static-method – Tchoupi Nov 17 '12 at 22:59
  • @MathieuImbert alright, thanks - that was new to me. so it is an object of class A that is is calling A::getUsers(). very complicated to debug, uh? – iRaS Nov 17 '12 at 23:06
0

Try

$a = new A();
$result = $a->getUsers();

The value of $getUsersQuery was probably not initialized when you called A::getUsers(). Static methods are called liked that.

codingbiz
  • 26,179
  • 8
  • 59
  • 96