Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

PHP PDO problem

Featured Replies

In the project I'm working on currently, I'm having an odd problem. I have a PDO object, stored in a parent class as a protected variable which is used by many child classes. I use it frequently, including several times in the current child class that I'm modifying. All of a sudden, within this class I'm writing, it stopped working.

 

I checked the stored procedure it's calling, and it works fine. I then checked the PHP code using it, by creating a new PDO object and replacing the one I normally used.

 

// $this->database = new PDO( DB_DSN, DB_USER, DB_PASS );
private function getPermissionsList( $id ) {
 $db = new PDO( DB_DSN, DB_USER, DB_PASS );
 $query = $this->database->prepare( "CALL spGetPermissionsList(:id);" );
 $query->bindParam( ':id', $id, PDO::PARAM_INT );
 $query->execute();
 $data = array();
 while ( $row = $query->fetch() )
  $data[ $row['id'] ] = ucfirst( $row['permission'] );
 return $data;
}

 

"$this->database" is the protected variable which stops working... if I replace it with the local variable "$db" as shown above, and use it on the line where the method "prepare" is called, everything works as intended. When using the inherited "$this->database" PDO object, the query simply returns an empty array.

 

I've tried using "echo $query->errorCode();" to find out which error was being produced, and it resulted with "00000" being printed. As I've said, I used this exact method many times throughout my project and this is the first time it has ever failed so inexplicably.

 

Does anyone know a reason why a PDO object would simply stop working like this? Any help would be greatly appreciated.

  • Author

Could you post a simple example that reproduces the problem?

 

The code I posted above is the precise code I'm currently using in my project which is not working properly. When I use "$db->prepare( .... );" instead of "$this->database->prepare( ... );" on that fourth line of code, it will work... however I'd like to use "$this->database" instead of "$db" for consistency purposes. Throughout this project, I've used the protected variable "$this->database" throughout my project and for some reason in this one instance it simply isn't working. No error is being produced (I checked using errorCode and var_dump) however it always returns an empty array when using "$this->database" for some reason.

Is the code failing only with that one child class? You could try posting a stripped down version of your parent and child class and the minimum amount of code that reproduces the problem, something that can be tested.

  • Author

Is the code failing only with that one child class? You could try posting a stripped down version of your parent and child class and the minimum amount of code that reproduces the problem, something that can be tested.

 

No, that "$this->database" variable was being used elsewhere and frequently within that same child class during the same page being processed as well as others. The problem seemed to be solved by using "closeCursor();" on the previous use of that PDO object.

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.