Top  Branding  Banner 
blank.gif
blank.gif
triangle.gif Du er her: /  Forsiden  /  Kildekoden  /  Db  /  Database   Login nu   Login
blank.gif
««« Se kilde koden
blank.gif
tl.gif Cms tr.gif tl.gif Component tr.gif tls.gif     Db  trs.gif tl.gif Db-basket tr.gif tl.gif Db-login tr.gif tl.gif Db-customer tr.gif tl.gif Db-select tr.gif tl.gif Jquery tr.gif tl.gif Form-elements tr.gif tl.gif Menu-fisheye tr.gif tl.gif Template tr.gif tl.gif Tree-node tr.gif tl.gif Validator tr.gif
blank.gif
blank.gif
arrow-headline.gif Index
MenuLink  MenuLeft  
Tilbage

Skjul: Navn

Database.php


Vis: Sample code, tutorial

Database, Sample code, tutorial

Sådan benyttes komponenten Database klassen

Først skal du inkludere den fil der beskriver komponenten, som en klasse fil

  • <?
    require_once(HTML_PACKAGE_PATH.'/Database.php');
    ?>

Dernæst kan du enten benytte komponenten som et taglib (statiske metoder):

  • <?
    Database
    ::display($param1$param2$param3, ...);
    ?>

eller du kan lave en instance af komponenten og benytte metoderne direkte:

  • <?
    $object 
    = new Database($param1$param2$param3, ...);
    print 
    $object->getHtml();
    ?>

Skjul: Sådan vises komponenten

Database, Sådan vises komponenten

Sådan vises komponenten Database klassen

Get the html for the Database object

Vis: PHP source code

Database, PHP source code

Den fulde PHP kildekode for Database klassen

<?php
/**
 * @package db
 * @see HTML_DB_DATABASE_PATH.'/Database.php'
 * @copyright (c) http://Finn-Rasmussen.com
 * @license http://Finn-Rasmussen.com/license/ myPHP License conditions
 * @author http://Finn-Rasmussen.com
 * @version 1.11
 * @since 27-nov-2009
 */

/**
 * The required files
 */
require_once(HTML_DB_DATABASE_PATH.'/DatabaseCommon.php');
if (
defined('HTML_LOG_UTIL_PATH')) {
    require_once(
HTML_LOG_UTIL_PATH.'/Log.php');
}

/**
 * The interface towards the mysql database
 * The interface use an advanced sql caching methode
 * <code>
 * Usage:
 *   $myDb = new Database();
 *   $myDb->open();       // Open a connection to the database
 *         :
 *   $myDb->doSomeThing() // Use the connection object to operate on table data
 *         :
 *   $myDb->freeResult(); // Free the resources
 *   $myDb->close();      // Close the connection
 * </code>
 * @package db
 */

