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

  • Home
  • SEARCH
  • 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 8925017
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T07:35:01+00:00 2026-06-15T07:35:01+00:00

class dbConnection { public $pdo; public function __construct() { $this->dbConnect(); } public function dbConnect()

  • 0
class dbConnection {
public $pdo;
public function __construct() {
    $this->dbConnect();
}
public function dbConnect() {
    if((count($_POST) == 6)&&($_GET['a'] == "connect")) {
        $host = $_POST['host'];
        $port = $_POST['port'];
        $username = $_POST['username'];
        $password = $_POST['password'];
        $database = $_POST['database']; 
        try{
                $this->pdo = new PDO('mysql:host='.$host.';dbname='.$database.';port='.$port, $username, $password );
                echo 'Connection successful!';
                return $pdo;
        }
        catch(PDOException $e){
                echo 'Error: ' . $e->getMessage();
        }   
    }
}
}

class Group extends dbConnection { //Class for group, for ex. employe and employers.
public $name;  // Name of group
public $pdo;

public function __construct ($_name, $conn) {
$this->pdo = $conn;
$this->name = $_name;

}

public function getGroupList() {
    if(isset($this->pdo)) {
        try
        {
            $conn = $this->pdo;
          $conn ->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); //to catch exceptions

          $stmt = $pdo -> query('SELECT id, Name, Skills FROM '.$this->name); //sql query with group name
          $list = $stmt->fetchAll(PDO::FETCH_NUM); //fetch statement into array
          $stmt -> closeCursor();
          unset($stmt);
          return var_dump($list); //gives pure data
        }
        catch(PDOException $e) 
        {
          return 'There was some error: ' . $e->getMessage();
        }
    }
    else {
    $pdo;
    }
}

}

And execution:

$conn = new dbConnection;
$workers = new Group("workers", $conn);
$workers->getGroupList();

I get error:

Call to undefined method dbConnection::setAttribute()
on line:

$conn ->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); //to catch exceptions

I’m trying to solve that, I don’t know why php treats PDO object as a method. I don’t have any idea. And I have to write more letters.

  • 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-15T07:35:02+00:00Added an answer on June 15, 2026 at 7:35 am

    $this->pdo in that context references a dbConnection object. Really, you should change the name to $this->conn or something similar for clarity.

    To fix it, try:

    $this->pdo->pdo->setAttribute();
    

    or specifically, in the context of your code:

    $conn->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    

    However, it also seems you’re using Inheritance incorrectly.

    Rather than passing a connection to the Group class, you should instead call the parent constructor so that it initializes properly, setting the value of pdo per its definition.

    Try this:

    class Group extends dbConnection { //Class for group, for ex. employe and employers.
        public $name;  // Name of group
    
        public function __construct ($_name) {
            $this->name = $_name;
            parent::__construct();
        }
    

    From there, you can reference $this->pdo as it’s defined in dbConnection

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i declared classes like this: class Foo{ public function __construct(){ echo 'Foo was created!';}
this is the function in class that i use public function content($search) { $this->dbconnection();
So Far class PDOExtender { private $_DBO; public function openConnection() { $dsn = mysql:host=.DB_HOST.;dbname=.DB_NAME;
I have a utility class that creates & returns a db connection: Public Shared
class Widget; std::vector< std::shared_ptr<Widget> > container class Criterium { public: bool operator()(const Widget& left,
I've created a class that extends DbConnection in a brand new project. public class
class Group extends dbConnection { //Class for group, for ex. employe and employers. public
import MySQLdb import sys from libdesklets.controls import Control from IDBConnection import IDBConnection class DBConnection(Control,
I have this class that contains vars for db connection; Imports Microsoft.VisualBasic Imports System.Data.SqlClient
class A: pass def b(self): print('b') A.b = b a = A() At this

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.