Merhaba, Ziyaretçi. Lütfen giriş yapın veya üye olun.
Aktivasyon mailiniz gelmediyse buraya tıklayın.
06, 2008, 09:44:41 pm
42720 Mesaj 8081 Konu Gönderen: 17918 Üye
Son üye: tlord
Turk-Php.Com Forum  |  Yardım Forumları  |  PHP Yardım Forumu  |  PHP Class Deposu  |  Gelişmiş MySQL Sınıfı « önceki sonraki »
Sayfa: [1] Yazdır
Yazan Konu: Gelişmiş MySQL Sınıfı  (Okunma Sayısı 689 defa)
OnurPhp
Yeni Kullanıcılar
*
Offline Offline

Mesaj Sayısı: 4


Üyelik Bilgileri
Gelişmiş MySQL Sınıfı
« : 14, 2007, 09:16:37 am »

Kod:
<?php

/**
 * PHP versions 4 and 5
 *
 * Database Class
 *
 * Copyright (C) 2006 Onur Yerlikaya
 * LICENCE : This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.

 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.

 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * @category   Core
 * @package   Database
 * @author     Onur Yerlikaya <onur.yerlikaya@linux.org.tr>
 * @version    0.0.1
 * @link   http://www.onuryerlikaya.com
 * @licence GNU General Public Licence - Version 2
 * @copyright  Onur Yerlikaya
 */


