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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T10:07:31+00:00 2026-05-27T10:07:31+00:00

I am trying to pass values gathered from PDO in the Datamanager object to

  • 0

I am trying to pass values gathered from PDO in the Datamanager object to the User object.

  1. The user object is initally called with a request to retrieve user with ID.
  2. The user creates a connection to the datamanager in the construct.
  3. The datamanager construct calls the PDO connection.
  4. Running the user select creates the query to preform on the database.
  5. The query is then passed to the datamanager which handles the query and returning the results.
  6. This results should be passed / set to the user object.

I’m not sure if it is step 5 or 6 that is wrong.

class Datamanager {
public function __construct() {
    try {
        $this->dbh = new PDO("mysql:host=$this->hostName;dbname=$this->dbName", $this->dbUser, $this->dbPassword);
    } catch(PDOException $e) {
        echo $e->getMessage();
    }
}
public function runQuery($query, $obj){
    try {
        $STH = $this->dbh->query($query);
        $STH->setFetchMode(PDO::FETCH_INTO, $obj);
    } catch(PDOException $e) {
        echo $e->getMessage();
    }
    return($obj);
}
class User {
public $user_id;
public $user_name;
public $user_password;
public $session_id;
private $dbc;

public function __construct() {
    $this->dbc = new Datamanager();
}

function selectUser ($userID, $obj) {
    $query = 'SELECT user_id, user_name, user_password, session_id FROM users';
    $results = $this->dbc->runQuery($query, $this);
}

$userID = "1";

$test = new User;

$test->selectUser($userID, $test);

I run the initial setup of user then try to load it with the values retrieved in the datamanager. I understand that the PDO is an object itself but I am trying to still create a general interaction because this code will be expanded upon to allow more then the user object to access the database.

  • 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-27T10:07:32+00:00Added an answer on May 27, 2026 at 10:07 am

    Here’s my version.

    class Datamanager {
    
        public $hostName = 'localhost';
        public $dbName = 'stackoverflow';
        public $dbUser = 'xxx';
        public $dbPassword = 'xxx';
    
        public function __construct() {
            try {
                $this->dbh = new PDO("mysql:host=$this->hostName;dbname=$this->dbName", $this->dbUser, $this->dbPassword);
            } catch (PDOException $e) {
                echo $e->getMessage();
            }
        }
    
        public function runQuery($query, $obj) {
            try {
                $STH = $this->dbh->prepare($query);
                $STH->bindParam(':id', $obj->user_id);
    
                $STH->setFetchMode(PDO::FETCH_INTO, $obj);
                $STH->execute();
                $STH->fetch();
            } catch (PDOException $e) {
                echo $e->getMessage();
            }
            return $obj;
        }
    
    }
    
    class User {
    
        public $user_id;
        public $user_name;
        public $user_password;
        public $session_id;
        private $dbc;
    
        public function __construct() {
            $this->dbc = new Datamanager();
        }
    
        function selectUser($userID, $obj) {
            $this->user_id = $userID;
            $query = 'SELECT user_id, user_name, user_password, session_id FROM users WHERE user_id=:id';
            $results = $this->dbc->runQuery($query, $this);
        }
    
    }
    
    $userID = "1";
    $test = new User;
    
    $test->selectUser($userID, $test);
    
    var_dump($test);
    

    I added a WHERE user_id=:id to your query and changed the pdo to use prepare().

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

Sidebar

Related Questions

Iam trying to pass dynamic argument values i.e username from request using spring ioc.But
I am trying to pass 2 values to a javascript xmlHttp request. The values
I have this code. Trying to pass form values from one internal page to
I am using this code. Trying to pass form values from one internal page
I'm trying to pass a ViewData object from a master page to a view
Im trying to pass some values into a text box from a child page
Am trying to pass an array of values from PHP function to Javascript. Not
I'm trying to pass some lat/long values from a JSON doc to iPhone's Map
I'm trying to code a way to pass the values from two spinners into
I'm trying to pass some values from one .aspx page to an handler (.ashx).

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.