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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T03:21:05+00:00 2026-06-13T03:21:05+00:00

I cant figure out how to assign each one user from my database as

  • 0

I cant figure out how to assign each one user from my database as class attribute and display them. Take a look. For example I have this table in my db:

+----+----------+------------+-----------+
| id | username | first_name | last_name |
+----+----------+------------+-----------+
|  1 | guu      |  Guu       | GuuShtein |
|  3 | user1    |  Allan     | Last      |
|  4 | Donny    |  Name      | Last Name |
+----+----------+------------+-----------+

And here is the class:

class users {

public $db;
private $id;
private $username;
private $first_name;
private $last_name;

public function __construct($db) {
    $this->db=$db;
}

private function find_by_id($id) {
   $record = $this->db->query('SELECT * FROM users WHERE id=' . $id .' LIMIT 1')->fetch(PDO::FETCH_ASSOC);     
   foreach ($record as $atribute => $value) {
       $this->$atribute = $value;
   } 
}

public function user_nfo($id){
    $this->find_by_id($id);

    $user_nfo = "User id: " . $this->id . "<br />";
    $user_nfo .= "Username: " . $this->username . "<br />";
    $user_nfo .= "User full name: " . $this->first_name . " " . $this->last_name . "<br />";       
    return $user_nfo;
}
}

$users = new mysql_a($db);
echo $users->user_nfo(1);

My question is how to write a method, that displays all users one by one? I mean something similar like in my user_nfo function, but just for all records? I tried “foreach” loop with PDOfetchAll, but it just overrides attributes and and displays only last record.

  • 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-13T03:21:06+00:00Added an answer on June 13, 2026 at 3:21 am

    What you want is a new object called user that has all the properties that you want, Like the below.

    <?php
    
    try {
        $db = new PDO('mysql:host=localhost;dbname=sub_db;charset=UTF-8', DB_USER, DB_PASS);
        $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        $db->setAttribute(PDO::ATTR_EMULATE_PREPARES, FALSE);
    } catch (PDOException $ex) {
        echo "error";
    }
    
    class users {
    
        public $db;
    
        public function __construct($db) {
            $this->db=$db;
        }
    
        /**
         * Fetch all users and return an array of their mapped objects
         *
         * @author Daniel Noel-Davies
         *
         * @return array
         */
        public function getAllUsers () {
            $users   = array();
    
            // Grab an array of each user
            $users = $this->db->query('SELECT * FROM users')->fetch(PDO::FETCH_ASSOC);
    
            // Loop through the users
            foreach( $users as $index => $user ) {
                // Replace each row with an object of that user (keeps memory down)
                $users[$index] = new User($user);
            }
    
            // return our array containing an object of each user
            return $users;
        }
    
        public function count_row(){
            $count = $this->db->query('SELECT COUNT(*) FROM users')->fetch(PDO::FETCH_ASSOC);
            return $count['COUNT(*)'];
        }
    
        public function find_by_id($row) { 
           $record = $this->db->query('SELECT * FROM users LIMIT 1 OFFSET ' . $row)->fetch(PDO::FETCH_ASSOC);
           return $record;
        }   
     }
    
    
    class user {
    
        public $id;
        public $username;
        public $first_name;
        public $last_name;
    
        public function __construct($record){    
               foreach ($record as $atribute => $value) {
                   $this->$atribute = $value;
               }
        }
    }
    
    // Initiate our users class
    $objUsers = new Users;
    
    // Fetch all the users
    $users = $objUsers->getAllUsers();
    
    // Loop through them all
    foreach ($users as $user) {
        // Using Printf keeps things neater, means you're template is tidier.
        printf(
            '%s<br />
            %s<br />
            %s<br />
            %s<br />',
    
            $user->id,
            $user->username,
            $user->first_name,
            $user->last_name
        );
    }
    ?>
    

    Does this help?

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

Sidebar

Related Questions

I cant figure out how to get lua to do any common timing tricks,
I want to do something but cant figure out how to do this (i
Im having a odd Problem with the TTS engine and I cant figure out
Hi I keep getting a syntax error but I cant figure out why. My
I am getting segmentation fault in this code but i cant figure out why.
This seems in my head like it should work but I cant figure out
Maybe I missed something, but I cant figure out why Visual Studio 2008 isn't
Total newbie question, but I cant figure out what im doing wrong. I want
I cant really figure out whats wrong with this. I used to write the
I need for Search method for a hashmap and I cant quite figure out

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.