Note that there are some explanatory texts on larger screens.

plurals
  1. POSQL query efficiency when using PHP classes
    primarykey
    data
    text
    <p>I've been programming in PHP for many years, however only recently started programming with classes. I have the following - basic - user class as follows:</p> <pre><code>&lt;?php /* The class for constructing any user's information */ class User { protected $userId, $email, $userGroup; protected function getEmail() { return $this-&gt;email; } protected function getUserId() { return $this-&gt;userId; } protected function getUserGroup() { return $this-&gt;userId; } public function __construct($userId='') { if($userId) { $select = mysql_query("SELECT userId, email, user_group FROM user WHERE userId = '$userId'"); while($user==mysql_fetch_array($select)) { $this-&gt;email = $user[email]; $this-&gt;userId = $userId; $this-&gt;userGroup = $user[user_group]; } } } }?&gt; </code></pre> <p>So I understand I can do the following</p> <pre><code>&lt;?php $user = new User($userId); echo $user-&gt;getEmail(); ?&gt; </code></pre> <p>To display the user's email address for a given userId. What I'd like to know is, what would be the best way - using OOP - to display, say, 40 user's emails. Obviously creating 40 user objects would be silly as that's 40 SQL queries. Would you simply make a "users" class that was used for returning an array of multiple users, after doing an SQL given various parameters?</p> <p>Ie</p> <pre><code>&lt;?php $getUsers = new Users('Tom'); // create 'Users' object where name is 'Tom' print_r($users); // prints the returned array of users? ?&gt; </code></pre> <p>Thanks for your help. Hopefully this is clear.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload