Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Here is a mostly complete working cut &amp; paste example of <a href="https://stackoverflow.com/users/246248/guillaume-boschini">Guillaume Boschini</a>'s answer above.</p> <p>A populated DB table (MySQL):</p> <pre><code>CREATE TABLE `useraddress` ( `addressid` int(10) unsigned NOT NULL AUTO_INCREMENT, `userid` int(10) unsigned NOT NULL, `addresstitle` char(100) NOT NULL, `streetaddressa` char(100) NOT NULL, `streetaddressb` char(100) DEFAULT NULL, `unit` char(50) DEFAULT NULL, `city` char(50) NOT NULL, `state` char(2) NOT NULL, `zip` int(5) NOT NULL, `zipplusfour` int(4) DEFAULT NULL, PRIMARY KEY (`addressid`), KEY `userid` (`userid`), CONSTRAINT `useraddress_fk_1` FOREIGN KEY (`userid`) REFERENCES `user` (`userid`) ) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; </code></pre> <p>In /DBLibrary/pdocore.php:</p> <pre><code>&lt;?php Config::write('db.host', 'localhost'); Config::write('db.port', '3306'); Config::write('db.basename', 'DBName'); Config::write('db.user', 'DBUser'); Config::write('db.password', 'DBPassword'); class Config { static $confArray; public static function read($name) { return self::$confArray[$name]; } public static function write($name, $value) { self::$confArray[$name] = $value; } } class Core { public $dbh; // handle of the db connection private static $instance; private function __construct() { // building data source name from config $dsn = 'mysql:host=' . Config::read('db.host') . ';dbname=' . Config::read('db.basename') . ';port=' . Config::read('db.port') .';connect_timeout=15'; // getting DB user from config $user = Config::read('db.user'); // getting DB password from config $password = Config::read('db.password'); $this-&gt;dbh = new PDO($dsn, $user, $password); $this-&gt;dbh-&gt;setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); } public static function getInstance() { if (!isset(self::$instance)) { $object = __CLASS__; self::$instance = new $object; } return self::$instance; } // others global functions } ?&gt; </code></pre> <p>In /objectsLibrary/SYS_UserAddress.php:</p> <pre><code>&lt;?php define('k_uaddress_addressid','addressid'); define('k_uaddress_userid','userid'); define('k_uaddress_addresstitle','addresstitle'); define('k_uaddress_addressa','streetaddressa'); define('k_uaddress_addressb','streetaddressb'); define('k_uaddress_unit','unit'); define('k_uaddress_city','city'); define('k_uaddress_state','state'); define('k_uaddress_zip','zip'); define('k_uaddress_zipplusfour','zipplusfour'); require_once '../DBLibrary/pdocore.php'; class SYS_UserAddress { public $addressid; public $userid; public $addresstitle; public $addressa; public $addressb; public $unit; public $city; public $state; public $zip; public $zipplusfour; public function SYS_UserAddressByAddressId($_addressid) { $returnValue=FALSE; $query='select * from useraddress where ' . k_uaddress_addressid . '=:addressid'; try { $pdoCore = Core::getInstance(); $pdoObject = $pdoCore-&gt;dbh-&gt;prepare($query); $queryArray = array(':addressid'=&gt;$_addressid); if ($pdoObject-&gt;execute($queryArray)) { $pdoObject-&gt;setFetchMode(PDO::FETCH_ASSOC);; while ($addressrow = $pdoObject-&gt;fetch()) { $this-&gt;addressid=$addressrow[k_uaddress_addressid]; $this-&gt;userid=$addressrow[k_uaddress_userid]; $this-&gt;addresstitle=$addressrow[k_uaddress_addresstitle]; $this-&gt;addressa=$addressrow[k_uaddress_addressa]; $this-&gt;addressb=$addressrow[k_uaddress_addressb]; $this-&gt;unit=$addressrow[k_uaddress_unit]; $this-&gt;city=$addressrow[k_uaddress_city]; $this-&gt;zip=$addressrow[k_uaddress_zip]; $this-&gt;zipplusfour=$addressrow[k_uaddress_zipplusfour]; } $returnValue=TRUE; } } catch(PDOException $pe) { trigger_error('Could not connect to MySQL database. ' . $pe-&gt;getMessage() , E_USER_ERROR); } return $returnValue; } } $test=1; $testAddressId=2; if($test&gt;0) { $testAddress = new SYS_UserAddress(); $testAddress-&gt;SYS_UserAddressByAddressId($testAddressId); echo '&lt;pre&gt;'; echo print_r($testAddress); echo '&lt;/pre&gt;'; } ?&gt; </code></pre> <p>The post above really helped me. This post I am making now would have gotten me to where I wanted to be faster. That is all. If anything isn't right, I'll be around to fix it.</p>
 

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