/** * Demonstrates the features of the Database library. * * Table Structure: * CREATE TABLE `pages` ( * `id` mediumint( 9 ) NOT NULL AUTO_INCREMENT , * `page_name` varchar( 100 ) NOT NULL , * `title` varchar( 255 ) NOT NULL , * `content` longtext NOT NULL , * `menu` tinyint( 1 ) NOT NULL default '0', * `filename` varchar( 255 ) NOT NULL , * `order` mediumint( 9 ) NOT NULL , * `date` int( 11 ) NOT NULL , * `child_of` mediumint( 9 ) NOT NULL default '0', * PRIMARY KEY ( `id` ) , * UNIQUE KEY `filename` ( `filename` ) * ) ENGINE = MYISAM DEFAULT CHARSET = utf8 PACK_KEYS =0; * */ function database() { $db = new Database(); $table = 'pages'; echo 'Does the ' . $table . ' table exist? '; if ($db->table_exists($table)) { echo '<p>YES! Lets do some work =)</p>'; $query = $db->select('DISTINCT pages.*')->from($table)->get(); echo $db->last_query(); echo '<h3>Iterate through the result:</h3>'; foreach ($query as $item) { echo '<p>' . $item->title . '</p>'; } echo '<h3>Numrows using count(): ' . count($query) . '</h3>'; echo 'Table Listing:<pre>' . print_r($db->list_tables(), TRUE) . '</pre>'; echo '<h3>Try Query Binding with objects:</h3>'; $sql = 'SELECT * FROM ' . $table . ' WHERE id = ?'; $query = $db->query($sql, array(1)); echo '<p>' . $db->last_query() . '</p>'; $query->result(TRUE); foreach ($query as $item) { echo '<pre>' . print_r($item, true) . '</pre>'; } echo '<h3>Try Query Binding with arrays (returns both associative and numeric because I pass MYSQL_BOTH to result():</h3>'; $sql = 'SELECT * FROM ' . $table . ' WHERE id = ?'; $query = $db->query($sql, array(1)); echo '<p>' . $db->last_query() . '</p>'; $query->result(FALSE, MYSQL_BOTH); foreach ($query as $item) { echo '<pre>' . print_r($item, true) . '</pre>'; } echo '<h3>Look, we can also manually advance the result pointer!</h3>'; $query = $db->select('title')->from($table)->get(); echo 'First:<pre>' . print_r($query->current(), true) . '</pre><br />'; $query->next(); echo 'Second:<pre>' . print_r($query->current(), true) . '</pre><br />'; $query->next(); echo 'Third:<pre>' . print_r($query->current(), true) . '</pre>'; echo '<h3>And we can reset it to the beginning:</h3>'; $query->rewind(); echo 'Rewound:<pre>' . print_r($query->current(), true) . '</pre>'; echo '<p>Number of rows using count_records(): ' . $db->count_records('pages') . '</p>'; } else { echo 'NO! The ' . $table . ' table doesn\'t exist, so we can\'t continue =( '; } echo "<br/><br/>\n"; echo 'done in {execution_time} seconds'; }