<?php
 
ini_set( 'display_errors', 1 );
 
error_reporting( 1 );
 
 
$params = array(
 
    'host'           => 'localhost',
 
    'username'       => 'postgres',
 
    'password'       => 'root',
 
    'dbname'         => 'test'
 
);
 
 
require_once 'Zend/Loader.php';
 
require_once 'Zend/Db.php';
 
 
$db = Zend_Db::factory('Pgsql', $params);
 
 
try {
 
    $db->getConnection();
 
} catch (Zend_Db_Adapter_Exception $e) {
 
    // perhaps a failed login credential, or perhaps the RDBMS is not running
 
} catch (Zend_Exception $e) {
 
    // perhaps factory() failed to load the specified Adapter class
 
}
 
 
$tables = $db->listTables();
 
print_r( $tables );
 
 
 
try {
 
    $db->beginTransaction();
 
 
    $r = $db->insert( 'test', array( 'name' => 'Apple' ) ); 
 
 
    echo $db->lastInsertId() . '<br/>';
 
    $db->commit();
 
    //$db->rollBack();
 
} catch ( Zend_Exception $e ) {
 
    $db->rollBack();
 
    $tracearr = $e->getTrace();
 
    foreach( $tracearr as $trace ) {
 
        $stack = array_pop( $trace[ 'args' ] );
 
        if ( is_array( $stack ) ) {
 
            $stack = implode( ' ', $stack );
 
        }
 
        $traceerror .= $trace[ 'line' ] . " line # in " . $trace[ 'file' ] . " reason: " . $stack . "<br/>\n";
 
    }
 
    echo $traceerror;
 
}
 
 
 
$count = $db->fetchOne( "SELECT COUNT(*) FROM test" );
 
 
var_dump( $count ); echo '<br/>';
 
 
$res = $db->fetchAll( "SELECT * FROM test" ); 
 
print_r( $res ); 
 
 
$res2 = $db->fetchAll( $db->select()->from( 'test' ) );
 
 
print_r( $res2 );
 
 
 
$stmt = $db->query( $db->select()->from('test') );
 
 
while ( $row = $stmt->fetch() ) {
 
    print_r( $row );
 
}
 
 
 
$stmt = $db->prepare( $db->select()->from( 'test' ) );
 
 
$stmt->execute();
 
 
$count = $stmt->rowCount(); 
 
 
 
print_r( $db->describeTable( 'test' ) );
 
 
 
$stmt = $db->query( $db->select()->from('test')->where( "name=?", 'Apple' ) );
 
 
while ( $row = $stmt->fetch() ) {
 
    print_r( $row );
 
}
 
 
 
$db->insert( 'only_oid_table', array( 'one' => 1, 'two' => 2 ) );
 
echo $db->lastInsertId();
 
 
?>
 
 |