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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T02:14:25+00:00 2026-06-15T02:14:25+00:00

If I want to get a customer’s email address from a database, the following

  • 0

If I want to get a customer’s email address from a database, the following is bad, because $row will contain an element for every single field in the customer table:

$result = mysqli_query ('SELECT * FROM customer WHERE custid = 1');
$row = mysqli_fetch_array ($result, MYSQLI_ASSOC);

This is considered better, because $row will contain a single element for the email address, which is all we need:

$result = mysqli_query ('SELECT email FROM customer WHERE custid = 1');
$row = mysqli_fetch_array ($result, MYSQLI_ASSOC);

I am trying to apply the same logic to objects. If a “customer” object has say 50 properties/fields, is it common to populate all 50 properties when you only need to access one?

Below is a basic example of a customer class and object which is resource hungry if all we want to do is get a customer’s email address, and not all their other details (in this example a “customer” object has 5 properties but in reality there could be many more):

class Customer {

    private $custid;
    private $firstname;
    private $lastname;
    private $email;
    private $dateadded;

    public function getCustomer ($id) {
        $result = mysql_queryi ('SELECT * FROM customer WHERE custid = ' . $id);
        $row = mysqli_fetch_array ($result, MYSQLI_ASSOC);
        foreach ($row as $key => $value) {
            if (isset ($this->$key)) {
                $this->$key = $value;
            }
        }
    }
}

$customer = new Customer ();
$customer->getCustomer (1); // All I wanted is the email address, but now I have everything :-(
  • 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-15T02:14:26+00:00Added an answer on June 15, 2026 at 2:14 am

    You could make a second argument to your getCustomer function that accepts an array of fields to query

    public function getCustomer ($id, $fields=array('*')) {
        $fields = implode(',', $fields);
        $result = mysql_queryi("SELECT {$fields} FROM customer WHERE ...");
        // ...
    

    Usage

    // get entire customer
    $customer->getCustomer(1);
    
    // just get the email field
    $customer->getCustomer(1, array('email'));
    
    // get full name
    $customer->getCustomer(1, array('firstname', 'lastname'));
    

    Bonus:

    I’d recommend storing your fields a little differently in your model

    class Customer
        private $_data;
    
        public function getCustomer($id, $fields=array('*')) {
            // ...
        }
    
        // PHP magic set method
        // calling $this->foo = 'bar' results in $this->_data['foo'] = 'bar'
        public function __set($key, $value) {
            return $this->_data[$key] = $value;
        }
    
        // PHP magic get method
        // calling $this->foo results in $this->_data['foo']
        public function __get($key) {
            return array_key_exists($key, $this->_data)
                ? $this->_data[$key]
                : null
            ;
        }
    }
    

    This way you can get rid of all the statically defined instance variables; e.g., private $custid;, private $firstname;, private ...

    Instead, all fields will be stored in $this->_data private associative array.

    What’s sweet about this is you don’t even have to change the way your getCustomer function is written.

    Learn more about PHP magic methods

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

Sidebar

Related Questions

I'm trying to select and get values from customer objects. What I want to
I want to get rows for all customer details from magento tables. Can anyone
I want to get the phone number from a customer but I want it
Basically, I want to get all rows from the customers table that do NOT
I want get as much as possible from Redis + Hiredis + libevent. I'm
What I want: get a xml from the AppData to use What I code
Basically I want get data I already have accessed from javascript and passing it
I want to get list of tables in a given database and a given
I want to get 100 and example from this string ?connect:100/username:example/ I searched in
In one of my current applications, I need to get customer data from a

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.