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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T08:52:37+00:00 2026-05-14T08:52:37+00:00

Greetings all, I’m looking for a more efficient way to grab multiple users without

  • 0

Greetings all, I’m looking for a more efficient way to grab multiple users without too much code redundancy.

I have a Users class, (here’s a highly condensed representation)

    Class Users {

    function __construct() { ... ... }

    public static function getNew($id) {

         // This is all only example code
         $sql = "SELECT Name,Email,Etc.. FROM Users WHERE User_ID=?";

         // Retrieval using prepared statements would go here
         $user = new User();
         $user->setID($id);
         ....

         return $user;
}
  public static function getNewestUsers($count) {
     $sql = "SELECT User_ID,Name,Email,Etc.. FROM Users ORDER BY Join_Date LIMIT ?";

     $users = array();

     // Prepared Statement Data Here

     $stmt->bind_result($id,$name,$email,$etc);

     while($stmt->fetch()) {
        $users[] = Users::getNew($id);
        ...
     }
     return $users;
   }
   // These have more sanity/security checks, demonstration only.
   function setID($id) { $this->id = $id; }
   function setName($name) { $this->name = $name; }
 ...
}

So clearly calling getNew() in a loop is inefficient, because it makes $count number of calls to the database each time. I would much rather select all users in a single SQL statement.

The problem is that I have probably a dozen or so methods for getting arrays of users, so all of these would now need to know the names of the database columns which is repeating a lot of structure that could always change later.

What are some other ways that I could approach this problem? My goals include efficiency, flexibility, scalability, and good design practice.

  • 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-14T08:52:37+00:00Added an answer on May 14, 2026 at 8:52 am

    Simplification of boilerplate SQL binding

    This isn’t so much a solution as a suggestion:

    You’re running pretty bare to the metal here as far as sql. I recommend separating your sql out a bit more, one way that I’ve found useful is creating a simple database abstraction library function, let’s call it query. Using Mikushi’s example above:

    // params: $select = full select statement with binding placeholders
    // params: $bindings = either associative or numeric array, whichever fits your database binding method better
    function query($select, $bindings=array()){
    $res = array();
    
    // Loop over each binding here
    // Run the bound select statement, get the $res result
    
    while($row = mysql_fetch_assoc($res)) {
            $rows[] = $row;
    
        }
        return $rows;
    }
    

    Ideal usage, going off of how binding works with PDO because I’m not really familiar with the binding method for mysql_fetch_assoc()

    $user_data = query('SELECT User_ID,Name,Email,Etc.. FROM Users ORDER BY Join_Date LIMIT :count', array('count'=>$count));
    

    That has three benefits: simpler binding, not having to do boiler-plate for single or multi-row select statements (an 80%-of-the-time use case), and potentially an easier time if you ever need to switch databases.

    Factory Pattern to Separate Creation of User objects from modification

    As far as the original issue of user creation, how about using the factory pattern to separate the creation of User objects from the methods that User objects can perform on their own data?

    Class UserFactory{
        public static function getNewestUsers($count){
            $sql_statement = "SELECT User_ID,Name,Email,Etc.. FROM Users ORDER BY Join_Date LIMIT :count";
            $users_info = query($sql_statement, array('count'=>$count);
            $users = array();
            foreach($users as $user_info){
                $users[] = new User($user_info);
            }
            return $users;
        }
    }
    

    Use:

    $user_objects = UserFactory::getNewestUsers( 45 );
    

    The user object would then only: use __constructor() on an array of data as Mikushi mentions, and perform alterations on that data (which the factory could potentially have a function saveUser($user_object) to save changes.

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

Sidebar

Related Questions

Greetings On all my controllers I recycle the same code that wraps my models
Greetings all, Is there a built in way to know when a user is
Greetings all, I read 3d grid data (from multiple TIF images) into a structure
Greetings all, I have a code snippet as follows : class AppCtx { private:
Greetings all, I want to run the code block inside the loop ,in seperate
Greetings all! Looking for some help with MVC in a PHP context. Currently I
Greetings all, I have some JSON code that looks like this: { playlist: [
Greetings all. I am writing some code using the Boost Units library and have
Greetings all, I am trying to download 'gz' file using URL class .Code snippet
Greetings all and please forgive me if my question is too cheap. I have

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.