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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T14:57:50+00:00 2026-05-14T14:57:50+00:00

So I am trying to understand OOP more and use it. The following code

  • 0

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?

  • 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-14T14:57:50+00:00Added an answer on May 14, 2026 at 2:57 pm

    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 {
     ...
    }
    ?>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Trying to understand an fft (Fast Fourier Transform) routine I'm using (stealing)(recycling) Input is
After trying to understand why client code is not rendered in a page (injected
I'm trying to understand someone else's Perl code without knowing much Perl myself. I
I was trying to understand something with pointers, so I wrote this code: #include
I'm trying to understand a particular Perl code from vcake . Usually I find
I've been trying to understand how Ruby blocks work, and to do that I've
I've been trying to understand Process.MainWindowHandle . According to MSDN; The main window is
I am trying to understand some assembly. The assembly as follows, I am interested
I'm trying to understand the concepts behind DDD, but I find it hard to
I'm trying to understand how you are supposed to add page numbers to the

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.