Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>What you want is a new object called user that has all the properties that you want, Like the below.</p> <pre><code>&lt;?php try { $db = new PDO('mysql:host=localhost;dbname=sub_db;charset=UTF-8', DB_USER, DB_PASS); $db-&gt;setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $db-&gt;setAttribute(PDO::ATTR_EMULATE_PREPARES, FALSE); } catch (PDOException $ex) { echo "error"; } class users { public $db; public function __construct($db) { $this-&gt;db=$db; } /** * Fetch all users and return an array of their mapped objects * * @author Daniel Noel-Davies * * @return array */ public function getAllUsers () { $users = array(); // Grab an array of each user $users = $this-&gt;db-&gt;query('SELECT * FROM users')-&gt;fetch(PDO::FETCH_ASSOC); // Loop through the users foreach( $users as $index =&gt; $user ) { // Replace each row with an object of that user (keeps memory down) $users[$index] = new User($user); } // return our array containing an object of each user return $users; } public function count_row(){ $count = $this-&gt;db-&gt;query('SELECT COUNT(*) FROM users')-&gt;fetch(PDO::FETCH_ASSOC); return $count['COUNT(*)']; } public function find_by_id($row) { $record = $this-&gt;db-&gt;query('SELECT * FROM users LIMIT 1 OFFSET ' . $row)-&gt;fetch(PDO::FETCH_ASSOC); return $record; } } class user { public $id; public $username; public $first_name; public $last_name; public function __construct($record){ foreach ($record as $atribute =&gt; $value) { $this-&gt;$atribute = $value; } } } // Initiate our users class $objUsers = new Users; // Fetch all the users $users = $objUsers-&gt;getAllUsers(); // Loop through them all foreach ($users as $user) { // Using Printf keeps things neater, means you're template is tidier. printf( '%s&lt;br /&gt; %s&lt;br /&gt; %s&lt;br /&gt; %s&lt;br /&gt;', $user-&gt;id, $user-&gt;username, $user-&gt;first_name, $user-&gt;last_name ); } ?&gt; </code></pre> <p>Does this help?</p>
    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