Each function in your class should do one task and one task only and should be named appropriately.
Your selectAllUsers function should just select all users, as you have done so,
function selectAllUsers() {
$q = "SELECT username FROM ".TBL_USERS."";
return mysql_query($q, $this->connection);
}
However, I added a return keyword, to return the resource which we can use later on.
You could then have a function called generateUserArray, which would populate an array of user's using the returned MySQL resource from the selectAllUsers function.
function generateUserArray() {
$u = array();
$result = $this->selectAllUsers();
while( $row=mysql_fetch_assoc($result)) {
$u[] = $row['username'];
}
return $u;
}
With more information regarding your question, the more answers you will receive in regards to what your trying to accomplish, or clear up any confusion.
First, here is my test table (users) from my test database (login),
mysql> select * from users;
+-----------+-----------+
| Name | Password |
+-----------+-----------+
| aforloney | whatsup |
| pam | qwerty |
+-----------+-----------+
2 rows in set (0.15 sec)
And here is my test class in MyClass.php
class MyClass {
function connect() {
mysql_connect("localhost","root","password");
mysql_select_db("login");
}
function selectAllValues() {
$q = "SELECT * FROM users";
return mysql_query($q);
}
function printAllValues() {
$result = $this->selectAllValues();
while ($row = mysql_fetch_assoc($result)) {
echo $row['Name'] . " and " . $row['Password'] . "<br/>";
}
}
}
Which you could then invoke from a Main class like so,
class Main {
// other variables...
// __construct is an object's constructor, it's what gets called when
// you create a new Main object
function __construct() {
$myClass = new MyClass();
$myClass->connect();
$myClass->selectAllValues();
$myClass->printAllValues();
}
}
And then called,
$m = new Main(); // will invoke the __construct function
Which will output to the browser,
aforloney and whatsup
pam and qwerty
As a side note, just to avoid any other confusion, my MyClass and Main example were two classes inside the same PHP file. If you were to use the above code examples and try to create a MyClass object, it would throw an error.
If you had wanted to instantiate an object that is defined in another class, you would need an include statement.
So with two different PHP files, I would change my code for the Main class like so,
<?php
include ('MyClass.php');
class Main {
...
}
?>