Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * Created by JetBrains PhpStorm.
- * User: artem
- * Date: 7/23/13
- * Time: 12:58 PM
- * To change this template use File | Settings | File Templates.
- */
- class Database {
- private $host = DB_HOST;
- private $user = DB_USER;
- private $pass = DB_PASS;
- private $dbname = DB_NAME;
- private $stmt;
- /**
- * construction
- */
- public function __construct(){
- // Set DSN
- $dsn = 'mysql:host=' . $this->host . ';dbname=' . $this->dbname;
- // Set options
- $options = array(
- PDO::ATTR_PERSISTENT => true,
- PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
- PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"
- );
- // Create a new PDO instanace
- try{
- $this->dbh = new PDO($dsn, $this->user, $this->pass, $options);
- }
- // Catch any errors
- catch(PDOException $e){
- $this->error = $e->getMessage();
- }
- }
- /**
- * prepare SQL query
- * @param $query
- */
- public function query($query){
- $this->stmt = $this->dbh->prepare($query);
- }
- /**
- * Bind parameters
- * @param $param
- * @param $value
- * @param null $type
- */
- public function bind($param, $value, $type = null){
- if (is_null($type)) {
- switch (true) {
- case is_int($value):
- $type = PDO::PARAM_INT;
- break;
- case is_bool($value):
- $type = PDO::PARAM_BOOL;
- break;
- case is_null($value):
- $type = PDO::PARAM_NULL;
- break;
- default:
- $type = PDO::PARAM_STR;
- }
- }
- $this->stmt->bindValue($param, $value, $type);
- }
- /**
- * execute query
- * @return mixed
- */
- public function execute(){
- return $this->stmt->execute();
- }
- /**
- * result set
- * @param int $method
- * @return mixed
- */
- public function resultset($method=PDO::FETCH_ASSOC){
- $this->execute();
- return $this->stmt->fetchAll($method);
- }
- /**
- * get single result
- * @return mixed
- */
- public function single(){
- $this->execute();
- return $this->stmt->fetch(PDO::FETCH_ASSOC);
- }
- /**
- * num of rows
- * @return mixed
- */
- public function rowCount(){
- return $this->stmt->rowCount();
- }
- /**
- * last inserted item ID
- * @return string
- */
- public function lastInsertId(){
- return $this->dbh->lastInsertId();
- }
- /**
- * get fetched IDs
- *
- * @return array|bool
- */
- public function fetched_ids() {
- $this->execute();
- $res = array();
- $arr_fetched = $this->stmt->fetchAll(PDO::FETCH_ASSOC);
- if( !$arr_fetched ) return false;
- foreach($arr_fetched as $row) {
- $res[] = current($row);
- }
- return $res;
- }
- /**
- *
- * @return bool
- */
- public function beginTransaction(){
- return $this->dbh->beginTransaction();
- }
- /**
- * @return bool
- */
- public function endTransaction(){
- return $this->dbh->commit();
- }
- /**
- * @return bool
- */
- public function cancelTransaction(){
- return $this->dbh->rollBack();
- }
- /**
- * @return mixed
- */
- public function debugDumpParams(){
- return $this->stmt->debugDumpParams();
- }
- /**
- * MySQL add function
- * @param $table
- * @param $values
- * @return string
- */
- public function add($table, $values){
- $fieldsArr = array_keys($values);
- $fields = implode(", ", $fieldsArr);
- $bindFields = implode(", :", $fieldsArr);
- $this->query('INSERT INTO ' . $table . ' (' . $fields . ') VALUES (:' . $bindFields . ')');
- foreach($values as $field => $value){
- $this->bind(":".$field, $value);
- }
- try{
- $this->execute();
- } catch(PDOException $e) {
- return $e->getMessage();
- }
- return (int) $this->lastInsertId();
- }
- /**
- * MySQL add multiple records function
- * @param $table
- * @param $values
- * @param $keyArr - foreign key for programs
- * @return bool
- */
- public function addMulti($table, $values, $keyArr=array()){
- foreach($values as $arrValues){
- $arrValues = array_merge($keyArr, $arrValues);
- $fieldsArr = array_keys($arrValues);
- $insertFields = implode(", ", $fieldsArr);
- $bindFields = implode(", :", $fieldsArr);
- $newFieldsArr = array();
- foreach($fieldsArr as $fieldName){
- array_push($newFieldsArr, $fieldName." = :".$fieldName);
- }
- $updateFields = implode(", ", $newFieldsArr);
- $this->query('INSERT INTO ' . $table . ' (' . $insertFields . ') VALUES (:' . $bindFields . ')
- ON DUPLICATE KEY UPDATE '. $updateFields);
- foreach($arrValues as $field => $value){
- $this->bind(":".$field, $value);
- }
- $this->execute();
- }
- // yeah at the end it will always be true
- return true;
- }
- /**
- * MySQL update function
- * @param $table
- * @param $values
- * @param $id
- * @return mixed
- */
- public function update($table, $values, $id){
- $fieldsArr = array_keys($values);
- $newFieldsArr = array();
- foreach($fieldsArr as $fieldName){
- array_push($newFieldsArr, $fieldName." = :".$fieldName);
- }
- $fields = implode(", ", $newFieldsArr);
- $this->query('UPDATE ' . $table . ' SET ' . $fields . ' WHERE ID=:id');
- $this->bind(":id", $id);
- foreach($values as $field => $value){
- $this->bind(":".$field, $value);
- }
- return $this->execute();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement