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 7027777
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T00:17:36+00:00 2026-05-28T00:17:36+00:00

I am learning OOPS concept and also using PDO , but really stuck in

  • 0

I am learning OOPS concept and also using PDO , but really stuck in very basic problem, please look into my code and tell me what i am doing wrong and how to optimize this code.

###class.db.php

class DB
{
    public $dbh;
    private $qry;
    
    public function __construct($user ='root',$pass = '')
    {
        try {
            $this->dbh = new PDO('mysql:host=localhost;dbname=parixan',$user,$pass);
            $this->dbh->exec("SET CHARACTER SET utf8"); 
            echo "connected";
        } catch(Exception $e){
            die("Unable to connect: " . $e->getMessage());
        }       
    }
} 

###class.user.php

class User
{
    public $db;
    protected $_table ='tbl_user';
    public function __construct($dbh)
    {
        $this->db = $dbh;       
    }
    public function getUserDetails($id) //line #10
    {
        $qry = "SELECT * FROM $this->_table WHERE id = :userID LIMIT 1";
        $stmt = $this->db->prepare($qry); //line #13
        $stmt->bindParam(':userID',$id,PDO::PARAM_INT);  
        if ($stmt->execute()) {
        while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
            echo "<pre>";print_r($row);
        }
    }

    }
    
}

###user.php

include_once('class.user.php');
include_once('class.db.php');
$dbh = new DB();
$obj_user = new User($dbh);
$obj_user ->getUserDetails(1);

on running user.php I got this one

connected Fatal error: Call to undefined method DB::prepare() in
C:\xampp\htdocs\oops\class.user.php on line 13

Thanks.

  • 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-05-28T00:17:37+00:00Added an answer on May 28, 2026 at 12:17 am

    Within the User object, $this->db is a reference to an object of the DB class. This class does not implement a prepare method, so there is none available when you attempt to call it with $stmt = $this->db->prepare($qry);.

    If you persist with your current class design, you need to create such a function:

    public function prepare($qry) {
        return $this->dbh->prepare($qry);
    }
    

    This means you will now have a PDOStatement object, on which you can call bindParam and execute.

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

Sidebar

Related Questions

Learning jquery, so please be kind :) Using PHP, I have a table with
I'm learning Java and OOPS and while programming a basic Hello World in eclipse,
I am a PHP developer but now I am learning asp.net with C# as
Learning Ruby. Needed to create a hash of arrays. This works... but I don't
Learning spine.js I completed both the tutorials no problem, seems like a great framework,
For learning purposes, I'm trying to make a function using Python that takes in
I am new to Java web development (but not new to Java/OOPs). I am
learning Jquery and integrating with PHP - getting there, but have one last challenge
Learning R language - I know how to do a moving average but I
Learning this very slowly... got some books today and they just plain suck.. so..

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.