Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Each function in your class should do <em>one</em> task and <em>one</em> task only and should be named appropriately.</p> <p>Your <code>selectAllUsers</code> function should just select all users, as you have done so,</p> <pre><code>function selectAllUsers() { $q = "SELECT username FROM ".TBL_USERS.""; return mysql_query($q, $this-&gt;connection); } </code></pre> <p>However, I added a <code>return</code> keyword, to return the <em>resource</em> which we can use later on.</p> <p>You could then have a function called <code>generateUserArray</code>, which would populate an array of user's using the <em>returned</em> MySQL resource from the <code>selectAllUsers</code> function.</p> <pre><code>function generateUserArray() { $u = array(); $result = $this-&gt;selectAllUsers(); while( $row=mysql_fetch_assoc($result)) { $u[] = $row['username']; } return $u; } </code></pre> <p>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.</p> <hr> <p>First, here is my test table (<code>users</code>) from my test database (<code>login</code>),</p> <pre><code>mysql&gt; select * from users; +-----------+-----------+ | Name | Password | +-----------+-----------+ | aforloney | whatsup | | pam | qwerty | +-----------+-----------+ 2 rows in set (0.15 sec) </code></pre> <p>And here is my test class in <code>MyClass.php</code></p> <pre><code>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-&gt;selectAllValues(); while ($row = mysql_fetch_assoc($result)) { echo $row['Name'] . " and " . $row['Password'] . "&lt;br/&gt;"; } } } </code></pre> <p>Which you could then invoke from a <code>Main</code> class like so,</p> <pre><code>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-&gt;connect(); $myClass-&gt;selectAllValues(); $myClass-&gt;printAllValues(); } } </code></pre> <p>And then called,</p> <pre><code>$m = new Main(); // will invoke the __construct function </code></pre> <p>Which will output to the browser,</p> <pre><code> aforloney and whatsup pam and qwerty </code></pre> <hr> <p>As a side note, just to avoid any other confusion, my <code>MyClass</code> and <code>Main</code> example were two classes inside the <em>same</em> PHP file. If you were to use the above code examples and try to create a <code>MyClass</code> object, it would throw an error.</p> <p>If you had wanted to instantiate an object that is defined in <em>another</em> class, you would need an <a href="http://php.net/manual/en/function.include.php" rel="nofollow noreferrer"><code>include</code></a> statement.</p> <p>So with two different PHP files, I would change my code for the <code>Main</code> class like so,</p> <pre><code>&lt;?php include ('MyClass.php'); class Main { ... } ?&gt; </code></pre>
    singulars
    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.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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