Search
 
SCRIPT & CODE EXAMPLE
 

PHP

php PDO database crud class

///////////////
//php PDO database crud class

<?php

class Database extends PDO
{
	
	public function __construct($DB_TYPE, $DB_HOST, $DB_NAME, $DB_USER, $DB_PASS)
	{
		parent::__construct($DB_TYPE.':host='.$DB_HOST.';dbname='.$DB_NAME, $DB_USER, $DB_PASS);
	}
	
	public function select($sql, $array = array(), $fetchMode = PDO::FETCH_ASSOC)
	{
		$sth = $this->prepare($sql);
		foreach ($array as $key => $value) {
			$sth->bindValue("$key", $value);
		}
		
		if(!$sth->execute()){
			$this->handleError();
			}
		else{
		return $sth->fetchAll($fetchMode);
		}
	}
	
	public function insert($table, $data)
	{
		ksort($data);
		
		$fieldNames = implode('`, `', array_keys($data));
		$fieldValues = ':' . implode(', :', array_keys($data));
		
		$sth = $this->prepare("INSERT INTO $table (`$fieldNames`) VALUES ($fieldValues)");
		
		foreach ($data as $key => $value) {
			$sth->bindValue(":$key", $value);
		}
		
		if(!$sth->execute()){
			$this->handleError();
			//print_r($sth->errorInfo());
		}
	}
	
	public function update($table, $data, $where)
	{
		ksort($data);
		
		$fieldDetails = NULL;
		foreach($data as $key=> $value) {
			$fieldDetails .= "`$key`=:$key,";
		}
		$fieldDetails = rtrim($fieldDetails, ',');
		
		$sth = $this->prepare("UPDATE $table SET $fieldDetails WHERE $where");
		
		foreach ($data as $key => $value) {
			$sth->bindValue(":$key", $value);
		}
		
		$sth->execute();
	}
	
	public function delete($table, $where, $limit = 1)
	{
		return $this->exec("DELETE FROM $table WHERE $where LIMIT $limit");
	}
	
	/* count rows*/
	public function rowsCount($table){
			$sth = $this->prepare("SELECT * FROM ".$table);
			$sth->execute();
			return $sth -> rowCount(); 
		}
	
	/* error check */
	private function handleError()
	{
		if ($this->errorCode() != '00000')
		{
			if ($this->_errorLog == true)
			//Log::write($this->_errorLog, "Error: " . implode(',', $this->errorInfo()));
			echo json_encode($this->errorInfo());
			throw new Exception("Error: " . implode(',', $this->errorInfo()));
		}
	}
	
}



///////////////////////////
// Usage


define('DB_TYPE', 'mysql');
define('DB_HOST', '127.0.0.1');
define('DB_NAME', 'my_db');
define('DB_USER', 'root');
define('DB_PASS', 'password');

$this->db = new Database(DB_TYPE, DB_HOST, DB_NAME, DB_USER, DB_PASS);


Comment

PREVIOUS NEXT
Code Example
Php :: phpmyadmin username password check 
Php :: laravel eloquent many to many query using whereHas 
Php :: php mail 
Php :: php shorten string with dots 
Php :: laravel store array to cache 
Php :: check if session is started 
Php :: explode php all values to int 
Php :: create controller with model resources and request command in laravel 
Php :: guzzlehttp/guzzle version with laravel-websockek 
Php :: css not working in live laravel project 
Php :: explode with new line 
Php :: use font awesome in laravel 8 
Php :: Make a Woo required field not required 
Php :: thousand seperator php 
Php :: guzzle http client 
Php :: laravel migration delete column 
Php :: woocommerce order item get product id 
Php :: end foreach loop 
Php :: laravel where on relationsship column 
Php :: Laravel Model Create Artisan Commant 
Php :: php microtime to ms 
Php :: php define multiple variables as 0 
Php :: request update password laravel 
Php :: php logout 
Php :: laravel module create module 
Php :: php json get value by key 
Php :: check if array is empty php 
Php :: static php 
Php :: php order filename 
Php :: update wordpress query 
ADD CONTENT
Topic
Content
Source link
Name
2+5 =