Top  Branding  Banner 
blank.gif
blank.gif
triangle.gif Du er her: /  Forsiden  /  Kildekoden  /  Db  /  Connection   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

Connection.php


Vis: Sample code, tutorial

Connection, Sample code, tutorial

Sådan benyttes komponenten Connection klassen

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

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

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

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

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

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

Skjul: Sådan vises komponenten

Connection, Sådan vises komponenten

Sådan vises komponenten Connection klassen


Vis: PHP source code

Connection, PHP source code

Den fulde PHP kildekode for Connection klassen

<?php

/**
 * @package db
 * @see HTML_DB_DATABASE_PATH.'/Connection.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_BASIC_UTIL_PATH.'/Message.php');
if (
defined('HTML_LOG_UTIL_PATH')) {
    require_once (
HTML_LOG_UTIL_PATH.'/Log.php');
}

/**
 * The connection interface towards the mysql database
 * <code>
 * Usage:
 *   $connection = new Connection($hostname, $username, $password, $database, $persistent);
 *   $connection->open();  // Conect to the host and select the database to use
 *           :             // Use the connection to operate on table data
 *          :              // Free the resources
 *   $connection->close(); // Remember to release the connection
 *
 * NOTE: The username/password are defined in an external file 
 * </code>
 * @package db
 * @todo Register function, when closing page
 */

class Connection {
    private 
$hostname   '';
    private 
$username   '';
    private 
$password   '';
    private 
$database   '';
    private 
$persistent '';
    private 
$portname   '';

    private 
$connection NULL;  // The database connection
    
private $message    '';    // Debug/Error/info messages
    
private $anyerrors  false// Flag, to indicate errors have occured

    /**
     * Constructor
     * @param String  $hostname   The server name to connect to
     * @param String  $username   The username
     * @param String  $password   The password (secret, don't look)
     * @param String  $database   The database to use
     * @param boolean $persistent Use persistent connection (true) or not (false)
     * @param String  $portname   The server port name to connect to
     */
    
function __construct($hostname=''$username=''$password=''$database=''$persistent=''$portname='') {
        
$this->hostname   $hostname;
        
$this->username   $username;
        
$this->password   $password;
        
$this->database   $database;
        
$this->persistent $persistent != '' $persistent false;
        
$this->portname   $portname;
    }

    
/**
     * Set the host name for the server to connect to
     * @param String $hostname The server name to connect to
     */
    
function setHostname($hostname) {
        
$this->hostname $hostname;
    }

    
/**
     * Set the username
     * @param String $username The username
     */
    
function setUsername($username) {
        
$this->username $username;
    }

    
/**
     * Set the password
     * @param String $password The password (secret, don't look)
     */
    
function setPassword($password) {
        
$this->password $password;
    }

    
/**
     * Set the name of the database to use
     * @param String $database The database to use
     */
    
function setDatabase($database) {
        
$this->database $database;
    }

    
/**
     * Set the flag, if the connection use persistent connection (true)
     * @param boolean $persistent Use persistent connection (true) or not (false)
     */
    
function setPersistent($persistent) {
        
$this->persistent $persistent;
    }

    
/**
     * Set the host port name for the server to connect to
     * @param String $portname The server port name to connect to
     */
    
function setPortname($portname) {
        
$this->portname $portname;
    }

    
/**
     * Connect to the host, and select the database to use
     * @return boolean Returns TRUE on success or FALSE if failure
     */
    
function open() {
        
$rc false;
        
$func 'mysql_'. ($this->persistent 'p' '').'connect';
        
$this->connection = @$func($this->host$this->username$this->password);
        if (!
$this->connection) {
            
$msg 'Connection->open() '.$this->getError();
            if (
defined('HTML_LOG_UTIL_PATH')) {
                
Log::error($msg__FILE____LINE__);
            }
            if (
defined('DEBUG_LEVEL_SHOW_SQL') && DEBUG_LEVEL DEBUG_LEVEL_SHOW_SQL) {
                
Message::add($msg__FILE____LINE__);
            }
        } else {
            if (!
mysql_select_db($this->database$this->connection)) {
                
$msg 'Connection->open() '.$this->getError();
                if (
defined('HTML_LOG_UTIL_PATH')) {
                    
Log::error($msg__FILE____LINE__);
                }
                if (
defined('DEBUG_LEVEL_SHOW_SQL') && DEBUG_LEVEL DEBUG_LEVEL_SHOW_SQL) {
                    
Message::add($msg__FILE____LINE__);
                }
            } else {
                
$rc true;
            }
        }
        return 
$rc;
    }

    
/**
     * Get the connection to the database
     * @return resource The connection to the database or NULL
     */
    
function getConnection() {
        if (
$this->connection==NULL) {
            
$this->open();
            
$msg 'Connection->getConnection() this->connection was NULL';
            
$this->setMessage($msg);
            if (
defined('HTML_LOG_UTIL_PATH')) {
                
//Log::debug($msg, __FILE__, __LINE__);
            
}
            if (
defined('DEBUG_LEVEL_SHOW_SQL') && DEBUG_LEVEL DEBUG_LEVEL_SHOW_SQL) {
                
Message::add($msg__FILE____LINE__);
            }
        } else {
        }
        return 
$this->connection;
    }

    
/**
     * Get the name of the database
     * @return String The name of the database, specified in the constructor
     */
    
function getDatabase() {
        return 
$this->database;
    }

    
/**
     * Close the connection to the database
     * @return boolean Returns TRUE on success or FALSE on failure
     */
    
function close() {
        
$rc false;
        if (
$this->connection!=NULL) {
            
$rc mysql_close($this->connection);
            
$this->connection NULL;
        } else {
            
$msg 'Connection->close() this->connection is NULL';
            
$this->setMessage($msg);
            if (
defined('HTML_LOG_UTIL_PATH')) {
                
//Log::debug($msg, __FILE__, __LINE__);
            
}
            if (
defined('DEBUG_LEVEL_SHOW_SQL') && DEBUG_LEVEL DEBUG_LEVEL_SHOW_SQL) {
                
Message::add($msg__FILE____LINE__);
            }
        }
        return 
$rc;
    }

    
/**
     * Get the error message
     * @return String The resulting error message from the last mysql call
     */
    
function getError() {
        
$error mysql_error();
        if (
$error != '') {
            
$this->anyerrors true// Dump error info later
        
}
        
$this->setMessage($error);
        return 
$error;
    }

    
/**
     * Get all message
     * @return String All messages from this scenario
     */
    
function getMessage() {
        return 
$this->message;
    }

    
/**
     * Set next message in this scenario
     * @param String $message The message to store
     */
    
function setMessage($message) {
        
$this->message .= "<br />\r\n".$message;
    }
    
/**
     * Get the html for the class
     * @return String The html
     */
    
function getHtml() {
         
$html "";
         return 
$html;
    }
}
?>

Vis: HTML source code

Connection, HTML source code

Den fulde HTML kildekode for Connection klassen

<?

?>

Vis: Class methods

Connection, Class methods

Her er 'klasse metoderne' for Connection klassen:

  • __construct
  • setHostname
  • setUsername
  • setPassword
  • setDatabase
  • setPersistent
  • setPortname
  • open
  • getConnection
  • getDatabase
  • close
  • getError
  • getMessage
  • setMessage
  • getHtml

Vis: Object vars

Connection, Object vars

Her er 'objekt variable' for Connection klassen:


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