05-18-2009, 03:33 PM
|
#3 (permalink)
|
|
The Acquainted
Join Date: Oct 2007
Posts: 170
Thanks: 18
|
I've made a little update to the database class, here it is:
Again, tips and suggestions are more than welcome!
PHP Code:
<?php
class Database { private static $m_pInstance; private $m_pConn; private function __construct() { if (LOCAL) { $this->connect('localhost', 'username', 'password', 'database'); } else { $this->connect('localhost', 'username', 'password', 'database'); } } private function __clone() { } public static function get_instance() { if (!self::$m_pInstance) { self::$m_pInstance = new Database(); } return self::$m_pInstance; } public function connect($szHostname, $szUsername, $szPassword, $szDatabase) { try { $this->m_pConn = new PDO('mysql:host='.$szHostname.';dbname='.$szDatabase, $szUsername, $szPassword); if (LOCAL) { $this->m_pConn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); } else { $this->m_pConn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_SILENT); } } catch (PDOException $e) { echo $e->getMessage(); } } public function query($szQuery) { $pStatement = $this->m_pConn->prepare($szQuery); $pStatement->execute(); $aResult = $pStatement->fetchAll(); return $aResult; } public function select_query($szTable, $aFields, $aWhere = NULL, $aOrderBy = NULL, $aLimit = NULL) { $szQuery = 'SELECT '; $szQuery .= implode(', ',$aFields); $szQuery .= ' FROM '.$szTable; if (is_array($aWhere)) { $szWhere = ''; foreach($aWhere as $szKey => $szValue) { if (!is_numeric($szValue)) { $szValue = "'".$szValue."'"; } $szWhere .= ' '.$szKey.' = '.$szValue.' AND'; } $szWhere = rtrim($szWhere, 'AND'); $szQuery .= ' WHERE '.$szWhere; } if (is_array($aOrderBy)) { $szOrderBy = ''; foreach($aOrderBy as $szKey => $szValue) { $szOrderBy .= ' '.$szKey.' '.$szValue.','; } $szOrderBy = rtrim($szOrderBy, ','); $szQuery .= ' ORDER BY '.$szOrderBy; } if ($aLimit !== NULL) { $szLimit = implode(',', $aLimit); $szQuery .= ' LIMIT '.$szLimit; } return $this->query($szQuery); } public function select_query_row($szTable, $aFields, $aWhere = NULL, $aOrderBy = NULL, $aLimit = NULL) { $aResult = $this->select_query($szTable, $aFields, $aWhere, $aOrderBy, $aLimit); return (isset($aResult[0]) ? $aResult[0] : FALSE); } public function select_query_column($szTable, $aFields, $aWhere = NULL, $aOrderBy = NULL, $aLimit = NULL) { $aResult = $this->select_query_row($szTable, $aFields, $aWhere, $aOrderBy, $aLimit); return (isset($aResult[0]) ? $aResult[0] : FALSE); } public function insert_query($szTable, $aFields, $aValues) { $szQuery = 'INSERT INTO '.$szTable.' ('; $szQuery .= implode(', ',$aFields); $szQuery .= ') VALUES'; foreach ($aValues as $aValue) { $aValue = array_map(array($this, 'clean_input'), $aValue); $szQuery .= ' ('; $szQuery .= implode(', ',$aValue); $szQuery = rtrim($szQuery, ','); $szQuery .= '),'; } $szQuery = rtrim($szQuery, ','); $iCount = $this->m_pConn->exec($szQuery); return $iCount; } private static function clean_input($mInput) { if (is_numeric($mInput)) { return $mInput; } else { return "'".$mInput."'"; } } }
?>
|
|
|
|