So I am trying to understand OOP more and use it.
The following code was written before i started using OOP.
//loop through all the users
$game = "$_POST[Game]_teams";
$result = mysql_query("SELECT username FROM `users`") or die(mysql_error());
while( $row=mysql_fetch_assoc($result) )
{
$u[] = $row['username'];
}
I have put the query into my database page like following:
function selectAllUsers()
{
$q = "SELECT username FROM ".TBL_USERS."";
mysql_query($q, $this->connection);
}
I’m a little confused about how the rest could be different?
Would it be? Is it possible for anyone to help me without more code or understanding of my structure?
Each function in your class should do one task and one task only and should be named appropriately.
Your
selectAllUsersfunction should just select all users, as you have done so,However, I added a
returnkeyword, 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 theselectAllUsersfunction.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),And here is my test class in
MyClass.phpWhich you could then invoke from a
Mainclass like so,And then called,
Which will output to the browser,
As a side note, just to avoid any other confusion, my
MyClassandMainexample were two classes inside the same PHP file. If you were to use the above code examples and try to create aMyClassobject, it would throw an error.If you had wanted to instantiate an object that is defined in another class, you would need an
includestatement.So with two different PHP files, I would change my code for the
Mainclass like so,