Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8299205
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T16:04:29+00:00 2026-06-08T16:04:29+00:00

I have a problem with my PDO class. I just have started learning OOP

  • 0

I have a problem with my PDO class. I just have started learning OOP and I don’t really know where I made a mistake

My class code:

<?php

class admin
{
  private $host = 'mysql:host=localhost;dbname=db501865';
  private $username = 'root';
  private $password = 'root';
  private $conn;


  public function connect() {
      try {
          $conn = new PDO($this->host, $this->username, $this->password);
      } catch ( PDOException $e ) {
          die( 'Connection failed: ' . $e->getMessage() );
      }
      return $conn;
  }

  public function disconnect( $conn ) {
      $conn = '';
  }


  public function listReal()
    {
        $this ->connect();
        $real = $conn->query('SELECT * FROM `real`');
        echo '<ul>';
        foreach ($real as $row)
        {
            echo'<li><img src="'.$row['image'].'"></li>';
        }
        $real -> closeCursor();
        echo'</ul>';
    }

}
?>

and after executing following code I have 500 error in my browser.

$db = new admin;
$db -> listReal();

Where I made a mistake?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T16:04:30+00:00Added an answer on June 8, 2026 at 4:04 pm

    You have to use $this->var_name for your member variables.
    Also there is no need to connect if you are already connected.

    class admin
    {
        private $host = 'mysql:host=127.0.0.1;dbname=test';
        private $username = 'root';
        private $password = 'local@pass';
        private $conn;
    
    
        public function connect() {
            if($this->conn) return;
            $this->conn = new PDO($this->host, $this->username, $this->password);
            $this->conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        }
    
        public function disconnect( $conn ) {
            $conn = '';
        }
    
    
        public function listReal()
        {
            $this ->connect();
            $real = $this->conn->query('SELECT * FROM `real`');
    
            echo '<ul>';
            foreach ($real as $row)
            {
                echo'<li><img src="'.$row['user_id'].'"></li>';
            }
            $real -> closeCursor();
            echo'</ul>';
        }
    
    }
    try{
        $db = new admin;
        $db -> listReal();
    } catch(Exception $e) {
        echo 'error: '.$e->getMessage();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm newish to PHP and OOP and I have been using the PDO class
I'm having a strange problem with php PDO and mysql. I have the following
I am learning to work with PHP and have a simple problem. <?php ini_set('display_errors',
I have a problem in a php script where I am using PDO to
I have a database class that I made that uses PDO to connect to
I've used PDO in my PHP application. But I have problem with fetch() function.
I have a helper class DAO (I do not know if is OK have
I have the following test-class made on the fly: http://sumoin.pastebin.com/ff744ea4 No fine-tuning or something
I have a problem in the following code: $query = 'SELECT user.id as id,
Question solved... silly mistake i have a little problem with my database update script

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.