diff options
-rw-r--r-- | Db.class.php | 6 | ||||
-rw-r--r-- | SQL dump/testdb.sql | 38 | ||||
-rw-r--r-- | composer.json | 4 | ||||
-rw-r--r-- | easyCRUD/Person.class.php | 13 | ||||
-rw-r--r-- | easyCRUD/easyCRUD.class.php | 129 | ||||
-rw-r--r-- | easyCRUD/index.php | 48 | ||||
-rw-r--r-- | settings.ini.php | 6 |
7 files changed, 5 insertions, 239 deletions
diff --git a/Db.class.php b/Db.class.php index 9040728..7494a7a 100644 --- a/Db.class.php +++ b/Db.class.php @@ -52,12 +52,12 @@ class DB */ private function Connect() { - $this->settings = parse_ini_file("settings.ini.php"); - $dsn = 'mysql:dbname='.$this->settings["dbname"].';host='.$this->settings["host"].''; + global $settings; + $dsn = 'mysql:dbname='.$settings["dbname"].';host='.$settings["dbhost"].''; try { # Read settings from INI file, set UTF8 - $this->pdo = new PDO($dsn, $this->settings["user"], $this->settings["password"], array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8")); + $this->pdo = new PDO($dsn, $ettings["dbuser"], $settings["dbpass"], array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8")); # We can now log any exceptions on Fatal error. $this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); diff --git a/SQL dump/testdb.sql b/SQL dump/testdb.sql deleted file mode 100644 index eadf3fb..0000000 --- a/SQL dump/testdb.sql +++ /dev/null @@ -1,38 +0,0 @@ -/* -Navicat MySQL Data Transfer - -Source Server : localhost_3306 -Source Server Version : 50527 -Source Host : localhost:3306 -Source Database : testdb - -Target Server Type : MYSQL -Target Server Version : 50527 -File Encoding : 65001 - -Date: 2012-11-12 14:07:39 -*/ - -SET FOREIGN_KEY_CHECKS=0; - --- ---------------------------- --- Table structure for `persons` --- ---------------------------- -DROP TABLE IF EXISTS `persons`; -CREATE TABLE `persons` ( - `Id` int(11) NOT NULL AUTO_INCREMENT, - `Firstname` varchar(32) DEFAULT NULL, - `Lastname` varchar(32) DEFAULT NULL, - `Sex` char(1) DEFAULT NULL, - `Age` tinyint(3) DEFAULT NULL, - PRIMARY KEY (`Id`) -) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=latin1; - --- ---------------------------- --- Records of persons --- ---------------------------- -INSERT INTO `persons` VALUES ('1', 'John', 'Doe', 'M', '19'); -INSERT INTO `persons` VALUES ('2', 'Bob', 'Black', 'M', '40'); -INSERT INTO `persons` VALUES ('3', 'Zoe', 'Chan', 'F', '21'); -INSERT INTO `persons` VALUES ('4', 'Sekito', 'Khan', 'M', '19'); -INSERT INTO `persons` VALUES ('5', 'Kader', 'Khan', 'M', '56'); diff --git a/composer.json b/composer.json index 2f2ef8a..0db4daf 100644 --- a/composer.json +++ b/composer.json @@ -1,6 +1,6 @@ { - "name": "Indieteq/PHP-MySQL-PDO-Database-Class", + "name": "jayc89/PHP-MySQL-PDO-Database-Class", "require": { "php": ">=5.3.0" } -}
\ No newline at end of file +} diff --git a/easyCRUD/Person.class.php b/easyCRUD/Person.class.php deleted file mode 100644 index 131b6b3..0000000 --- a/easyCRUD/Person.class.php +++ /dev/null @@ -1,13 +0,0 @@ -<?php - require_once("easyCRUD.class.php"); - - class Person Extends Crud { - - # Your Table name - protected $table = 'persons'; - - # Primary Key of the Table - protected $pk = 'id'; - } - -?>
\ No newline at end of file diff --git a/easyCRUD/easyCRUD.class.php b/easyCRUD/easyCRUD.class.php deleted file mode 100644 index 2033b15..0000000 --- a/easyCRUD/easyCRUD.class.php +++ /dev/null @@ -1,129 +0,0 @@ -<?php -/** -* Easy Crud - This class kinda works like ORM. Just created for fun :) -* -* @author Author: Vivek Wicky Aswal. (https://twitter.com/#!/VivekWickyAswal) -* @version 0.1a -*/ -require_once(__DIR__ . '/../Db.class.php'); -class Crud { - - private $db; - - public $variables; - - public function __construct($data = array()) { - $this->db = new DB(); - $this->variables = $data; - } - - public function __set($name,$value){ - if(strtolower($name) === $this->pk) { - $this->variables[$this->pk] = $value; - } - else { - $this->variables[$name] = $value; - } - } - - public function __get($name) - { - if(is_array($this->variables)) { - if(array_key_exists($name,$this->variables)) { - return $this->variables[$name]; - } - } - - $trace = debug_backtrace(); - trigger_error( - 'Undefined property via __get(): ' . $name . - ' in ' . $trace[0]['file'] . - ' on line ' . $trace[0]['line'], - E_USER_NOTICE); - return null; - } - - public function save($id = "0") { - $this->variables[$this->pk] = (empty($this->variables[$this->pk])) ? $id : $this->variables[$this->pk]; - - $fieldsvals = ''; - $columns = array_keys($this->variables); - - foreach($columns as $column) - { - if($column !== $this->pk) - $fieldsvals .= $column . " = :". $column . ","; - } - - $fieldsvals = substr_replace($fieldsvals , '', -1); - - if(count($columns) > 1 ) { - $sql = "UPDATE " . $this->table . " SET " . $fieldsvals . " WHERE " . $this->pk . "= :" . $this->pk; - return $this->db->query($sql,$this->variables); - } - } - - public function create() { - $bindings = $this->variables; - - if(!empty($bindings)) { - $fields = array_keys($bindings); - $fieldsvals = array(implode(",",$fields),":" . implode(",:",$fields)); - $sql = "INSERT INTO ".$this->table." (".$fieldsvals[0].") VALUES (".$fieldsvals[1].")"; - } - else { - $sql = "INSERT INTO ".$this->table." () VALUES ()"; - } - - return $this->db->query($sql,$bindings); - } - - public function delete($id = "") { - $id = (empty($this->variables[$this->pk])) ? $id : $this->variables[$this->pk]; - - if(!empty($id)) { - $sql = "DELETE FROM " . $this->table . " WHERE " . $this->pk . "= :" . $this->pk. " LIMIT 1" ; - return $this->db->query($sql,array($this->pk=>$id)); - } - } - - public function find($id = "") { - $id = (empty($this->variables[$this->pk])) ? $id : $this->variables[$this->pk]; - - if(!empty($id)) { - $sql = "SELECT * FROM " . $this->table ." WHERE " . $this->pk . "= :" . $this->pk . " LIMIT 1"; - $this->variables = $this->db->row($sql,array($this->pk=>$id)); - } - } - - public function all(){ - return $this->db->query("SELECT * FROM " . $this->table); - } - - public function min($field) { - if($field) - return $this->db->single("SELECT min(" . $field . ")" . " FROM " . $this->table); - } - - public function max($field) { - if($field) - return $this->db->single("SELECT max(" . $field . ")" . " FROM " . $this->table); - } - - public function avg($field) { - if($field) - return $this->db->single("SELECT avg(" . $field . ")" . " FROM " . $this->table); - } - - public function sum($field) { - if($field) - return $this->db->single("SELECT sum(" . $field . ")" . " FROM " . $this->table); - } - - public function count($field) { - if($field) - return $this->db->single("SELECT count(" . $field . ")" . " FROM " . $this->table); - } - -} -?> diff --git a/easyCRUD/index.php b/easyCRUD/index.php deleted file mode 100644 index b48efa3..0000000 --- a/easyCRUD/index.php +++ /dev/null @@ -1,48 +0,0 @@ -<?php -// Require the person class file - require("Person.class.php"); - -// Instantiate the person class - $person = new Person(); - -// Create new person - $person->Firstname = "Kona"; - $person->Age = "20"; - $person->Sex = "F"; - $creation = $person->Create(); - -// Update Person Info - $person->id = "4"; - $person->Age = "32"; - $saved = $person->Save(); - -// Find person - $person->id = "4"; - $person->Find(); - - d($person->Firstname, "Person->Firstname"); - d($person->Age, "Person->Age"); - -// Delete person - $person->id = "17"; - $delete = $person->Delete(); - - // Get all persons - $persons = $person->all(); - - // Aggregates methods - d($person->max('age'), "Max person age"); - d($person->min('age'), "Min person age"); - d($person->sum('age'), "Sum persons age"); - d($person->avg('age'), "Average persons age"); - d($person->count('id'), "Count persons"); - - function d($v,$t) - { - echo '<pre>'; - echo '<h1>' . $t. '</h1>'; - var_dump($v); - echo '</pre>'; - } - -?> diff --git a/settings.ini.php b/settings.ini.php deleted file mode 100644 index b554d52..0000000 --- a/settings.ini.php +++ /dev/null @@ -1,6 +0,0 @@ -;<?php return; ?> -[SQL] -host = localhost -user = root -password = -dbname = testdb |