<?php
class DBHandler { public static function Connect($DB_HOST, $DB_NAME, $DB_USER, $DB_PASS) { try { //mysql conn $con=mysql_connect($DB_HOST,$DB_USER,$DB_PASS); $db=mysql_select_db($DB_NAME); } catch (PDOException $e) { print "Error!: " . $e->getMessage() . "<br/>"; die(); } } public function DBHandler($DB_HOST, $DB_NAME, $DB_USER, $DB_PASS) { return $this->__construct($DB_HOST, $DB_NAME, $DB_USER, $DB_PASS); } public function __construct($DB_HOST, $DB_NAME, $DB_USER, $DB_PASS) { self::Connect($DB_HOST, $DB_NAME, $DB_USER, $DB_PASS); } /** * select * @param string $sql An SQL string * @param array $array Paramters to bind * @param constant $fetchMode A PDO Fetch mode * @return mixed */ public static function Query($strTable, $arFields = '*', $strWhere = '', $strOrder = '',$intRecords=10, $intPage = 1 ) { if($strWhere != '') { $strWhere = ' where '. $strWhere; } if($strOrder != '') { $strOrder = 'order by '.$strOrder; } $limit = sprintf(' limit %d, %d', ($intPage-1) * $intRecords, $intRecords); if( is_array( $arFields )){ $strFields = implode( ',', $arFields); }else{ $strFields = $arFields; if( $strFields == ''){ $strFields = '*';} } $strSQL = "SELECT $strFields FROM $strTable $strWhere $strOrder $limit"; $objRes = mysql_query($strSQL) or die('Invalid SQL: '.mysql_error()); return $objRes; } public static function QueryArray($strTable, $arFields = '*', $strWhere = '', $strOrder = '',$intRecords=10, $intPage = 1 ) { $objRes = self::Query( $strTable, $arFields, $strWhere, $strOrder, $intRecords, $intPage); return self::MySQL_to_Array($objRes); }
/** * insert * @param string $table A name of table to insert into * @param string $data An associative array */ public static function insert($table, $data) { $fieldNames = implode('`, `', array_keys($data)); $fieldValues = implode('", "', array_values($data)); $sth = mysql_query("INSERT INTO $table (`$fieldNames`) VALUES (\"$fieldValues\")"); return mysql_insert_id();
} public static function buildUpdateString( $arrData, $strDelim = ', ' ){ $arrRows = array(); foreach($arrData as $key=> $value) { $arrRows[] = "`$key`='$value'"; } return implode( $strDelim, $arrRows ); } /** * update * @param string $table A name of table to insert into * @param string $data An associative array * @param string $where the WHERE query part */ public static function update($table, $data, $where) { //ksort($data); $fieldDetails = NULL; $fieldDetails = self::buildUpdateString( $data ); $sqlStr="UPDATE $table SET $fieldDetails WHERE $where"; $sth = mysql_query($sqlStr) or die('SQL Error: '.mysql_error()); return $sth; } /** * delete * * @param string $table * @param string $where * @param integer $limit * @return integer Affected Rows */ public static function delete($table, $where='') { if ($where=='') return false; $strSql="DELETE FROM $table WHERE $where "; //echo $strSql; mysql_query($strSql) or die(mysql_error()); return true; } /** Public function to make database query */ public static function MySQL_to_Array( $arResult, $arFields = '' ) { if( !is_array( $arFields )) { $totFields = mysql_num_fields( $arResult ); $i = 0; while( $i<$totFields) { $arFields[] = mysql_field_name( $arResult, $i++); } } $content = array(); $i = 0; if(mysql_num_rows($arResult)>0) { while( $row = mysql_fetch_assoc($arResult)) { foreach($arFields as $field) { $content[ $i ][ $field ] = $row[ $field ] ; $content[ $i ][ $field ] = $content[ $i ][ $field ]; } $i++; } }else{ $content = false; } return $content; }
}
|