smarty template engine
Saturday, October 11, 2008  
download | documentation | faq | forum | mailing lists | changelog | contribs 


search for in the  


Sample Application: Guestbook

/web/www.example.com/smarty/guestbook/libs/sql.lib.php
<?php

/**
 * Project: Guestbook Sample Smarty Application
 * Author: Monte Ohrt <monte [AT] ohrt [DOT] com>
 * Date: March 14th, 2005
 * File: sql.lib.php
 * Version: 1.0
 */

// define the query types
define('SQL_NONE'1);
define('SQL_ALL'2);
define('SQL_INIT'3);

// define the query formats
define('SQL_ASSOC'1);
define('SQL_INDEX'2);

class 
SQL {
    
    var 
$db null;
    var 
$result null;
    var 
$error null;
    var 
$record null;
    
    
/**
     * class constructor
     */
    
function SQL() { }
    
    
/**
     * connect to the database
     *
     * @param string $dsn the data source name
     */
    
function connect($dsn) {
        
$this->db DB::connect($dsn);

        if(
DB::isError($this->db)) {
            
$this->error $this->db->getMessage();
            return 
false;
        }        
        return 
true;
    }
    
    
/**
     * disconnect from the database
     */
    
function disconnect() {
        
$this->db->disconnect();   
    }
    
    
/**
     * query the database
     *
     * @param string $query the SQL query
     * @param string $type the type of query
     * @param string $format the query format
     */
    
function query($query$type SQL_NONE$format SQL_INDEX) {

        
$this->record = array();
        
$_data = array();
        
        
// determine fetch mode (index or associative)
        
$_fetchmode = ($format == SQL_ASSOC) ? DB_FETCHMODE_ASSOC null;
        
        
$this->result $this->db->query($query);
        if (
DB::isError($this->result)) {
            
$this->error $this->result->getMessage();
            return 
false;
        }
        switch (
$type) {
            case 
SQL_ALL:
                
// get all the records
                
while($_row $this->result->fetchRow($_fetchmode)) {
                    
$_data[] = $_row;   
                }
                
$this->result->free();            
                
$this->record $_data;
                break;
            case 
SQL_INIT:
                
// get the first record
                
$this->record $this->result->fetchRow($_fetchmode);
                break;
            case 
SQL_NONE:
            default:
                
// records will be looped over with next()
                
break;   
        }
        return 
true;
    }
    
    
/**
     * connect to the database
     *
     * @param string $format the query format
     */
    
function next($format SQL_INDEX) {
        
// fetch mode (index or associative)
        
$_fetchmode = ($format == SQL_ASSOC) ? DB_FETCHMODE_ASSOC null;
        if (
$this->record $this->result->fetchRow($_fetchmode)) {
            return 
true;
        } else {
            
$this->result->free();
            return 
false;
        }
            
    }
    
}

?>

sql.lib.php is our database wrapper class around PEAR::DB. It will help keep the database access syntax in our application to a minimum. You can just copy and paste the above code, don't worry too much about understanding it unless you feel inclined. Here is a crash course on the usage:

$guestbook->sql->query("select * from GUESTBOOK", SQL_ALL);
print_r($guestbook->sql->record);   
OUTPUT:
Array
(
    [0] => Array
        (
            [0] => 1
            [1] => Monte
            [2] => 2005-03-12 17:23:32
            [3] => test entry 1
        )

    [1] => Array
        (
            [0] => 2
            [1] => Monte
            [2] => 2005-03-12 17:23:33
            [3] => test entry 2
        )

    [2] => Array
        (
            [0] => 3
            [1] => Monte
            [2] => 2005-03-12 17:23:35
            [3] => test entry 3
        )

)

All of the guestbook entries are shown. SQL_ALL will get all of the query records.

$guestbook->sql->query("select * from GUESTBOOK");
while($guestbook->sql->next()) {
    print_r($guestbook->sql->record);
}   
OUTPUT:
Array
(
    [0] => 1
    [1] => Monte
    [2] => 2005-03-12 17:23:32
    [3] => test entry 1
)

Array
(
    [0] => 2
    [1] => Monte
    [2] => 2005-03-12 17:23:33
    [3] => test entry 2
)

Array
(
    [0] => 3
    [1] => Monte
    [2] => 2005-03-12 17:23:35
    [3] => test entry 3
)

This loops over the records one by one. If no second parameter is supplied to query(), then the resulting records are looped over with next().

$guestbook->sql->query("select * from GUESTBOOK", SQL_INIT);
print_r($guestbook->sql->record);
OUTPUT:
Array
(
    [0] => 1
    [1] => Monte
    [2] => 2005-03-12 17:23:32
    [3] => test entry 1
)

This outputs only one record (the first one). SQL_INIT will get one record only.

$guestbook->sql->query("select * from GUESTBOOK", SQL_INIT, SQL_ASSOC);
print_r($guestbook->sql->record);
OUTPUT:
Array
(
    [id] => 1
    [Name] => Monte
    [EntryDate] => 2005-03-12 17:23:32
    [Comment] => test entry 1
)

Passing a third parameter of SQL_ASSOC to query() will return the results as an associative array: fieldname => value.

$guestbook->sql->query("select * from GUESTBOOK");
while($guestbook->sql->next(SQL_ASSOC)) {
    print_r($guestbook->sql->record);
}   
OUTPUT:
Array
(
    [id] => 1
    [Name] => Monte
    [EntryDate] => 2005-03-12 17:23:32
    [Comment] => test entry 1
)

Array
(
    [id] => 2
    [Name] => Monte
    [EntryDate] => 2005-03-12 17:23:33
    [Comment] => test entry 2
)

Array
(
    [id] => 3
    [Name] => Monte
    [EntryDate] => 2005-03-12 17:23:35
    [Comment] => test entry 3
)

Passing SQL_ASSOC as a parameter to next() will also return results as an associative array.


[Page 1] [Page 2] [Page 3] [Page 4] [Page 5]


 

credits 

Smarty Copyright © 2002-2008
New Digital Group, Inc.

All rights reserved.