pdo.php
| 8.3 KB | Satir:
0
| php
Geri
<?php require("Log.class.php"); class db{ # @object, The PDO object private $pdo; # @object, PDO statement object private $sQuery; # @array, The database settings private $settings; # @bool , Connected to the database private $bConnected = false; # @object, Object for logging exceptions private $log; # @array, The parameters of the SQL query private $parameters; /** * Default Constructor * * 1. Instantiate Log class. * 2. Connect to database. * 3. Creates the parameter array. */ public function __construct() { $this->log = new Log(); $this->Connect(); $this->parameters = array(); } /** * This method makes connection to the database. * * 1. Reads the database settings from a ini file. * 2. Puts the ini content into the settings array. * 3. Tries to connect to the database. * 4. If connection failed, exception is displayed and a log file gets created. */ var $dsn="mysql:host=localhost;dbname=yeniistiklal_yeniistiklal;charset=utf8"; var $user="yeniistiklal_venus"; var $password="yv9wVK~oQW{V"; private function baglantiAc() { try { $this->db = new PDO($this->dsn, $this->user, $this->password); } catch (PDOException $e) { echo 'Veritabanı bağlantısı başarısız oldu: ' . $e->getMessage(); exit; } } private function baglantiKapat() { $this->db = null; } private function Connect() { try { # Read settings from INI file, set UTF8 $this->pdo = new PDO($this->dsn, $this->user, $this->password); $this->pdo->exec("SET NAMES 'utf8mb4'; SET CHARSET 'utf8'"); $this->pdo->exec("set names utf8mb4"); # We can now log any exceptions on Fatal error. $this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); # Disable emulation of prepared statements, use REAL prepared statements instead. $this->pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false); # Connection succeeded, set the boolean to true. $this->bConnected = true; } catch (PDOException $e) { # Write into log echo $this->ExceptionLog($e->getMessage()); die(); } } /* * You can use this little method if you want to close the PDO connection * */ public function CloseConnection() { # Set the PDO object to null to close the connection # http://www.php.net/manual/en/pdo.connections.php $this->pdo = null; } /** * Every method which needs to execute a SQL query uses this method. * * 1. If not connected, connect to the database. * 2. Prepare Query. * 3. Parameterize Query. * 4. Execute Query. * 5. On exception : Write Exception into the log + SQL query. * 6. Reset the Parameters. */ private function Init($query,$parameters = "") { # Connect to database if(!$this->bConnected) { $this->Connect(); } try { # Prepare query $this->sQuery = $this->pdo->prepare($query); # Add parameters to the parameter array $this->bindMore($parameters); # Bind parameters if(!empty($this->parameters)) { foreach($this->parameters as $param) { $parameters = explode("\x7F",$param); $this->sQuery->bindParam($parameters[0],$parameters[1]); } } # Execute SQL $this->succes = $this->sQuery->execute(); } catch(PDOException $e) { # Write into log and display Exception echo $this->ExceptionLog($e->getMessage(), $query ); die(); } # Reset the parameters $this->parameters = array(); } /** * @void * * Add the parameter to the parameter array * @param string $para * @param string $value */ public function bind($para, $value) { $this->parameters[sizeof($this->parameters)] = ":" . $para . "\x7F" . utf8_encode($value); } /** * @void * * Add more parameters to the parameter array * @param array $parray */ public function bindMore($parray) { if(empty($this->parameters) && is_array($parray)) { $columns = array_keys($parray); foreach($columns as $i => &$column) { $this->bind($column, $parray[$column]); } } } /** * If the SQL query contains a SELECT or SHOW statement it returns an array containing all of the result set row * If the SQL statement is a DELETE, INSERT, or UPDATE statement it returns the number of affected rows * * @param string $query * @param array $params * @param int $fetchmode * @return mixed */ public function query($query,$params = null, $fetchmode = PDO::FETCH_ASSOC) { $query = trim($query); $this->Init($query,$params); $rawStatement = explode(" ", $query); # Which SQL statement is used $statement = strtolower($rawStatement[0]); if ($statement === 'select' || $statement === 'show') { return $this->sQuery->fetchAll($fetchmode); } elseif ( $statement === 'insert' || $statement === 'update' || $statement === 'delete' ) { return $this->sQuery->rowCount(); } else { return NULL; } } /** * Returns the last inserted id. * @return string */ public function lastInsertId() { return $this->pdo->lastInsertId(); } /** * Returns an array which represents a column from the result set * * @param string $query * @param array $params * @return array */ public function result($query,$params = null) { $this->Init($query,$params); return $this->sQuery->fetchColumn(); } public function fetch($query,$params = null,$fetchmode = PDO::FETCH_ASSOC) { $this->Init($query,$params); return $this->sQuery->fetch($fetchmode); } public function rows($query,$params = null,$fetchmode = PDO::FETCH_ASSOC) { $this->Init($query,$params); return $this->sQuery->fetchAll($fetchmode); } /** * Returns the value of one single field/column * * @param string $query * @param array $params * @return string */ function guncelle($tablo, $veriler, $id="") { $sonuc = 0; $alan = ""; foreach ($veriler as $anahtar => $deger) $alan .= $anahtar . "= :".$anahtar.","; $alan = substr($alan,0,strlen($alan)-1); $this->baglantiAc(); $query = $this->db->prepare("UPDATE ".$tablo." SET ".$alan." WHERE ".$id.""); $update = $query->execute($veriler); if ( $update ) $sonuc = 1; else $sonuc = 0; $this->baglantiKapat(); return $sonuc; } function idGuncelle($tablo, $veriler, $id) { $sonuc = 0; $alan = ""; foreach ($veriler as $anahtar => $deger) $alan .= $anahtar . "= :".$anahtar.","; $alan = substr($alan,0,strlen($alan)-1); $veriler['id'] = (int)$id; $this->baglantiAc(); $query = $this->db->prepare("UPDATE ".$tablo." SET ".$alan." WHERE id=:id"); $update = $query->execute($veriler); $arr = $query->errorInfo(); if($arr){ //print_r($arr); } if ( $update ) $sonuc = 1; else $sonuc = 0; $this->baglantiKapat(); return $sonuc; } function ekle($tablo, $veriler) { $sonuc = 0; $alan1 = ""; $alan2 = ""; foreach ($veriler as $anahtar => $deger) { $alan1 .= $anahtar . ","; $alan2 .= ":".$anahtar.","; } $alan1 = substr($alan1,0,strlen($alan1)-1); $alan2 = substr($alan2,0,strlen($alan2)-1); $this->baglantiAc(); $query = $this->db->prepare("INSERT INTO ".$tablo." (".$alan1.") VALUES (".$alan2.")"); $query->execute($veriler); $arr = $query->errorInfo(); if($arr){ print_r($arr); } if ( $query ) $sonuc = $this->db->lastInsertId(); else $sonuc = 0; $this->baglantiKapat(); return $sonuc; } public function count($query,$params = null) { $this->Init($query,$params); return $this->sQuery->rowcount(); } public function single($query,$params = null) { $this->Init($query,$params); return $this->sQuery->fetchColumn(); } /** * Writes the log and returns the exception * * @param string $message * @param string $sql * @return string */ private function ExceptionLog($message , $sql = "") { $exception = 'Unhandled Exception. <br />'; $exception .= $message; $exception .= "<br /> You can find the error back in the log."; if(!empty($sql)) { # Add the Raw SQL to the Log $message .= "\r\nRaw SQL : " . $sql; } # Write into log $this->log->write($message); return $exception; } } $sys=new db(); ?>
Kaydet
Ctrl+S ile kaydet