'pgsql:' ); //MySql-konfiguraatio. Muista vaihtaa socketin osoitteeseen oma käyttäjänimesi, sekä tietokannan nimi ja tunnuksesi $config = array( 'dburl' => 'mysql:unix_socket=/home/FIXME/mysql/socket;dbname=FIXME', 'dbusername' => 'root', 'dbpassword' => 'FIXME', ); /***************************************************************/ /* Koodia. Tästä tiedostosta ei kannata ottaa mallia mihinkään */ /***************************************************************/ function getDatabase() { global $config; static $db = null; if ($db === null) { if (isset($config['dbusername'])) { $db = new PDO($config['dburl'], $config['dbusername'], $config['dbpassword']); } else { $db = new PDO($config['dburl']); } $db->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION); } return $db; } function getObjects() { $args = func_get_args(); $sql = array_shift($args); $query = getDatabase()->prepare($sql); $query->execute($args); return $query->fetchAll(PDO::FETCH_OBJ); } function getValue() { $args = func_get_args(); $sql = array_shift($args); $query = getDatabase()->prepare($sql); $query->execute($args); return $query->fetchColumn(); } class TableLister { protected $name; public function __construct($name) {$this->name = $name;} public function getName() { return $this->name; } public function quoteName() { return static::TABLE_QUOTE.$this->name.static::TABLE_QUOTE; } public function getRowCount() { return getValue('SELECT count(*) FROM '.$this->quoteName()); } public function getRows($limit = 100) { return getObjects("SELECT * FROM ".$this->quoteName()." LIMIT ".(int)$limit); } } class MySqlTableLister extends TableLister { const TABLE_QUOTE = '`'; public static function getTables() { $objs = getObjects("SHOW TABLES"); $ret = array(); foreach($objs as $obj) { foreach($obj as $v) { $ret[] = new self($v); break; } } return $ret; } public function getColumns() { return getObjects("DESCRIBE ".$this->quoteName()); } } class PgSqlTableLister extends TableLister { const TABLE_QUOTE = '"'; public static function getTables() { $objs = getObjects("select tablename from pg_tables where tableowner != 'postgres' order by tablename"); $ret = array(); foreach($objs as $obj) { $ret[] = new self($obj->tablename); } return $ret; } public function getColumns() { $sql = << 0 ORDER BY f.attnum; SQL; return getObjects($sql, $this->name); } } function prettyprint($data) { if (count($data) == 0) { echo '

no data

'; return; } echo ''; echo '',implode('', array_map(function($t) {return "";}, array_keys((array)array_shift(array_values($data))))),''; foreach($data as $datum) { echo '',implode('', array_map(function($t) {return "";}, (array)$datum)),''; } echo '
$t
$t
'; } try { list($dbtype, $_) = explode(':', $config['dburl'], 2); switch($dbtype) { case 'mysql': $tables = MySqlTableLister::getTables(); break; case 'pgsql': $tables = PgSqlTableLister::getTables(); break; default: die("Unknown database"); } ?> Tietokantayhteystesti

Projektin tietokantataulut

getName(); ?>

getColumns()); ?>
getRows()); ?>
', $e->getMessage(); }