class Database
{
/**
 * Connection Link
 *
 * @var mixed
 */
var $link null;

/** 
 * Error Report Variables
 *
 * @var string $error Error Text
 * @var integer $errno Error Number
 */
var $error null;
var $errno 0;

/**
 * Temporary Variable
 *
 * @var string
 */
var $tmp null;

/**
 * Holds Sql Results
 *
 * @var string
 */
var $result null;

/**
 *  Debug Tpl File
 * 
 * @var string
 */
var $debug_tpl  = 'db_debug.tpl';



/**
 * Database Class Version
 * 
 * @var string
 */
var $version'0.0.1';
 
/**
 * Construct can set sql informations
 *
 * @static
 */
function Database() { }

/**
 * Connects Sql Database
 * 
 * @return boolean
 */
function connect($dsn)
{
// Sql Connection with Default mysql drivers
@$this->link mysql_connect($dsn['hostname'],$dsn['username'],$dsn['password']);

// Debugging ..
if(!$this->link)
{
// Calls Error Handling function
$this->_error();
} else {
// Calling selectDatabase function in sql class
$this->_selectDatabase($dsn['database']);
return true;
}

}

/**
 * Database Class select Database
 *
 * @param  string Database Name
 * @return boolean
 */
function _selectDatabase($Database)
{
// Sql Select Database with mysql drivers
@$sqlSelectDatabase mysql_select_db($Database);

// Debugging ..
if(!$sqlSelectDatabase)
{
// Calls Error Handling function
$this->_error();
} else {
return true;
}
}

/**
 * Checks Sql Errors
 *
 * @param string $process Holds process variable
 * @return  Error|false  Sql Errors
 */
function checkError($process)
{
// Debugging ..
if(!$process && $this->error != null && $this->errno != 0)
{
// Get Debug Template Content
$debug file_get_contents(dirname(__FILE__).'/'.$this->debug_tpl);

// Replaces {-error-}
$debug str_replace('{-error-}',$this->error,$debug);

// Replaces {-errno-}
$debug str_replace('{-errno-}',$this->errno,$debug);
die($debug);
} else {
return true;
}
}

/**
 * Prepares Sql Query
 *
 * @param string $sql Sql Code
 * @param string $name Prepared Sql Memory
 */
function prepare($sql,$name='default')
{
// It sets Sql code in memory
$this->tmp[$name] $sql;
}

/**
 * Execute Sql Query Which prepered before
 *
 * @param string $name Prepared Sql Memory
 * @param string $array_data Array Data for values..
 * @static
 */
function execute($name='default',$array=null)
{
// Send Query with prepared Sql Code
 $this->query($this->tmp[$name],$array);

}

/**
 * Freeing Result set
 *
 * @param integer if is zero Database class frees all memory
 * @static
 * @return  boolean
 */
function free($type=1)
{
/**
 * If type variable sets zero unset Database class 
 * temporary variables from memory
 */
if($type == ) {
unset($this->tmp);
}

// Sql Free Results with mysql drivers
@$sqlFreeResult mysql_free_result($this->result);

// Debugging ..
if(!$sqlFreeResult)
{
// Calls Error Handling function
$this->_error();
} else {
return true;
}
}

/**
 * Prepares sql queries based on you supply
 *
 * <code>
 *  ..
 * $table_name = 'users';
 * $table_fields = array('id','name','surname');
 *
 * /**
 *  * This is AutoPrepare a new SQL code
 *  * 
 *  * Select (id,name,surname) From users where id=1
 *  * / 
 * $Database->autoPrepare($table_name,$table_fields,'select','where id=1');
 * // $Database->autoPrepare($table_name,$table_fields,'update','where id=2');
 *
 * </code>
 * @param string $tableName Table Name
 * @param string $tableFields Table Fields in array
 * @param string $type Sql query types ( select,insert,update,delete )
 * @param string $statements Sql Statements 
 * @param string $name Prepared Sql Memory
 * @return boolean
 */
function autoPrepare($tableName,$tableFields='*',$type,$statements=null,$name='default')
{
// Setting fields for select and insert queries
if(is_array($tableFields)) {
$table_fields "(".join(',',$tableFields).")";

switch($type)
{
case 'select':
// Creating Sql Code for Select Query
$this->tmp[$name] "Select ".$table_fields." From ".$tableName." ".$statements;
break;

case 'insert':
// Creating Sql Code for Insert Query
$insert "(".join(',',array_fill(1,count($tableFields),'?')).")";
$this->tmp[$name] "insert into ".$tableName." ".$table_fields" VALUES ".$insert." ".$statements;
break;

case 'update':
// Creating Sql Code for Update Query
for($i=0;$i<count($tableFields);$i++)
{
$update .= $tableFields[$i]."=?, ";
}
$this->tmp[$name] "Update ".$tableName." SET ".$update." ".$statements;
break;

case 'delete':
// Creating Sql Code for Delete Query
$this->tmp[$name] "Delete From ".$tableName." ".$statements;
break;
}
}

/**
 * Get number of fields in result
 *
 * @return false|$sqlNumCols count fields in results.
 */
 
function numCols()
{
// Getting num fields with mysql drivers
@$sqlNumCols mysql_num_fields($this->result);

// Debugging..
if(!$sqlNumCols)
{
// Calls Error Handling function
$this->_error();
} else {
return $sqlNumCols;
}
}

/**
 * Get number of rows in result
 *
 * @return false|$sqlNumRows count rows in result
 */

function numRows()
{
// Getting num rows with mysql drivers
@$sqlNumRows mysql_num_rows($this->result);

// Debugging..
if(!$sqlNumRows)
{
// Calls Error Handling function
$this->_error();
} else {
return $sqlNumRows;
}


/**
 * Fetching Result
 *
 * @return false|$sqlFetch Fetched Result
 */
function fetch()
{
// Fetching result with mysql drivers
@$sqlFetch mysql_fetch_array($this->result,MYSQL_BOTH);

// Debugging..
if(!$sqlFetch)
{
// Calls Error Handling function
$this->_error();
} else {
return $sqlFetch;
}
}

/**
 * Escapes Sql bad characters
 *
 * @return mixed
 */
function escape($data)
{
// Escapes special characters
return addslashes($data);
}

/**
 * Get a data from Sql Query
 * 
 * @param string $sql Sql Code
 * @param boolean $pre Prepared Sql
 * @return  string One Result
 */
function getOne($sql,$pre=false)
{
// Check for prepared before
if($pre) {
$sql $this->tmp[$sql];
}
$this->query($sql);
$fetched =  $this->fetch();
return $fetched[0];
}

/**
 * Get a row from Sql Query
 *
 * @param string $sql Sql Code
 * @param boolean $pre Prepared Sql
 * @return string  One Row Result
 */
function getRow($sql,$pre=false)
{
// Check for prepared before
if($pre) {
$sql $this->tmp[$sql];
}
$this->query($sql);
return $this->fetch();
}

/**
 * Database Class Sql Query
 *
 * @param string $sql Sql Code
 * @param array $array Sql Data
 * @return  false|$sqlQuery Sql Query
 */

function query($sql,$array=null)
{
// Replaced ? characters to string ( %s )
@$editSqlCode str_replace('?',"'%s'",$sql);

// Formatted Sql code
@$sql vsprintf($editSqlCode,$array);

// Query with mysql drivers
@$this->result mysql_query($sql);

//Debugging
if(!$this->result)
{
// Calls Error Handling function
$this->_error();
} else {
return $this->result;
}
}

/**
 * Disconnect from sql
 *
 * @return boolean
 */
function disconnect()
{
// Close with mysql drivers
@$sqlClose mysql_close($this->link);

// Debugging..
if(!$sqlClose)
{
// Calls Error Handling function
$this->_error();
} else {
return true;
}
}

/**
 * Error Handling
 *
 * @return boolean
 */
function _error()
{
// Assign error with mysql driver
$this->error mysql_error();

// Assign error number with mysql driver
$this->errno mysql_errno();
return false;
}


?>

Logged
Sayfa: [1] Yazdır 
« önceki sonraki »
Gitmek istediğiniz yer:  


Turk-Php.Com Forum | SMF Forum Software © 2005, Simple Machines LLC. All Rights Reserved.