class Database extends DatabaseCommon {
    
/**
     * @var int $affectedRows The Number of rows affected by the drop, insert, update etc.
     */
    
protected $affectedRows 0;

    
/**
     * @var int $numRows Number of rows returned from a select,explain, etc. query
     */
    
protected $numRows 0;

    
/**
     * @var int $row The row returned
     */
    
protected $row NULL

    
/**
     * @var String $sql The query
     */
    
protected $sql '';

    
/**
     * @var String $error The error message for the query
     */
    
protected $error '';

    
/**
     * Constructor
     */
    
function __construct() {
        
parent::__construct(CLASS_NAME_CONNECTION);
    }

    
/**
     * Execute the specified query. SELECT, UPDATE, INSERT etc...
     * Return false or a resource identifier (for SELECT,SHOW,EXPLAIN or DESCRIBE, ...)
     * @param String $sql The sql query to execute
     * @return boolean Returns TRUE (INSERT, UPDATE, DELETE, ...)
     */
    
function query($sql) {
        
$rc false;
        if (
$this->connection!=NULL) {
            
$con $this->connection->getConnection();
            if (
$con == NULL) {
                
$msg $this->getClassName().'->query(), The connection is NULL. this->connection->getConnection()';
                if (
defined('HTML_LOG_UTIL_PATH')) {
                    
Log::error($msg__FILE____LINE__);
                } else {
                    if (
defined('DEBUG_LEVEL_SHOW_SQL') && DEBUG_LEVEL DEBUG_LEVEL_SHOW_SQL) {
                        
Message::add($msg__FILE____LINE__);
                    }
                }
                
$this->connection->close();
                return 
$rc;
            }
            
$this->result mysql_query($sql$con);
            
$rc $this->result != FALSE;
            if (
$rc) {
                
/**
                 * SELECT, SHOW, DESCRIBE eller EXPLAIN returns a resource
                 * UPDATE, DELETE, DROP etc. return true on success
                 */
                
if ($this->result !== true) {
                    
$this->numRows mysql_num_rows($this->result);
                } else {
                    
$this->affectedRows mysql_affected_rows($con);
                }
                
$msg $this->getClassName()."->query(), OK, sql=".$sql." affectedRows=".$this->affectedRows;
                if (
defined('HTML_LOG_UTIL_PATH')) {
                    
Log::query($msg__FILE____LINE__);
                } else {
                    if (
defined('DEBUG_LEVEL_SHOW_SQL') && DEBUG_LEVEL DEBUG_LEVEL_SHOW_SQL) {
                        
Message::add($msg__FILE____LINE__);
                    }
                }
            } else {
                
$this->error $this->getError();
                
$msg $this->getClassName().'->query(), FAILED, sql='.$sql;
                if (
defined('HTML_LOG_UTIL_PATH')) {
                    
Log::query($msg__FILE____LINE__);
                } else {
                    if (
defined('DEBUG_LEVEL_SHOW_SQL') && DEBUG_LEVEL DEBUG_LEVEL_SHOW_SQL) {
                        
Message::add($msg__FILE____LINE__);
                    }
                }
                if (
defined('DEBUG_LEVEL_SHOW_SQL') && DEBUG_LEVEL DEBUG_LEVEL_SHOW_SQL) {
                    
Message::add('SQL:<pre>'.$msg__FILE____LINE__);
                }
            }
        } else {
            
$msg $this->getClassName().'->query(), FAILED, this->connection is NULL';
            if (
defined('HTML_LOG_UTIL_PATH')) {
                
Log::error($msg__FILE____LINE__);
            } else {
                if (
defined('DEBUG_LEVEL_SHOW_SQL') && DEBUG_LEVEL DEBUG_LEVEL_SHOW_SQL) {
                    
Message::add($msg__FILE____LINE__);
                }
            }
        }
        return 
$rc;
    }

    
/**
     * Return the ID generated for the INSERT statement for an AUTO_INCREMENT type
     * @return int Return the ID generated by the INSERT query
     */
    
function getInsertId() {
        
$con $this->connection->getConnection();
        if (
$con==NULL) {
            
$msg $this->getClassName().'->getInsertId(), The connection is NULL. this->connection->getConnection()';
            if (
defined('HTML_LOG_UTIL_PATH')) {
                
Log::error($msg__FILE____LINE__);
            } else {
                if (
defined('DEBUG_LEVEL_SHOW_SQL') && DEBUG_LEVEL DEBUG_LEVEL_SHOW_SQL) {
                    
Message::add($msg__FILE____LINE__);
                }
            }
            
$this->connection->close();
            return -
1;
        }
        return 
mysql_insert_id($con);
    }

    
/**
     * Get the affected rows
     * @return int The number of rows affected by the query
     */
    
function getAffectedRows() {
        return 
$this->affectedRows;
    }

    
/**
     * Get the number of rows
     * @return int The number of rows returned by the query
     */
    
function getNumRows() {
        return 
$this->numRows;
    }

    
/**
     * Fetch one row of data from the result associated with the specified result identifier
     * @param  object $result The result identifier or NULL
     * @return object Returns an array that corresponds to the fetched row, or FALSE if there are no more rows
     */
    
function fetchRow($result='') {
        
$this->row mysql_fetch_row($result != ''?$result:$this->result);
        return 
$this->row;
    }

    
/**
     * Fetch the result of the query
     * @param  object $result_type The optional argument result_type is a constant and 
     * can take the following values: MYSQL_ASSOC, MYSQL_NUM, and MYSQL_BOTH. 
     * @return object Returns the contents of the resultset, returns FALSE when all have been returned
     */
    
function fetchObject($result_type=MYSQL_ASSOC) {
        return 
mysql_fetch_object($this->result$result_type);
    }

    
/**
     * Fetch the result of the query
     * @param int $result_type The optional argument result_type is a constant and 
     * can take the following values: MYSQL_ASSOC, MYSQL_NUM, and MYSQL_BOTH. 
     * @return array Returns the contents of the resultset, returns FALSE when all have been returned
     */
    
function fetchArray($result_type=MYSQL_BOTH) {
        return 
mysql_fetch_array($this->result$result_type);
    }

    
/**
     * Fetch the result of the query
     * @return object Returns the contents of the resultset, returns FALSE when all have been returned
     */
    
function fetchAssoc() {
        return 
mysql_fetch_assoc($this->result);
    }

    
/**
     * Returns the number of fields in the result   
     * @return int The number of fields in the result 
     */
    
function getNumFields() {
        return 
mysql_num_fields($this->result); 
    }
    
    
/**
     * Get the name of the specified field in a result  
     * @param  int $offset The field offset to fetch
     * @return String Returns the name of the specified field index
     */
    
function getFieldName($offset) {
        
$rc mysql_field_name($this->result$offset);
        return 
$rc===false?'':$rc
    }

    
/**
     * Get the length of the specified field 
     * @param  int $offset The field offset to fetch
     * @return int Returns the length of the specified field
     */
    
function getFieldLen($offset) {
        
$rc mysql_field_len($this->result$offset);
        return 
$rc===false?'':$rc
    }

    
/**
     * Get the type of the specified field in a result   
     * @param  int $offset The field offset to fetch
     * @return String Returns the field type and will be one of 
     * "int", "real", "string", "blob", ...
     */
    
function getFieldType($offset) {
        
$rc mysql_field_type($this->result$offset);
        return 
$rc===false?'':$rc
    }

    
/**
     * Get the flags associated with the specified field in a result 
     * The flags are reported as an array of words per flag separated by a single space, 
     * @param  int $offset The field offset to fetch
     * @return array Returns an array of the field flags or FALSE on failure
     */
    
function getFieldFlags($offset) {
        
$rc mysql_field_flags($this->result$offset);
        return 
$rc===false?'':explode(' '$rc); 
    }

    
/**
     * Get name of the table the specified field is in  
     * @param  String $offset The field offset to fetch
     * @return String Returns the name of the table that the specifed field is in
     */
    
function getFieldTable($offset) {
        
$rc mysql_field_table($this->result$offset);
        return 
$rc===false?'':$rc;
    }
}
?>

Vis: HTML source code

Database, HTML source code

Den fulde HTML kildekode for Database klassen

<?
Get the html 
for the Database object<br />

?>

Vis: Class methods

Database, Class methods

Her er 'klasse metoderne' for Database klassen:

  • __construct
  • query
  • getInsertId
  • getAffectedRows
  • getNumRows
  • fetchRow
  • fetchObject
  • fetchArray
  • fetchAssoc
  • getNumFields
  • getFieldName
  • getFieldLen
  • getFieldType
  • getFieldFlags
  • getFieldTable
  • set
  • freeResult
  • getResult
  • getError
  • open
  • close
  • getHtml
  • getClassName
  • getMsg
  • addHtml
  • __toString
  • getCacheFileName
  • save
  • content

Vis: Object vars

Database, Object vars

Her er 'objekt variable' for Database klassen:

  • html =>

MenuRight 
triangle.gif

Dansk

Deutch

English (UK)

France

Italy

Norsk

Svensk

English (USA)


 
blank.gif
MenuBottom 
triangle.gif Copyright @ 1999-2010 www.Finn-Rasmussen.com Powered by myPHP Version (5.3.3-7+squeeze3) 1.11
blank.gif