Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here's what I've used (just replace the references to Zzz_Config with $GLOBALS['db_conf'] or something):</p> <pre><code>/** * Extended PDO with databse connection (instance) storage by name. */ class Zzz_Db extends PDO { /** * Named connection instances. * * @var array */ static private $_instances; /** * Retrieves (or instantiates) a connection by name. * * @param string $name Connection name (config item key). * @return Zzz_Db Named connection. */ static public function getInstance($name = null) { $name = $name === null ? 'db' : "db.$name"; if (!isset(self::$_instances[$name])) { if (!$config = Zzz_Config::get($name)) { throw new RuntimeException("No such database config item: $name"); } if (!isset($config['dsn'])) { if (!isset($config['database'])) { throw new RuntimeException('Invalid db config'); } $config['dsn'] = sprintf('%s:host=%s;dbname=%s', isset($config['adapter']) ? $config['adapter'] : 'mysql', isset($config['host']) ? $config['host'] : 'localhost', $config['database']); } $db = self::$_instances[$name] = new self( $config['dsn'], isset($config['username']) ? $config['username'] : null, isset($config['password']) ? $config['password'] : null); $db-&gt;setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); //$db-&gt;setAttribute(PDO::ATTR_STATEMENT_CLASS, 'Zzz_Db_Statement'); if ($db-&gt;getAttribute(PDO::ATTR_DRIVER_NAME) == 'mysql') { $db-&gt;setAttribute(PDO::ATTR_EMULATE_PREPARES, true); $db-&gt;exec('SET CHARACTER SET utf8'); } } return self::$_instances[$name]; } } </code></pre> <p>Usage whould be:</p> <pre><code>$db = Zzz_Db::getInstance(); // or Zzz_Db::getInstance('some_named_db') $stmt = $db-&gt;prepare('SELECT ... </code></pre> <p>The goal is to keep the db configuration in an *.ini file (editable by a non-coder).</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    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. VO
      singulars
      1. This table or related slice is empty.
    2. 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