Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here is a quick example that might help you with your problem. Let's say I wanted to fetch an array of all my <strong><em>"Customer"</em></strong> data from my <strong><em>"customers"</em></strong> table, How would I successfully complete this task?</p> <p>For this example I will be using two files:</p> <ul> <li><strong>db.class.php:</strong> This class will hold all of the database functions.</li> <li><strong>init.php:</strong> This file will require the database class to fetch all of the customer rows.</li> </ul> <p><strong>db.class.php</strong> </p> <pre><code>&lt;?php class DB { private $host; private $user; private $pw; private $db; public function __construct($db_config) { $this-&gt;host = $db_config['host']; $this-&gt;user = $db_config['user']; $this-&gt;pw = $db_config['pw']; $this-&gt;db = $db_config['db']; } public function open() { /* establish link */ $link = new mysqli($this-&gt;host, $this-&gt;user, $this-&gt;pw, $this-&gt;db); if (!$link) { return false; } $link-&gt;autocommit(TRUE); return $link; } public function getRows($table) { /* establish database connection */ $db = self::open(); /* execute query */ $result = $db-&gt;query("SELECT * FROM $table"); /* fetch rows */ while ($row = $result-&gt;fetch_array()) { $rows[] = $row; } return $rows; } } </code></pre> <p><strong>init.php</strong></p> <pre><code>&lt;?php require 'db.class.php'; /* database config */ $db_config = array( 'host' =&gt; '', 'user' =&gt; '', 'pw' =&gt; '', 'db' =&gt; '' ); /* input table */ $table = 'cust'; $db = new DB($db_config); $rows = $db-&gt;getRows($table); foreach ($rows as $key =&gt; $value) { echo 'Row: '.$key." : ".$value."&lt;br /&gt;"; } ?&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. This table or related slice is empty.
    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