From 43feb75d2886596d4b358c0830bdd9021c129ef7 Mon Sep 17 00:00:00 2001 From: Indieteq Date: Wed, 17 Oct 2012 12:16:48 -0700 Subject: Initial commit --- README.md | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 README.md diff --git a/README.md b/README.md new file mode 100644 index 0000000..5a24453 --- /dev/null +++ b/README.md @@ -0,0 +1,4 @@ +PHP-MySQL-PDO-Database-Class +============================ + +A database class for PHP MySQL which makes use of PDO . \ No newline at end of file -- cgit v1.1 From fda5f14b8fefad0a80d01b694ff5621fc0976894 Mon Sep 17 00:00:00 2001 From: Indieteq Date: Wed, 17 Oct 2012 21:38:56 +0200 Subject: The database files --- Db.class.php | 242 ++++++++++++++++++++++++++++++++++++++++++++ Log.class.php | 70 +++++++++++++ easyCRUD/Person.class.php | 13 +++ easyCRUD/easyCRUD.class.php | 96 ++++++++++++++++++ easyCRUD/index.php | 39 +++++++ settings.ini.php | 5 + 6 files changed, 465 insertions(+) create mode 100644 Db.class.php create mode 100644 Log.class.php create mode 100644 easyCRUD/Person.class.php create mode 100644 easyCRUD/easyCRUD.class.php create mode 100644 easyCRUD/index.php create mode 100644 settings.ini.php diff --git a/Db.class.php b/Db.class.php new file mode 100644 index 0000000..0798c72 --- /dev/null +++ b/Db.class.php @@ -0,0 +1,242 @@ +log = new Log(); + $this->Connect(); + $this->parameters = array(); + } + + /** + * This method makes connection to the database. + * + * 1. Reads the database settings from a ini file. + * 2. Puts the ini content into the settings array. + * 3. Tries to connect to the database. + * 4. If connection failed, exception is displayed and a log file gets created. + */ + private function Connect() + { + $this->settings = parse_ini_file("settings.ini.php"); + $dsn = 'mysql:dbname='.$this->settings["dbname"].';host='.$this->settings["host"].''; + try + { + # Read settings from INI file + $this->pdo = new PDO($dsn, $this->settings["user"], $this->settings["password"]); + + # We can now log any exceptions on Fatal error. + $this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); + + # Disable emulation of prepared statements, use REAL prepared statements instead. + $this->pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false); + + # Connection succeeded, set the boolean to true. + $this->bConnected = true; + } + catch (PDOException $e) + { + # Write into log + echo $this->ExceptionLog($e->getMessage()); + die(); + } + } + /** + * Every method which needs to execute a SQL query uses this method. + * + * 1. If not connected, connect to the database. + * 2. Prepare Query. + * 3. Parameterize Query. + * 4. Execute Query. + * 5. On exception : Write Exception into the log + SQL query. + * 6. Reset the Parameters. + */ + private function Init($query,$parameters = "") + { + # Connect to database + if(!$this->bConnected) { $this->Connect(); } + try { + # Prepare query + $this->sQuery = $this->pdo->prepare($query); + + # Add parameters to the parameter array + $this->bindMore($parameters); + + # Bind parameters + if(!empty($this->parameters)) { + foreach($this->parameters as $param) + { + $parameters = explode("\x7F",$param); + $this->sQuery->bindParam($parameters[0],$parameters[1]); + } + } + + # Execute SQL + $this->succes = $this->sQuery->execute(); + } + catch(PDOException $e) + { + # Write into log and display Exception + echo $this->ExceptionLog($e->getMessage(),$this->sQuery->queryString); + die(); + } + + # Reset the parameters + $this->parameters = array(); + } + + /** + * @void + * + * Add the parameter to the parameter array + * @param string $para + * @param string $value + */ + public function bind($para, $value) + { + $this->parameters[sizeof($this->parameters)] = ":" . $para . "\x7F" . $value; + } + /** + * @void + * + * Add more parameters to the parameter array + * @param array $parray + */ + public function bindMore($parray) + { + if(empty($this->parameters) && is_array($parray)) { + $columns = array_keys($parray); + foreach($columns as $i => &$column) { + $this->bind($column, $parray[$column]); + } + } + } + /** + * If the SQL query contains a SELECT statement it returns an array containing all of the result set row + * If the SQL statement is a DELETE, INSERT, or UPDATE statement it returns the number of affected rows + * + * @param string $query + * @param array $params + * @param int $fetchmode + * @return mixed + */ + public function query($query,$params = null,$fetchmode = PDO::FETCH_ASSOC) + { + $query = trim($query); + + $this->Init($query,$params); + + if (stripos($query, 'select') === 0){ + return $this->sQuery->fetchAll($fetchmode); + } + elseif (stripos($query, 'insert') === 0 || stripos($query, 'update') === 0 || stripos($query, 'delete') === 0) { + return $this->sQuery->rowCount(); + } + else { + return NULL; + } + } + /** + * Returns an array which represents a column from the result set + * + * @param string $query + * @param array $params + * @return array + */ + public function column($query,$params = null) + { + $this->Init($query,$params); + $Columns = $this->sQuery->fetchAll(PDO::FETCH_NUM); + + $column = null; + + foreach($Columns as $cells) { + $column[] = $cells[0]; + } + + return $column; + + } + /** + * Returns an array which represents a row from the result set + * + * @param string $query + * @param array $params + * @param int $fetchmode + * @return array + */ + public function row($query,$params = null,$fetchmode = PDO::FETCH_ASSOC) + { + $this->Init($query,$params); + return $this->sQuery->fetch($fetchmode); + } + /** + * Returns the value of one single field/column + * + * @param string $query + * @param array $params + * @return string + */ + public function single($query,$params = null) + { + $this->Init($query,$params); + return $this->sQuery->fetchColumn(); + } + /** + * Writes the log and returns the exception + * + * @param string $message + * @param string $sql + * @return string + */ + private function ExceptionLog($message , $sql = "") + { + $exception = 'Unhandled Exception.
'; + $exception .= $message; + $exception .= "
You can find the error back in the log."; + + if(!empty($sql)) { + # Add the Raw SQL to the Log + $message .= "\r\nRaw SQL : " . $sql; + } + # Write into log + $this->log->write($message); + + return $exception; + } + } +?> \ No newline at end of file diff --git a/Log.class.php b/Log.class.php new file mode 100644 index 0000000..58e5672 --- /dev/null +++ b/Log.class.php @@ -0,0 +1,70 @@ +path = dirname(__FILE__) . $this->path; + } + + /** + * @void + * Creates the log + * + * @param string $message the message which is written into the log. + * @description: + * 1. Checks if directory exists, if not, create one and call this method again. + * 2. Checks if log already exists. + * 3. If not, new log gets created. Log is written into the logs folder. + * 4. Logname is current date(Year - Month - Day). + * 5. If log exists, edit method called. + * 6. Edit method modifies the current log. + */ + public function write($message) { + $date = new DateTime(); + $log = $this->path . $date->format('Y-m-d').".txt"; + + if(is_dir($this->path)) { + if(!file_exists($log)) { + $fh = fopen($log, 'w') or die("Fatal Error !"); + $logcontent = "Time : " . $date->format('H:i:s')."\r\n" . $message ."\r\n"; + fwrite($fh, $logcontent); + fclose($fh); + } + else { + $this->edit($log,$date, $message); + } + } + else { + if(mkdir($this->path,0777) === true) // Check on true, Otherwise we would have a never-ending loop :S + { + $this->write($message); + } + } + } + + /** + * @void + * Gets called if log exists. + * Modifies current log and adds the message to the log. + * + * @param string $log + * @param DateTimeObject $date + * @param string $message + */ + private function edit($log,$date,$message) { + $logcontent = "Time : " . $date->format('H:i:s')."\r\n" . $message ."\r\n\r\n"; + $logcontent = $logcontent . file_get_contents($log); + file_put_contents($log, $logcontent); + } + } +?> \ No newline at end of file diff --git a/easyCRUD/Person.class.php b/easyCRUD/Person.class.php new file mode 100644 index 0000000..131b6b3 --- /dev/null +++ b/easyCRUD/Person.class.php @@ -0,0 +1,13 @@ + \ No newline at end of file diff --git a/easyCRUD/easyCRUD.class.php b/easyCRUD/easyCRUD.class.php new file mode 100644 index 0000000..3e4c647 --- /dev/null +++ b/easyCRUD/easyCRUD.class.php @@ -0,0 +1,96 @@ +variables[$name] = $value; + } + + public function __get($name) + { + 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 __construct($data = array()) { + $this->db = new DB(); + $this->variables = $data; + } + + 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); + } +} +?> \ No newline at end of file diff --git a/easyCRUD/index.php b/easyCRUD/index.php new file mode 100644 index 0000000..23c4e8b --- /dev/null +++ b/easyCRUD/index.php @@ -0,0 +1,39 @@ +"kona","Age"=>"20","Sex"=>"F")); + //$person = new Person(array("id"=>"67")); + +// Create new person +// $person->Firstname = "Kona"; +// $person->Age = "20"; +// $person->Sex = "F"; +// $creation = $person->Create(); + + //var_dump($creation); + +// Update Person Info +// $person->id = "4"; +// $person->Age = "32"; +// $saved = $person->Save(); + +// var_dump($saved); + +// Find person + //$person->id = "4"; + //$person->Find(); + +// echo $person->Firstname; +// echo $person->Age; + +// Delete person +// $person->id = "17"; + $delete = $person->Delete(); + var_dump($delete); + + // Get all persons + $persons = $person->all(); + + +?> \ No newline at end of file diff --git a/settings.ini.php b/settings.ini.php new file mode 100644 index 0000000..d0472a7 --- /dev/null +++ b/settings.ini.php @@ -0,0 +1,5 @@ +[SQL] +host = localhost +user = root +password = +dbname = ultim \ No newline at end of file -- cgit v1.1 From e125147d78a57a39725b7704082614779cf735a2 Mon Sep 17 00:00:00 2001 From: Indieteq Date: Wed, 17 Oct 2012 22:42:03 +0300 Subject: Update Db.class.php Fixed the tab spacing --- Db.class.php | 38 +++++++++++++++++++------------------- 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/Db.class.php b/Db.class.php index 0798c72..469e2e9 100644 --- a/Db.class.php +++ b/Db.class.php @@ -1,12 +1,12 @@ parameters = array(); } - /** + /** * This method makes connection to the database. * - * 1. Reads the database settings from a ini file. - * 2. Puts the ini content into the settings array. + * 1. Reads the database settings from a ini file. + * 2. Puts the ini content into the settings array. * 3. Tries to connect to the database. * 4. If connection failed, exception is displayed and a log file gets created. */ @@ -75,7 +75,7 @@ die(); } } - /** + /** * Every method which needs to execute a SQL query uses this method. * * 1. If not connected, connect to the database. @@ -119,7 +119,7 @@ $this->parameters = array(); } - /** + /** * @void * * Add the parameter to the parameter array @@ -130,7 +130,7 @@ { $this->parameters[sizeof($this->parameters)] = ":" . $para . "\x7F" . $value; } - /** + /** * @void * * Add more parameters to the parameter array @@ -170,7 +170,7 @@ return NULL; } } - /** + /** * Returns an array which represents a column from the result set * * @param string $query @@ -191,7 +191,7 @@ return $column; } - /** + /** * Returns an array which represents a row from the result set * * @param string $query @@ -204,7 +204,7 @@ $this->Init($query,$params); return $this->sQuery->fetch($fetchmode); } - /** + /** * Returns the value of one single field/column * * @param string $query @@ -216,7 +216,7 @@ $this->Init($query,$params); return $this->sQuery->fetchColumn(); } - /** + /** * Writes the log and returns the exception * * @param string $message -- cgit v1.1 From 78987303cc0ee23a822cbcb97816b6c50decf9c6 Mon Sep 17 00:00:00 2001 From: Indieteq Date: Wed, 17 Oct 2012 22:50:15 +0300 Subject: Update Db.class.php --- Db.class.php | 52 ++++++++++++++++++++++++++-------------------------- 1 file changed, 26 insertions(+), 26 deletions(-) diff --git a/Db.class.php b/Db.class.php index 469e2e9..4fe87ad 100644 --- a/Db.class.php +++ b/Db.class.php @@ -28,7 +28,7 @@ # @array, The parameters of the SQL query private $parameters; - /** + /** * Default Constructor * * 1. Instantiate Log class. @@ -42,13 +42,13 @@ $this->parameters = array(); } - /** - * This method makes connection to the database. - * - * 1. Reads the database settings from a ini file. - * 2. Puts the ini content into the settings array. + /** + * This method makes connection to the database. + * + * 1. Reads the database settings from a ini file. + * 2. Puts the ini content into the settings array. * 3. Tries to connect to the database. - * 4. If connection failed, exception is displayed and a log file gets created. + * 4. If connection failed, exception is displayed and a log file gets created. */ private function Connect() { @@ -75,7 +75,7 @@ die(); } } - /** + /** * Every method which needs to execute a SQL query uses this method. * * 1. If not connected, connect to the database. @@ -119,7 +119,7 @@ $this->parameters = array(); } - /** + /** * @void * * Add the parameter to the parameter array @@ -130,7 +130,7 @@ { $this->parameters[sizeof($this->parameters)] = ":" . $para . "\x7F" . $value; } - /** + /** * @void * * Add more parameters to the parameter array @@ -145,13 +145,13 @@ } } } - /** - * If the SQL query contains a SELECT statement it returns an array containing all of the result set row - * If the SQL statement is a DELETE, INSERT, or UPDATE statement it returns the number of affected rows - * - * @param string $query - * @param array $params - * @param int $fetchmode + /** + * If the SQL query contains a SELECT statement it returns an array containing all of the result set row + * If the SQL statement is a DELETE, INSERT, or UPDATE statement it returns the number of affected rows + * + * @param string $query + * @param array $params + * @param int $fetchmode * @return mixed */ public function query($query,$params = null,$fetchmode = PDO::FETCH_ASSOC) @@ -170,7 +170,7 @@ return NULL; } } - /** + /** * Returns an array which represents a column from the result set * * @param string $query @@ -191,7 +191,7 @@ return $column; } - /** + /** * Returns an array which represents a row from the result set * * @param string $query @@ -204,7 +204,7 @@ $this->Init($query,$params); return $this->sQuery->fetch($fetchmode); } - /** + /** * Returns the value of one single field/column * * @param string $query @@ -217,12 +217,12 @@ return $this->sQuery->fetchColumn(); } /** - * Writes the log and returns the exception - * - * @param string $message - * @param string $sql - * @return string - */ + * Writes the log and returns the exception + * + * @param string $message + * @param string $sql + * @return string + */ private function ExceptionLog($message , $sql = "") { $exception = 'Unhandled Exception.
'; -- cgit v1.1 From 6a35925789bd20e09376872c7710dbb12577a1b0 Mon Sep 17 00:00:00 2001 From: Indieteq Date: Wed, 17 Oct 2012 22:50:54 +0300 Subject: Update Db.class.php --- Db.class.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Db.class.php b/Db.class.php index 4fe87ad..3300499 100644 --- a/Db.class.php +++ b/Db.class.php @@ -4,7 +4,7 @@ * * @author Author: Vivek Wicky Aswal. (https://twitter.com/#!/VivekWickyAswal) * @git htt://github.com/indieteq-vivek/simple-db-class -* @version 0.2a +* @version 0.2ab * */ require("Log.class.php"); -- cgit v1.1 From 56e9be13da22a65ebb777820648ce30e26c056ce Mon Sep 17 00:00:00 2001 From: Indieteq Date: Sat, 10 Nov 2012 20:19:13 +0100 Subject: Update easyCRUD/easyCRUD.class.php --- easyCRUD/easyCRUD.class.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/easyCRUD/easyCRUD.class.php b/easyCRUD/easyCRUD.class.php index 3e4c647..f369197 100644 --- a/easyCRUD/easyCRUD.class.php +++ b/easyCRUD/easyCRUD.class.php @@ -1,6 +1,6 @@ Date: Sat, 10 Nov 2012 20:20:53 +0100 Subject: Update easyCRUD/index.php --- easyCRUD/index.php | 35 ++++++++++++++--------------------- 1 file changed, 14 insertions(+), 21 deletions(-) diff --git a/easyCRUD/index.php b/easyCRUD/index.php index 23c4e8b..65891df 100644 --- a/easyCRUD/index.php +++ b/easyCRUD/index.php @@ -1,39 +1,32 @@ "kona","Age"=>"20","Sex"=>"F")); - //$person = new Person(array("id"=>"67")); +// Our person object + $person = new Person(); // Create new person -// $person->Firstname = "Kona"; -// $person->Age = "20"; -// $person->Sex = "F"; -// $creation = $person->Create(); + $person->Firstname = "Kona"; + $person->Age = "20"; + $person->Sex = "F"; + $creation = $person->Create(); - //var_dump($creation); // Update Person Info -// $person->id = "4"; -// $person->Age = "32"; -// $saved = $person->Save(); - -// var_dump($saved); + $person->id = "4"; + $person->Age = "32"; + $saved = $person->Save(); // Find person - //$person->id = "4"; - //$person->Find(); + $person->id = "4"; + $person->Find(); -// echo $person->Firstname; -// echo $person->Age; + echo $person->Firstname; + echo $person->Age; // Delete person -// $person->id = "17"; + $person->id = "17"; $delete = $person->Delete(); - var_dump($delete); // Get all persons $persons = $person->all(); - - ?> \ No newline at end of file -- cgit v1.1 From 9cf38545dc942b1cd030fcae5ba1b6366af1e805 Mon Sep 17 00:00:00 2001 From: Indieteq Date: Sat, 10 Nov 2012 20:22:40 +0100 Subject: Update Db.class.php --- Db.class.php | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/Db.class.php b/Db.class.php index 3300499..6ff03bd 100644 --- a/Db.class.php +++ b/Db.class.php @@ -1,12 +1,12 @@ Date: Sat, 10 Nov 2012 20:23:57 +0100 Subject: Update Log.class.php --- Log.class.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Log.class.php b/Log.class.php index 58e5672..204fbe5 100644 --- a/Log.class.php +++ b/Log.class.php @@ -7,7 +7,7 @@ */ class Log { - # @string, Log directory name + # @string, Log directory name private $path = '/logs/'; # @void, Default Constructor, Sets the timezone and path of the log files. -- cgit v1.1 From 702e2a3037afd1745bfeb85bfae703df28c314aa Mon Sep 17 00:00:00 2001 From: Indieteq Date: Sat, 10 Nov 2012 20:25:11 +0100 Subject: Update Log.class.php --- Log.class.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/Log.class.php b/Log.class.php index 204fbe5..3e0d1c0 100644 --- a/Log.class.php +++ b/Log.class.php @@ -5,12 +5,12 @@ * @git htt://github.com/indieteq-vivek/simple-db-class * @version 0.1a */ - class Log { + class Log { - # @string, Log directory name - private $path = '/logs/'; + # @string, Log directory name + private $path = '/logs/'; - # @void, Default Constructor, Sets the timezone and path of the log files. + # @void, Default Constructor, Sets the timezone and path of the log files. public function __construct() { date_default_timezone_set('Europe/Amsterdam'); $this->path = dirname(__FILE__) . $this->path; @@ -32,7 +32,7 @@ public function write($message) { $date = new DateTime(); $log = $this->path . $date->format('Y-m-d').".txt"; - + if(is_dir($this->path)) { if(!file_exists($log)) { $fh = fopen($log, 'w') or die("Fatal Error !"); @@ -47,7 +47,7 @@ else { if(mkdir($this->path,0777) === true) // Check on true, Otherwise we would have a never-ending loop :S { - $this->write($message); + $this->write($message); } } } -- cgit v1.1 From b04cfd3daf8b547b22bdd7744e17a16ae7bd79ac Mon Sep 17 00:00:00 2001 From: Indieteq Date: Sat, 10 Nov 2012 20:26:44 +0100 Subject: Update Log.class.php --- Log.class.php | 76 +++++++++++++++++++++++++++++------------------------------ 1 file changed, 38 insertions(+), 38 deletions(-) diff --git a/Log.class.php b/Log.class.php index 3e0d1c0..ec291cb 100644 --- a/Log.class.php +++ b/Log.class.php @@ -1,5 +1,5 @@ path = dirname(__FILE__) . $this->path; - } + # @void, Default Constructor, Sets the timezone and path of the log files. + public function __construct() { + date_default_timezone_set('Europe/Amsterdam'); + $this->path = dirname(__FILE__) . $this->path; + } /** * @void * Creates the log * - * @param string $message the message which is written into the log. - * @description: - * 1. Checks if directory exists, if not, create one and call this method again. - * 2. Checks if log already exists. - * 3. If not, new log gets created. Log is written into the logs folder. - * 4. Logname is current date(Year - Month - Day). - * 5. If log exists, edit method called. - * 6. Edit method modifies the current log. - */ - public function write($message) { - $date = new DateTime(); - $log = $this->path . $date->format('Y-m-d').".txt"; - - if(is_dir($this->path)) { - if(!file_exists($log)) { - $fh = fopen($log, 'w') or die("Fatal Error !"); - $logcontent = "Time : " . $date->format('H:i:s')."\r\n" . $message ."\r\n"; - fwrite($fh, $logcontent); - fclose($fh); - } - else { - $this->edit($log,$date, $message); - } + * @param string $message the message which is written into the log. + * @description: + * 1. Checks if directory exists, if not, create one and call this method again. + * 2. Checks if log already exists. + * 3. If not, new log gets created. Log is written into the logs folder. + * 4. Logname is current date(Year - Month - Day). + * 5. If log exists, edit method called. + * 6. Edit method modifies the current log. + */ + public function write($message) { + $date = new DateTime(); + $log = $this->path . $date->format('Y-m-d').".txt"; + + if(is_dir($this->path)) { + if(!file_exists($log)) { + $fh = fopen($log, 'w') or die("Fatal Error !"); + $logcontent = "Time : " . $date->format('H:i:s')."\r\n" . $message ."\r\n"; + fwrite($fh, $logcontent); + fclose($fh); } else { - if(mkdir($this->path,0777) === true) // Check on true, Otherwise we would have a never-ending loop :S - { - $this->write($message); - } + $this->edit($log,$date, $message); } } + else { + if(mkdir($this->path,0777) === true) // Check on true, Otherwise we would have a never-ending loop :S + { + $this->write($message); + } + } + } /** * @void @@ -61,10 +61,10 @@ * @param DateTimeObject $date * @param string $message */ - private function edit($log,$date,$message) { - $logcontent = "Time : " . $date->format('H:i:s')."\r\n" . $message ."\r\n\r\n"; - $logcontent = $logcontent . file_get_contents($log); - file_put_contents($log, $logcontent); - } + private function edit($log,$date,$message) { + $logcontent = "Time : " . $date->format('H:i:s')."\r\n" . $message ."\r\n\r\n"; + $logcontent = $logcontent . file_get_contents($log); + file_put_contents($log, $logcontent); + } } ?> \ No newline at end of file -- cgit v1.1 From 30a9b147cf7c1632753a66e201dc121f2ae77146 Mon Sep 17 00:00:00 2001 From: Indieteq Date: Sat, 10 Nov 2012 20:30:25 +0100 Subject: Update Db.class.php --- Db.class.php | 420 +++++++++++++++++++++++++++++------------------------------ 1 file changed, 210 insertions(+), 210 deletions(-) diff --git a/Db.class.php b/Db.class.php index 6ff03bd..f518e0f 100644 --- a/Db.class.php +++ b/Db.class.php @@ -7,236 +7,236 @@ * @version 0.2ab * */ - require("Log.class.php"); - class DB - { - # @object, The PDO object - private $pdo; +require("Log.class.php"); +class DB +{ + # @object, The PDO object + private $pdo; - # @object, PDO statement object - private $sQuery; + # @object, PDO statement object + private $sQuery; - # @array, The database settings - private $settings; + # @array, The database settings + private $settings; - # @bool , Connected to the database - private $bConnected = false; + # @bool , Connected to the database + private $bConnected = false; - # @object, Object for logging exceptions - private $log; + # @object, Object for logging exceptions + private $log; - # @array, The parameters of the SQL query - private $parameters; - - /** - * Default Constructor - * - * 1. Instantiate Log class. - * 2. Connect to database. - * 3. Creates the parameter array. - */ - public function __construct() - { - $this->log = new Log(); - $this->Connect(); - $this->parameters = array(); - } - - /** - * This method makes connection to the database. - * - * 1. Reads the database settings from a ini file. - * 2. Puts the ini content into the settings array. - * 3. Tries to connect to the database. - * 4. If connection failed, exception is displayed and a log file gets created. - */ - private function Connect() + # @array, The parameters of the SQL query + private $parameters; + + /** + * Default Constructor + * + * 1. Instantiate Log class. + * 2. Connect to database. + * 3. Creates the parameter array. + */ + public function __construct() + { + $this->log = new Log(); + $this->Connect(); + $this->parameters = array(); + } + + /** + * This method makes connection to the database. + * + * 1. Reads the database settings from a ini file. + * 2. Puts the ini content into the settings array. + * 3. Tries to connect to the database. + * 4. If connection failed, exception is displayed and a log file gets created. + */ + private function Connect() + { + $this->settings = parse_ini_file("settings.ini.php"); + $dsn = 'mysql:dbname='.$this->settings["dbname"].';host='.$this->settings["host"].''; + try { - $this->settings = parse_ini_file("settings.ini.php"); - $dsn = 'mysql:dbname='.$this->settings["dbname"].';host='.$this->settings["host"].''; - try - { - # Read settings from INI file - $this->pdo = new PDO($dsn, $this->settings["user"], $this->settings["password"]); - - # We can now log any exceptions on Fatal error. - $this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); - - # Disable emulation of prepared statements, use REAL prepared statements instead. - $this->pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false); - - # Connection succeeded, set the boolean to true. - $this->bConnected = true; - } - catch (PDOException $e) - { - # Write into log - echo $this->ExceptionLog($e->getMessage()); - die(); - } + # Read settings from INI file + $this->pdo = new PDO($dsn, $this->settings["user"], $this->settings["password"]); + + # We can now log any exceptions on Fatal error. + $this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); + + # Disable emulation of prepared statements, use REAL prepared statements instead. + $this->pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false); + + # Connection succeeded, set the boolean to true. + $this->bConnected = true; } - /** - * Every method which needs to execute a SQL query uses this method. - * - * 1. If not connected, connect to the database. - * 2. Prepare Query. - * 3. Parameterize Query. - * 4. Execute Query. - * 5. On exception : Write Exception into the log + SQL query. - * 6. Reset the Parameters. - */ - private function Init($query,$parameters = "") + catch (PDOException $e) { - # Connect to database - if(!$this->bConnected) { $this->Connect(); } - try { - # Prepare query - $this->sQuery = $this->pdo->prepare($query); - - # Add parameters to the parameter array - $this->bindMore($parameters); - - # Bind parameters - if(!empty($this->parameters)) { - foreach($this->parameters as $param) - { - $parameters = explode("\x7F",$param); - $this->sQuery->bindParam($parameters[0],$parameters[1]); - } - } + # Write into log + echo $this->ExceptionLog($e->getMessage()); + die(); + } + } + /** + * Every method which needs to execute a SQL query uses this method. + * + * 1. If not connected, connect to the database. + * 2. Prepare Query. + * 3. Parameterize Query. + * 4. Execute Query. + * 5. On exception : Write Exception into the log + SQL query. + * 6. Reset the Parameters. + */ + private function Init($query,$parameters = "") + { + # Connect to database + if(!$this->bConnected) { $this->Connect(); } + try { + # Prepare query + $this->sQuery = $this->pdo->prepare($query); + + # Add parameters to the parameter array + $this->bindMore($parameters); - # Execute SQL - $this->succes = $this->sQuery->execute(); - } - catch(PDOException $e) - { - # Write into log and display Exception - echo $this->ExceptionLog($e->getMessage(),$this->sQuery->queryString); - die(); - } + # Bind parameters + if(!empty($this->parameters)) { + foreach($this->parameters as $param) + { + $parameters = explode("\x7F",$param); + $this->sQuery->bindParam($parameters[0],$parameters[1]); + } + } - # Reset the parameters - $this->parameters = array(); + # Execute SQL + $this->succes = $this->sQuery->execute(); } - - /** - * @void - * - * Add the parameter to the parameter array - * @param string $para - * @param string $value - */ - public function bind($para, $value) - { - $this->parameters[sizeof($this->parameters)] = ":" . $para . "\x7F" . $value; - } - /** - * @void - * - * Add more parameters to the parameter array - * @param array $parray - */ - public function bindMore($parray) + catch(PDOException $e) { - if(empty($this->parameters) && is_array($parray)) { - $columns = array_keys($parray); - foreach($columns as $i => &$column) { - $this->bind($column, $parray[$column]); - } - } + # Write into log and display Exception + echo $this->ExceptionLog($e->getMessage(),$this->sQuery->queryString); + die(); } - /** - * If the SQL query contains a SELECT statement it returns an array containing all of the result set row - * If the SQL statement is a DELETE, INSERT, or UPDATE statement it returns the number of affected rows - * - * @param string $query - * @param array $params - * @param int $fetchmode - * @return mixed - */ - public function query($query,$params = null,$fetchmode = PDO::FETCH_ASSOC) - { - $query = trim($query); - - $this->Init($query,$params); - if (stripos($query, 'select') === 0){ - return $this->sQuery->fetchAll($fetchmode); - } - elseif (stripos($query, 'insert') === 0 || stripos($query, 'update') === 0 || stripos($query, 'delete') === 0) { - return $this->sQuery->rowCount(); - } - else { - return NULL; + # Reset the parameters + $this->parameters = array(); + } + + /** + * @void + * + * Add the parameter to the parameter array + * @param string $para + * @param string $value + */ + public function bind($para, $value) + { + $this->parameters[sizeof($this->parameters)] = ":" . $para . "\x7F" . $value; + } + /** + * @void + * + * Add more parameters to the parameter array + * @param array $parray + */ + public function bindMore($parray) + { + if(empty($this->parameters) && is_array($parray)) { + $columns = array_keys($parray); + foreach($columns as $i => &$column) { + $this->bind($column, $parray[$column]); } - } - /** - * Returns an array which represents a column from the result set - * - * @param string $query - * @param array $params - * @return array - */ - public function column($query,$params = null) - { - $this->Init($query,$params); - $Columns = $this->sQuery->fetchAll(PDO::FETCH_NUM); - - $column = null; + } + } + /** + * If the SQL query contains a SELECT statement it returns an array containing all of the result set row + * If the SQL statement is a DELETE, INSERT, or UPDATE statement it returns the number of affected rows + * + * @param string $query + * @param array $params + * @param int $fetchmode + * @return mixed + */ + public function query($query,$params = null,$fetchmode = PDO::FETCH_ASSOC) + { + $query = trim($query); - foreach($Columns as $cells) { - $column[] = $cells[0]; - } + $this->Init($query,$params); - return $column; - + if (stripos($query, 'select') === 0){ + return $this->sQuery->fetchAll($fetchmode); + } + elseif (stripos($query, 'insert') === 0 || stripos($query, 'update') === 0 || stripos($query, 'delete') === 0) { + return $this->sQuery->rowCount(); } - /** - * Returns an array which represents a row from the result set - * - * @param string $query - * @param array $params - * @param int $fetchmode - * @return array - */ - public function row($query,$params = null,$fetchmode = PDO::FETCH_ASSOC) - { - $this->Init($query,$params); - return $this->sQuery->fetch($fetchmode); + else { + return NULL; } - /** - * Returns the value of one single field/column - * - * @param string $query - * @param array $params - * @return string - */ - public function single($query,$params = null) - { - $this->Init($query,$params); - return $this->sQuery->fetchColumn(); + } + /** + * Returns an array which represents a column from the result set + * + * @param string $query + * @param array $params + * @return array + */ + public function column($query,$params = null) + { + $this->Init($query,$params); + $Columns = $this->sQuery->fetchAll(PDO::FETCH_NUM); + + $column = null; + + foreach($Columns as $cells) { + $column[] = $cells[0]; } - /** - * Writes the log and returns the exception - * - * @param string $message - * @param string $sql - * @return string - */ - private function ExceptionLog($message , $sql = "") - { - $exception = 'Unhandled Exception.
'; - $exception .= $message; - $exception .= "
You can find the error back in the log."; - if(!empty($sql)) { - # Add the Raw SQL to the Log - $message .= "\r\nRaw SQL : " . $sql; - } - # Write into log - $this->log->write($message); + return $column; + + } + /** + * Returns an array which represents a row from the result set + * + * @param string $query + * @param array $params + * @param int $fetchmode + * @return array + */ + public function row($query,$params = null,$fetchmode = PDO::FETCH_ASSOC) + { + $this->Init($query,$params); + return $this->sQuery->fetch($fetchmode); + } + /** + * Returns the value of one single field/column + * + * @param string $query + * @param array $params + * @return string + */ + public function single($query,$params = null) + { + $this->Init($query,$params); + return $this->sQuery->fetchColumn(); + } + /** + * Writes the log and returns the exception + * + * @param string $message + * @param string $sql + * @return string + */ + private function ExceptionLog($message , $sql = "") + { + $exception = 'Unhandled Exception.
'; + $exception .= $message; + $exception .= "
You can find the error back in the log."; + + if(!empty($sql)) { + # Add the Raw SQL to the Log + $message .= "\r\nRaw SQL : " . $sql; + } + # Write into log + $this->log->write($message); - return $exception; - } - } + return $exception; + } +} ?> \ No newline at end of file -- cgit v1.1 From 08b7d76cf8e671a4acec579baf4eecad40671a9a Mon Sep 17 00:00:00 2001 From: Indieteq Date: Sat, 10 Nov 2012 20:31:23 +0100 Subject: Update Db.class.php --- Db.class.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Db.class.php b/Db.class.php index f518e0f..1eb616e 100644 --- a/Db.class.php +++ b/Db.class.php @@ -3,7 +3,7 @@ * DB - A simple database class * * @author Author: Vivek Wicky Aswal. (https://twitter.com/#!/VivekWickyAswal) - * @git htt://github.com/indieteq-vivek/simple-db-class + * @git htt://github.com/indieteq-vivek/simple-db-class * @version 0.2ab * */ -- cgit v1.1 From 6691618d2b2b8c93790938e80ae94c503610950f Mon Sep 17 00:00:00 2001 From: Indieteq Date: Mon, 12 Nov 2012 14:02:42 +0100 Subject: Update Log.class.php --- Log.class.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Log.class.php b/Log.class.php index ec291cb..07ccd04 100644 --- a/Log.class.php +++ b/Log.class.php @@ -35,7 +35,7 @@ if(is_dir($this->path)) { if(!file_exists($log)) { - $fh = fopen($log, 'w') or die("Fatal Error !"); + $fh = fopen($log, 'a+') or die("Fatal Error !"); $logcontent = "Time : " . $date->format('H:i:s')."\r\n" . $message ."\r\n"; fwrite($fh, $logcontent); fclose($fh); @@ -45,7 +45,7 @@ } } else { - if(mkdir($this->path,0777) === true) // Check on true, Otherwise we would have a never-ending loop :S + if(mkdir($this->path,0777) === true) { $this->write($message); } -- cgit v1.1 From 6b98954b8040ec4cae63a2be9d79ac9bc18cd903 Mon Sep 17 00:00:00 2001 From: Indieteq Date: Mon, 12 Nov 2012 14:17:05 +0100 Subject: SQL dump and Example PHP file --- SQL dump/testdb.sql | 38 ++++++++++++++++++++++++++++++++++++++ index.example.php | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 85 insertions(+) create mode 100644 SQL dump/testdb.sql create mode 100644 index.example.php diff --git a/SQL dump/testdb.sql b/SQL dump/testdb.sql new file mode 100644 index 0000000..eadf3fb --- /dev/null +++ b/SQL dump/testdb.sql @@ -0,0 +1,38 @@ +/* +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/index.example.php b/index.example.php new file mode 100644 index 0000000..71a78e0 --- /dev/null +++ b/index.example.php @@ -0,0 +1,47 @@ +bind("firstname","John"); + $db->bind("age","19"); + + // 2. Bind more parameters + $db->bindMore(array("firstname"=>"John","age"=>"19")); + + // 3. Or just give the parameters to the method + $db->query("SELECT * FROM Persons WHERE firstname = :firstname AND age = :age", array("firstname"=>"John","age"=>"19")); + + // Fetching data + $person = $db->query("SELECT * FROM Persons"); + + // If you want another fetchmode just give it as parameter + $persons_num = $db->query("SELECT * FROM Persons", null, PDO::FETCH_NUM); + + // Fetching single value + $firstname = $db->single("SELECT firstname FROM Persons WHERE Id = :id ", array('id' => '31' ) ); + + // Single Row + $id_age = $db->row("SELECT Id, Age FROM Persons WHERE firstname = :f", array("f"=>"John")); + + // Single Row with numeric index + $id_age_num = $db->row("SELECT Id, Age FROM Persons WHERE firstname = :f", array("f"=>"John"),PDO::FETCH_NUM); + + // Column, numeric index + $ages = $db->column("SELECT age FROM Persons"); + + // The following statemens will return the affected rows + + // Update statement + $update = $db->query("UPDATE Persons SET firstname = :f WHERE Id = :id",array("f"=>"Johny","id"=>"1")); + + // Insert statement +// $insert = $db->query("INSERT INTO Persons(Firstname,Age) VALUES(:f,:age)",array("f"=>"Vivek","age"=>"20")); + + // Delete statement +// $delete = $db->query("DELETE FROM Persons WHERE Id = :id",array("id"=>"6")); +?> \ No newline at end of file -- cgit v1.1 From fb97eae3e72a0f436137861ee2a1e6b85fe677ca Mon Sep 17 00:00:00 2001 From: Indieteq Date: Mon, 12 Nov 2012 14:21:44 +0100 Subject: Update easyCRUD/index.php --- easyCRUD/index.php | 44 ++++++++++++++++++++++---------------------- 1 file changed, 22 insertions(+), 22 deletions(-) diff --git a/easyCRUD/index.php b/easyCRUD/index.php index 65891df..e77c04e 100644 --- a/easyCRUD/index.php +++ b/easyCRUD/index.php @@ -1,32 +1,32 @@ -Firstname = "Kona"; - $person->Age = "20"; - $person->Sex = "F"; - $creation = $person->Create(); +// 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(); -// Update Person Info - $person->id = "4"; - $person->Age = "32"; - $saved = $person->Save(); +// Find person + $person->id = "4"; + $person->Find(); -// Find person - $person->id = "4"; - $person->Find(); - - echo $person->Firstname; - echo $person->Age; + echo $person->Firstname; + echo $person->Age; // Delete person - $person->id = "17"; - $delete = $person->Delete(); + $person->id = "17"; + $delete = $person->Delete(); // Get all persons - $persons = $person->all(); + $persons = $person->all(); ?> \ No newline at end of file -- cgit v1.1 From 02bc2b4664080b1115f7202edebc0077b053baba Mon Sep 17 00:00:00 2001 From: Indieteq Date: Tue, 13 Nov 2012 20:08:56 +0100 Subject: Added some usefull info --- README.md | 200 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 198 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 5a24453..aef3fb6 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,200 @@ -PHP-MySQL-PDO-Database-Class +PDO Database Class ============================ -A database class for PHP MySQL which makes use of PDO . \ No newline at end of file +A database class for PHP-MySQL which uses the PDO extension. + +## Feature overview +- Simple fetching +- Logger class + +## To use the class +#### 1. Edit the database settings in the settings.ini.php +``` +[SQL] +host = localhost +user = root +password = +dbname = yourdatabase +``` +#### 2. Require the class in your project +```php +query("SELECT * FROM persons"); +``` +#### Fetching with Bindings: +There are three different ways to bind parameters. +```php +bind("id","1"); +$db->bind("firstname","John"); +$person = $db->query("SELECT * FROM Persons WHERE firstname = :firstname AND id = :id"); + +// 2. Bind more parameters +$db->bindMore(array("firstname"=>"John","id"=>"1")); +$person = $db->query("SELECT * FROM Persons WHERE firstname = :firstname AND id = :id")); + +// 3. Or just give the parameters to the method +$person = $db->query("SELECT * FROM Persons WHERE firstname = :firstname",array("firstname"=>"John","id"=>"1")); +``` +#### Fetching Row: +This method always returns only 1 row. +```php +row("SELECT * FROM Persons"); +``` +#### Fetching Single Value: +This method returns only one single value of a record. +```php +bind("id","3"); +$firstname = $db->single("SELECT firstname FROM Persons WHERE id = :id"); + +echo $firstname; +// Wicky +``` +#### Fetching Column: +```php +column("SELECT Firstname FROM Persons"); +``` +#### Delete a record: +```php +query("DELETE FROM Persons WHERE Id = :id", array("id"=>"1")); +``` +#### Update a record: +```php +query("UPDATE Persons SET firstname = :f WHERE Id = :id", array("f"=>"Jan","id"=>"32")); +``` +#### Insert a record: +```php +query("INSERT INTO Persons(Firstname,Age) VALUES(:f,:age)", array("f"=>"Vivek","age"=>"20")); +``` +EasyCRUD +============================ +The easyCRUD is a class which you can use to easily execute basic SQL operations like(insert, update, select, delete) on your database. + +It uses the database class I've created to execute the SQL queries. + +## How to use easyCRUD +#### 1. First, Create a new class. Then require the easyCRUD class. +#### 2. Extend your class and add the following fields to the class. +```php +Firstname = "Kona"; +$person->Age = "20"; +$person->Sex = "F"; +$created = $person->Create(); + +// Or give the bindings to the constructor +$person = new person(array("Firstname"=>"Kona","age"=>"20","sex"=>"F")); +$created = person->Create(); + +// SQL Equivalent +"INSERT INTO persons (Firstname,Age,Sex) VALUES ('Kona','20','F')" +``` +#### Deleting a record +```php +Id = "17"; +$deleted = $person->Delete(); + +// Shorthand method, give id as parameter +$deleted = $person->Delete(17); + +// SQL Equivalent +"DELETE FROM persons WHERE Id = 17 LIMIT 1" +``` +#### Saving person's data +```php +Firstname = "Nhoj"; +$person->Age = "20"; +$person->Sex = "F"; +$person->Id = "4"; +$saved = $person->Save(); + +// Or give the bindings to the constructor +$person = new person(array("Firstname"=>"John","age"=>"20","sex"=>"F","Id"=>"4")); +$saved = $person->Save(); + +// SQL Equivalent +"UPDATE persons SET Firstname = 'John',Age = 20, Sex = 'F' WHERE Id= 4" +``` +#### Finding a person +```php +Id = "1"; +$person = $person->Find(); + +// Shorthand method, give id as parameter +$person = $person->Find(1); + +// SQL Equivalent +"SELECT * FROM persons WHERE Id= 1" +``` +#### Getting all the persons +```php +all(); + +// SQL Equivalent +"SELECT * FROM persons +``` \ No newline at end of file -- cgit v1.1 From 39f97f683cd1a15ce88f11de636c03589a70a3df Mon Sep 17 00:00:00 2001 From: Indieteq Date: Tue, 13 Nov 2012 20:46:43 +0100 Subject: Update README.md --- README.md | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/README.md b/README.md index aef3fb6..7452f4d 100644 --- a/README.md +++ b/README.md @@ -37,6 +37,14 @@ If you want to use these files you''ll have to modify the rights of the logs fol ## Examples +| Id | Firstname | Lastname | Sex | Age +|:-----------:|:------------:|:------------:|:------------:|:------------:| +| 1 | John | Doe | M | 19 +| 2 | Bob | Black | M | 41 +| 3 | Zoe | Chan | F | 20 +| 4 | Kona | Khan | M | 14 +| 5 | Kader| Khan | M | 56 + #### Fetching from the database: ```php Date: Tue, 13 Nov 2012 21:44:19 +0100 Subject: Update README.md --- README.md | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 7452f4d..fa0aed7 100644 --- a/README.md +++ b/README.md @@ -37,7 +37,8 @@ If you want to use these files you''ll have to modify the rights of the logs fol ## Examples -| Id | Firstname | Lastname | Sex | Age +#### The table persons, +| id | Firstname | Lastname | Sex | Age |:-----------:|:------------:|:------------:|:------------:|:------------:| | 1 | John | Doe | M | 19 | 2 | Bob | Black | M | 41 @@ -45,7 +46,7 @@ If you want to use these files you''ll have to modify the rights of the logs fol | 4 | Kona | Khan | M | 14 | 5 | Kader| Khan | M | 56 -#### Fetching from the database: +#### Fetching everything from the table ```php row("SELECT * FROM Persons"); +$ages = $db->row("SELECT * FROM Persons WHERE id = :id", array("id"=>"1")); ``` #### Fetching Single Value: This method returns only one single value of a record. @@ -83,7 +84,7 @@ $db->bind("id","3"); $firstname = $db->single("SELECT firstname FROM Persons WHERE id = :id"); echo $firstname; -// Wicky +// Zoe ``` #### Fetching Column: ```php @@ -116,7 +117,7 @@ The easyCRUD is a class which you can use to easily execute basic SQL operations It uses the database class I've created to execute the SQL queries. ## How to use easyCRUD -#### 1. First, Create a new class. Then require the easyCRUD class. +#### 1. First, create a new class. Then require the easyCRUD class. #### 2. Extend your class and add the following fields to the class. ```php Date: Tue, 13 Nov 2012 22:13:23 +0100 Subject: Update README.md --- README.md | 63 +++++++++++++++++++++++++++++++++++++++------------------------ 1 file changed, 39 insertions(+), 24 deletions(-) diff --git a/README.md b/README.md index fa0aed7..7d6a8bc 100644 --- a/README.md +++ b/README.md @@ -38,7 +38,7 @@ If you want to use these files you''ll have to modify the rights of the logs fol ## Examples #### The table persons, -| id | Firstname | Lastname | Sex | Age +| id | firstname | lastname | sex | age |:-----------:|:------------:|:------------:|:------------:|:------------:| | 1 | John | Doe | M | 19 | 2 | Bob | Black | M | 41 @@ -66,15 +66,19 @@ $db->bindMore(array("firstname"=>"John","id"=>"1")); $person = $db->query("SELECT * FROM Persons WHERE firstname = :firstname AND id = :id")); // 3. Or just give the parameters to the method -$person = $db->query("SELECT * FROM Persons WHERE firstname = :firstname",array("firstname"=>"John","id"=>"1")); +$person = $db->query("SELECT * FROM Persons WHERE firstname = :firstname",array("firstname"=>"John","id"=>"1")); ``` #### Fetching Row: This method always returns only 1 row. ```php row("SELECT * FROM Persons WHERE id = :id", array("id"=>"1")); +$ages = $db->row("SELECT * FROM Persons WHERE id = :id", array("id"=>"1")); ``` +##### Result +| id | firstname | lastname | sex | age +|:-----------:|:------------:|:------------:|:------------:|:------------:| +| 1 | John | Doe | M | 19 #### Fetching Single Value: This method returns only one single value of a record. ```php @@ -82,33 +86,44 @@ This method returns only one single value of a record. // Fetch one single value $db->bind("id","3"); $firstname = $db->single("SELECT firstname FROM Persons WHERE id = :id"); - -echo $firstname; -// Zoe ``` +##### Result +|firstname +|:------------: +| Zoe #### Fetching Column: ```php column("SELECT Firstname FROM Persons"); ``` -#### Delete a record: +##### Result +|firstname | +|:-----------: +| John +| Bob +| Zoe +| Kona +| Kader +### Delete / Update / Insert +When executing the delete, update, or insert statement by using the query method the affected rows will be returned. ```php query("DELETE FROM Persons WHERE Id = :id", array("id"=>"1")); -``` -#### Update a record: -```php -query("UPDATE Persons SET firstname = :f WHERE Id = :id", array("f"=>"Jan","id"=>"32")); -``` -#### Insert a record: -```php -query("INSERT INTO Persons(Firstname,Age) VALUES(:f,:age)", array("f"=>"Vivek","age"=>"20")); + +// Do something with the data +if($insert > 0 ) { + return 'Succesfully created a new person !'; +} + ``` EasyCRUD ============================ @@ -143,13 +158,13 @@ class YourClass Extends Crud { $person = new person(); // Create new person -$person->Firstname = "Kona"; -$person->Age = "20"; -$person->Sex = "F"; -$created = $person->Create(); +$person->Firstname = "Kona"; +$person->Age = "20"; +$person->Sex = "F"; +$created = $person->Create(); // Or give the bindings to the constructor -$person = new person(array("Firstname"=>"Kona","age"=>"20","sex"=>"F")); +$person = new person(array("Firstname"=>"Kona","age"=>"20","sex"=>"F")); $created = person->Create(); // SQL Equivalent @@ -159,11 +174,11 @@ $created = person->Create(); ```php Id = "17"; -$deleted = $person->Delete(); +$person->Id = "17"; +$deleted = $person->Delete(); // Shorthand method, give id as parameter -$deleted = $person->Delete(17); +$deleted = $person->Delete(17); // SQL Equivalent "DELETE FROM persons WHERE Id = 17 LIMIT 1" -- cgit v1.1 From a470727859a822c24754142544c6c520e34457bd Mon Sep 17 00:00:00 2001 From: Indieteq Date: Tue, 13 Nov 2012 22:17:29 +0100 Subject: Update README.md --- README.md | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 7d6a8bc..38242b5 100644 --- a/README.md +++ b/README.md @@ -128,9 +128,10 @@ if($insert > 0 ) { EasyCRUD ============================ The easyCRUD is a class which you can use to easily execute basic SQL operations like(insert, update, select, delete) on your database. - It uses the database class I've created to execute the SQL queries. +Actually it's just a little ORM class. + ## How to use easyCRUD #### 1. First, create a new class. Then require the easyCRUD class. #### 2. Extend your class and add the following fields to the class. @@ -149,9 +150,9 @@ class YourClass Extends Crud { } ``` -## Some examples of easyCRUD in action. +## EasyCRUD in action. -#### Creating a record +#### Creating a new person ```php Create(); // SQL Equivalent "INSERT INTO persons (Firstname,Age,Sex) VALUES ('Kona','20','F')" ``` -#### Deleting a record +#### Deleting a person ```php Date: Tue, 13 Nov 2012 22:20:24 +0100 Subject: fixed bug in easyCRUD --- easyCRUD/easyCRUD.class.php | 25 ++++++++++++++++--------- index.example.php | 6 +++--- settings.ini.php | 2 +- 3 files changed, 20 insertions(+), 13 deletions(-) diff --git a/easyCRUD/easyCRUD.class.php b/easyCRUD/easyCRUD.class.php index f369197..d6e6bff 100644 --- a/easyCRUD/easyCRUD.class.php +++ b/easyCRUD/easyCRUD.class.php @@ -12,16 +12,28 @@ class Crud { public $variables; + public function __construct($data = array()) { + $this->db = new DB(); + $this->variables = $data; + } + public function __set($name,$value){ - $this->variables[$name] = $value; + if(strtolower($name) === $this->pk) { + $this->variables[$this->pk] = $value; + } + else { + $this->variables[$name] = $value; + } } public function __get($name) { - if(array_key_exists($name,$this->variables)) { - return $this->variables[$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 . @@ -31,11 +43,6 @@ class Crud { return null; } - public function __construct($data = array()) { - $this->db = new DB(); - $this->variables = $data; - } - public function save($id = "0") { $this->variables[$this->pk] = (empty($this->variables[$this->pk])) ? $id : $this->variables[$this->pk]; diff --git a/index.example.php b/index.example.php index 71a78e0..783a08e 100644 --- a/index.example.php +++ b/index.example.php @@ -23,13 +23,13 @@ $persons_num = $db->query("SELECT * FROM Persons", null, PDO::FETCH_NUM); // Fetching single value - $firstname = $db->single("SELECT firstname FROM Persons WHERE Id = :id ", array('id' => '31' ) ); + $firstname = $db->single("SELECT firstname FROM Persons WHERE Id = :id ", array('id' => '3' ) ); // Single Row - $id_age = $db->row("SELECT Id, Age FROM Persons WHERE firstname = :f", array("f"=>"John")); + $id_age = $db->row("SELECT Id, Age FROM Persons WHERE firstname = :f", array("f"=>"Zoe")); // Single Row with numeric index - $id_age_num = $db->row("SELECT Id, Age FROM Persons WHERE firstname = :f", array("f"=>"John"),PDO::FETCH_NUM); + $id_age_num = $db->row("SELECT Id, Age FROM Persons WHERE firstname = :f", array("f"=>"Zoe"),PDO::FETCH_NUM); // Column, numeric index $ages = $db->column("SELECT age FROM Persons"); diff --git a/settings.ini.php b/settings.ini.php index d0472a7..35fd7a9 100644 --- a/settings.ini.php +++ b/settings.ini.php @@ -2,4 +2,4 @@ host = localhost user = root password = -dbname = ultim \ No newline at end of file +dbname = testdb \ No newline at end of file -- cgit v1.1 From 33260959a1a666a636c0825a7bfba54cd174fc99 Mon Sep 17 00:00:00 2001 From: Indieteq Date: Wed, 14 Nov 2012 23:15:10 +0100 Subject: Update README.md --- README.md | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/README.md b/README.md index 38242b5..715dc0a 100644 --- a/README.md +++ b/README.md @@ -125,6 +125,22 @@ if($insert > 0 ) { } ``` +## Method parameters +Every method which executes a query has the optional parameter called bindings. + +The row and the query method have a third optional parameter which is the fetch style. +The default fetch style is PDO::FETCH_ASSOC which returns an associative array. + +Here an example : + +```php +query("SELECT * FROM Persons", null, PDO::FETCH_NUM); +``` +More info about the PDO fetchstyle : http://php.net/manual/en/pdostatement.fetch.php + + EasyCRUD ============================ The easyCRUD is a class which you can use to easily execute basic SQL operations like(insert, update, select, delete) on your database. -- cgit v1.1 From 2fedec82f555bbfc9b020f88de821675f103131e Mon Sep 17 00:00:00 2001 From: Indieteq Date: Wed, 14 Nov 2012 23:28:21 +0100 Subject: Update README.md --- README.md | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 715dc0a..8b22422 100644 --- a/README.md +++ b/README.md @@ -52,7 +52,10 @@ If you want to use these files you''ll have to modify the rights of the logs fol // Fetch whole table $persons = $db->query("SELECT * FROM persons"); ``` -#### Fetching with Bindings: +#### Fetching with Bindings (ANTI-SQL-INJECTION): +Binding parameters is the best way to prevent SQL injection. The class prepares your SQL query and binds the parameters +afterwards. + There are three different ways to bind parameters. ```php query("SELECT * FROM Persons", null, PDO::FETCH_NUM); + $person_num = $db->row("SELECT * FROM Persons WHERE id = :id", array("id"=>"1"), PDO::FETCH_NUM); + + print_r($person_num); + // Array ( [0] => 1 [1] => Johny [2] => Doe [3] => M [4] => 19 ) + ``` More info about the PDO fetchstyle : http://php.net/manual/en/pdostatement.fetch.php @@ -150,7 +157,8 @@ Actually it's just a little ORM class. ## How to use easyCRUD #### 1. First, create a new class. Then require the easyCRUD class. -#### 2. Extend your class and add the following fields to the class. +#### 2. Extend your class to the base class Crud and add the following fields to the class. +#### Example class : ```php Date: Wed, 14 Nov 2012 23:34:29 +0100 Subject: Update README.md --- README.md | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 8b22422..d79c563 100644 --- a/README.md +++ b/README.md @@ -132,7 +132,7 @@ if($insert > 0 ) { Every method which executes a query has the optional parameter called bindings. The row and the query method have a third optional parameter which is the fetch style. -The default fetch style is PDO::FETCH_ASSOC which returns an associative array. +The default fetch style is PDO::FETCH_ASSOC which returns an associative array. Here an example : @@ -216,6 +216,7 @@ $person->Firstname = "Nhoj"; $person->Age = "20"; $person->Sex = "F"; $person->Id = "4"; +// Returns affected rows $saved = $person->Save(); // Or give the bindings to the constructor @@ -230,13 +231,16 @@ $saved = $person->Save(); Id = "1"; -$person = $person->Find(); +$person->Find(); + +echo $person->firstname; +// Johny // Shorthand method, give id as parameter -$person = $person->Find(1); +$person->Find(1); // SQL Equivalent -"SELECT * FROM persons WHERE Id= 1" +"SELECT * FROM persons WHERE Id = 1" ``` #### Getting all the persons ```php -- cgit v1.1 From 58e35fcb39740b596e2d128b6e6967eaf5f836eb Mon Sep 17 00:00:00 2001 From: Indieteq Date: Thu, 15 Nov 2012 10:24:32 +0100 Subject: Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index d79c563..52f7d60 100644 --- a/README.md +++ b/README.md @@ -212,7 +212,7 @@ $deleted = $person->Delete(17); ```php Firstname = "Nhoj"; +$person->Firstname = "John"; $person->Age = "20"; $person->Sex = "F"; $person->Id = "4"; -- cgit v1.1 From d5f6e752bdd7fb6548dda51b7f0dcf1e14321a74 Mon Sep 17 00:00:00 2001 From: Indieteq Date: Thu, 15 Nov 2012 21:28:42 +0100 Subject: Update README.md --- README.md | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 52f7d60..beb3eaa 100644 --- a/README.md +++ b/README.md @@ -3,10 +3,6 @@ PDO Database Class A database class for PHP-MySQL which uses the PDO extension. -## Feature overview -- Simple fetching -- Logger class - ## To use the class #### 1. Edit the database settings in the settings.ini.php ``` @@ -27,17 +23,18 @@ require("Db.class.php"); // The instance $db = new Db(); ``` -#### 4. Logs - Modify the rights of the logs folder +#### 4. Logs - Modify the read/write rights of the root folder + Everytime an exception is thrown by the database class a log file gets created or modified. -These logs are stored in the logs directory. +These logs are stored in the logs directory. Which means the database class needs write access for the logs folder. +If the files are on a webserver you'll have to modify the rights of the root folder otherwise you'll get a "Permission denied" error. The log file is a simple plain text file with the current date('year-month-day') as filename. -If you want to use these files you''ll have to modify the rights of the logs folder. - ## Examples - -#### The table persons, +Below some examples of the basic functions of the database class. I've included a SQL dump so you can easily test the database +class functions. +#### The persons table | id | firstname | lastname | sex | age |:-----------:|:------------:|:------------:|:------------:|:------------:| | 1 | John | Doe | M | 19 -- cgit v1.1 From 3e7e9cb2edad4ab394bcbf17de681d9f4f2604ce Mon Sep 17 00:00:00 2001 From: Indieteq Date: Wed, 21 Nov 2012 22:47:33 +0100 Subject: Update README.md --- README.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/README.md b/README.md index beb3eaa..69f9e9a 100644 --- a/README.md +++ b/README.md @@ -68,6 +68,9 @@ $person = $db->query("SELECT * FROM Persons WHERE firstname = :firstname AND // 3. Or just give the parameters to the method $person = $db->query("SELECT * FROM Persons WHERE firstname = :firstname",array("firstname"=>"John","id"=>"1")); ``` + +More about SQL injection prevention : http://indieteq.com/index/readmore/how-to-prevent-sql-injection-in-php + #### Fetching Row: This method always returns only 1 row. ```php -- cgit v1.1 From c2c4dfc41b05d3009303978c1cc8c928148de128 Mon Sep 17 00:00:00 2001 From: Indieteq Date: Sun, 9 Dec 2012 14:03:04 +0100 Subject: Update Db.class.php --- Db.class.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Db.class.php b/Db.class.php index 1eb616e..6be4c94 100644 --- a/Db.class.php +++ b/Db.class.php @@ -3,7 +3,7 @@ * DB - A simple database class * * @author Author: Vivek Wicky Aswal. (https://twitter.com/#!/VivekWickyAswal) - * @git htt://github.com/indieteq-vivek/simple-db-class + * @git https://github.com/indieteq-vivek/PHP-MySQL-PDO-Database-Class * @version 0.2ab * */ -- cgit v1.1 From 86d877be6ce1898ca5915a7c86c3ca14eb1b115c Mon Sep 17 00:00:00 2001 From: Indieteq Date: Sun, 9 Dec 2012 14:05:06 +0100 Subject: Update Log.class.php --- Log.class.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Log.class.php b/Log.class.php index 07ccd04..c0ca402 100644 --- a/Log.class.php +++ b/Log.class.php @@ -2,7 +2,7 @@ /* * * Log A logger class which creates logs when an exception is thrown. * @author Author: Vivek Wicky Aswal. (https://twitter.com/#!/VivekWickyAswal) - * @git htt://github.com/indieteq-vivek/simple-db-class + * @git https://github.com/indieteq-vivek/PHP-MySQL-PDO-Database-Class * @version 0.1a */ class Log { -- cgit v1.1 From 03e1d606b99229787d63eb550b043c6bc48bfa26 Mon Sep 17 00:00:00 2001 From: Indieteq Date: Sat, 19 Jan 2013 18:13:35 +0100 Subject: Composer --- composer.json | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 composer.json diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..2f2ef8a --- /dev/null +++ b/composer.json @@ -0,0 +1,6 @@ +{ + "name": "Indieteq/PHP-MySQL-PDO-Database-Class", + "require": { + "php": ">=5.3.0" + } +} \ No newline at end of file -- cgit v1.1 From 4aa232983c59760412112f5c77b9aeb06ab58a27 Mon Sep 17 00:00:00 2001 From: Indieteq Date: Fri, 30 Aug 2013 18:56:27 +0200 Subject: Renamed index.example.php to index.php You'll instantly see the results if you browse to the folder. --- index.example.php | 47 ----------------------------------------------- index.php | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 47 deletions(-) delete mode 100644 index.example.php create mode 100644 index.php diff --git a/index.example.php b/index.example.php deleted file mode 100644 index 783a08e..0000000 --- a/index.example.php +++ /dev/null @@ -1,47 +0,0 @@ -bind("firstname","John"); - $db->bind("age","19"); - - // 2. Bind more parameters - $db->bindMore(array("firstname"=>"John","age"=>"19")); - - // 3. Or just give the parameters to the method - $db->query("SELECT * FROM Persons WHERE firstname = :firstname AND age = :age", array("firstname"=>"John","age"=>"19")); - - // Fetching data - $person = $db->query("SELECT * FROM Persons"); - - // If you want another fetchmode just give it as parameter - $persons_num = $db->query("SELECT * FROM Persons", null, PDO::FETCH_NUM); - - // Fetching single value - $firstname = $db->single("SELECT firstname FROM Persons WHERE Id = :id ", array('id' => '3' ) ); - - // Single Row - $id_age = $db->row("SELECT Id, Age FROM Persons WHERE firstname = :f", array("f"=>"Zoe")); - - // Single Row with numeric index - $id_age_num = $db->row("SELECT Id, Age FROM Persons WHERE firstname = :f", array("f"=>"Zoe"),PDO::FETCH_NUM); - - // Column, numeric index - $ages = $db->column("SELECT age FROM Persons"); - - // The following statemens will return the affected rows - - // Update statement - $update = $db->query("UPDATE Persons SET firstname = :f WHERE Id = :id",array("f"=>"Johny","id"=>"1")); - - // Insert statement -// $insert = $db->query("INSERT INTO Persons(Firstname,Age) VALUES(:f,:age)",array("f"=>"Vivek","age"=>"20")); - - // Delete statement -// $delete = $db->query("DELETE FROM Persons WHERE Id = :id",array("id"=>"6")); -?> \ No newline at end of file diff --git a/index.php b/index.php new file mode 100644 index 0000000..b96b482 --- /dev/null +++ b/index.php @@ -0,0 +1,47 @@ +bind("firstname","John"); + $db->bind("age","19"); + + // 2. Bind more parameters + $db->bindMore(array("firstname"=>"John","age"=>"19")); + + // 3. Or just give the parameters to the method + $db->query("SELECT * FROM Persons WHERE firstname = :firstname AND age = :age", array("firstname"=>"John","age"=>"19")); + + // Fetching data + $person = $db->query("SELECT * FROM Persons"); + + // If you want another fetchmode just give it as parameter + $persons_num = $db->query("SELECT * FROM Persons", null, PDO::FETCH_NUM); + + // Fetching single value + $firstname = $db->single("SELECT firstname FROM Persons WHERE Id = :id ", array('id' => '3' ) ); + + // Single Row + $id_age = $db->row("SELECT Id, Age FROM Persons WHERE firstname = :f", array("f"=>"Zoe")); + + // Single Row with numeric index + $id_age_num = $db->row("SELECT Id, Age FROM Persons WHERE firstname = :f", array("f"=>"Zoe"),PDO::FETCH_NUM); + + // Column, numeric index + $ages = $db->column("SELECT age FROM Persons"); + + // The following statemens will return the affected rows + + // Update statement + $update = $db->query("UPDATE Persons SET firstname = :f WHERE Id = :id",array("f"=>"Johny","id"=>"1")); + + // Insert statement +// $insert = $db->query("INSERT INTO Persons(Firstname,Age) VALUES(:f,:age)",array("f"=>"Vivek","age"=>"20")); + + // Delete statement +// $delete = $db->query("DELETE FROM Persons WHERE Id = :id",array("id"=>"6")); +?> -- cgit v1.1 From a9ce530b84b533b566038409ce18fda93ee26d94 Mon Sep 17 00:00:00 2001 From: Indieteq Date: Sat, 31 Aug 2013 13:16:15 +0200 Subject: Update README.md --- README.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 69f9e9a..cba43aa 100644 --- a/README.md +++ b/README.md @@ -3,6 +3,8 @@ PDO Database Class A database class for PHP-MySQL which uses the PDO extension. +If you have any questions go to : http://indieteq.com/index/readmore/how-to-prevent-sql-injection-in-php + ## To use the class #### 1. Edit the database settings in the settings.ini.php ``` @@ -250,4 +252,4 @@ $persons = $person->all(); // SQL Equivalent "SELECT * FROM persons -``` \ No newline at end of file +``` -- cgit v1.1 From b166f0ad7581d43f39c0f41abd4fe727cda77be8 Mon Sep 17 00:00:00 2001 From: Indieteq Date: Sat, 31 Aug 2013 13:25:19 +0200 Subject: Show some results --- index.php | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/index.php b/index.php index b96b482..623cb9a 100644 --- a/index.php +++ b/index.php @@ -44,4 +44,17 @@ // Delete statement // $delete = $db->query("DELETE FROM Persons WHERE Id = :id",array("id"=>"6")); + + function d($v,$t) + { + echo '
';
+		echo '

' . $t. '

'; + var_dump($v); + echo '
'; + } + d($person, "All persons"); + d($firstname, "Fetch Single value, The firstname"); + d($id_age, "Single Row, Id and Age"); + d($ages, "Fetch Column, Numeric Index"); + ?> -- cgit v1.1 From 8fb5251aa1cbaf872e953c2125e0316220196c33 Mon Sep 17 00:00:00 2001 From: Indieteq Date: Mon, 9 Sep 2013 20:43:43 +0200 Subject: Added aggregate functions Added Aggregate methods such as : min, max, avg, sum etc --- easyCRUD/easyCRUD.class.php | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/easyCRUD/easyCRUD.class.php b/easyCRUD/easyCRUD.class.php index d6e6bff..5e21bbe 100644 --- a/easyCRUD/easyCRUD.class.php +++ b/easyCRUD/easyCRUD.class.php @@ -99,5 +99,31 @@ class Crud { 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); + } + } -?> \ No newline at end of file +?> -- cgit v1.1 From 097f88ca83861112101f1244113e35b71e67e2f8 Mon Sep 17 00:00:00 2001 From: Indieteq Date: Mon, 9 Sep 2013 20:44:56 +0200 Subject: Added some Aggregate method examples Added some Aggregate method examples --- easyCRUD/index.php | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/easyCRUD/index.php b/easyCRUD/index.php index e77c04e..b48efa3 100644 --- a/easyCRUD/index.php +++ b/easyCRUD/index.php @@ -20,13 +20,29 @@ $person->id = "4"; $person->Find(); - echo $person->Firstname; - echo $person->Age; - + d($person->Firstname, "Person->Firstname"); + d($person->Age, "Person->Age"); + // Delete person $person->id = "17"; $delete = $person->Delete(); // Get all persons $persons = $person->all(); -?> \ No newline at end of file + + // 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 '
';
+      echo '

' . $t. '

'; + var_dump($v); + echo '
'; + } + +?> -- cgit v1.1 From 5938f993a966603ee92aa2e85a1c1a351ff30bae Mon Sep 17 00:00:00 2001 From: Indieteq Date: Fri, 18 Oct 2013 18:26:33 +0200 Subject: Fixed relative path problem It's now possible to require the easyCRUD class file from any file from any folder. --- easyCRUD/easyCRUD.class.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/easyCRUD/easyCRUD.class.php b/easyCRUD/easyCRUD.class.php index 5e21bbe..2033b15 100644 --- a/easyCRUD/easyCRUD.class.php +++ b/easyCRUD/easyCRUD.class.php @@ -5,7 +5,7 @@ * @author Author: Vivek Wicky Aswal. (https://twitter.com/#!/VivekWickyAswal) * @version 0.1a */ -require_once("../Db.class.php"); +require_once(__DIR__ . '/../Db.class.php'); class Crud { private $db; -- cgit v1.1 From f7f0436cc993cf9980d2eafdc91d95b0223d1374 Mon Sep 17 00:00:00 2001 From: Indieteq Date: Sun, 20 Oct 2013 18:02:26 +0200 Subject: The settings.ini file isn't readable anymore ( from the browser) --- settings.ini.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/settings.ini.php b/settings.ini.php index 35fd7a9..b554d52 100644 --- a/settings.ini.php +++ b/settings.ini.php @@ -1,5 +1,6 @@ +; [SQL] host = localhost user = root password = -dbname = testdb \ No newline at end of file +dbname = testdb -- cgit v1.1 From cfa2fe3ed39a315f5dc33fce582d64d4854f6a9c Mon Sep 17 00:00:00 2001 From: Indieteq Date: Sun, 10 Nov 2013 18:06:26 +0100 Subject: Added lastinsertId method And fixed the sql writing to log. --- Db.class.php | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/Db.class.php b/Db.class.php index 6be4c94..813de24 100644 --- a/Db.class.php +++ b/Db.class.php @@ -111,7 +111,7 @@ class DB catch(PDOException $e) { # Write into log and display Exception - echo $this->ExceptionLog($e->getMessage(),$this->sQuery->queryString); + echo $this->ExceptionLog($e->getMessage(), $query ); die(); } @@ -169,7 +169,16 @@ class DB else { return NULL; } - } + } + + /** + * Returns the last inserted id. + * @return string + */ + public function lastInsertId() { + return $this->pdo->lastInsertId(); + } + /** * Returns an array which represents a column from the result set * @@ -239,4 +248,4 @@ class DB return $exception; } } -?> \ No newline at end of file +?> -- cgit v1.1 From 5d5cd89a6ce41568d4a1be809390b7fcdb2cb6d6 Mon Sep 17 00:00:00 2001 From: Indieteq Date: Fri, 3 Jan 2014 15:46:10 +0100 Subject: Added close connection methode --- Db.class.php | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/Db.class.php b/Db.class.php index 813de24..4780607 100644 --- a/Db.class.php +++ b/Db.class.php @@ -75,6 +75,17 @@ class DB die(); } } + /* + * You can use this little method if you want to close the PDO connection + * + */ + private function CloseConnection() + { + # Set the PDO object to null to close the connection + # http://www.php.net/manual/en/pdo.connections.php + $this->pdo = null; + } + /** * Every method which needs to execute a SQL query uses this method. * -- cgit v1.1 From c995f7ce676780156de564fbc5471c8aa8069ff5 Mon Sep 17 00:00:00 2001 From: Indieteq Date: Fri, 3 Jan 2014 15:46:37 +0100 Subject: Update Db.class.php --- Db.class.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Db.class.php b/Db.class.php index 4780607..53f0d72 100644 --- a/Db.class.php +++ b/Db.class.php @@ -79,7 +79,7 @@ class DB * You can use this little method if you want to close the PDO connection * */ - private function CloseConnection() + public function CloseConnection() { # Set the PDO object to null to close the connection # http://www.php.net/manual/en/pdo.connections.php -- cgit v1.1 From 086c72ca2a679da6b63b9945332a2514c961e162 Mon Sep 17 00:00:00 2001 From: Indieteq Date: Mon, 6 Jan 2014 18:42:27 +0100 Subject: UTF8, Query method fix When pdo object is initialized the encoding is set to utf8. when writing sql statements with lower case the query method will call the correct method instead of returning null. --- Db.class.php | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/Db.class.php b/Db.class.php index 53f0d72..bdf2954 100644 --- a/Db.class.php +++ b/Db.class.php @@ -56,8 +56,8 @@ class DB $dsn = 'mysql:dbname='.$this->settings["dbname"].';host='.$this->settings["host"].''; try { - # Read settings from INI file - $this->pdo = new PDO($dsn, $this->settings["user"], $this->settings["password"]); + # 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")); # We can now log any exceptions on Fatal error. $this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); @@ -165,16 +165,19 @@ class DB * @param int $fetchmode * @return mixed */ - public function query($query,$params = null,$fetchmode = PDO::FETCH_ASSOC) + public function query($query,$params = null, $fetchmode = PDO::FETCH_ASSOC) { $query = trim($query); $this->Init($query,$params); - if (stripos($query, 'select') === 0){ + # Assuming there are spaces in the query. Otherwise it would be an invalid query. + $statement = strtolower(substr($query, 0 , 6)); + + if ($statement === 'select') { return $this->sQuery->fetchAll($fetchmode); } - elseif (stripos($query, 'insert') === 0 || stripos($query, 'update') === 0 || stripos($query, 'delete') === 0) { + elseif ( $statement === 'insert' || $statement === 'update' || $statement === 'delete' ) { return $this->sQuery->rowCount(); } else { -- cgit v1.1 From 5f4f1fefa5f6ed34681b526b66b16bfe6654c741 Mon Sep 17 00:00:00 2001 From: Indieteq Date: Wed, 8 Jan 2014 12:41:48 +0100 Subject: Update Db.class.php --- Db.class.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Db.class.php b/Db.class.php index bdf2954..1e67b11 100644 --- a/Db.class.php +++ b/Db.class.php @@ -171,7 +171,7 @@ class DB $this->Init($query,$params); - # Assuming there are spaces in the query. Otherwise it would be an invalid query. + # The first six letters of the sql statement -> insert, select, etc... $statement = strtolower(substr($query, 0 , 6)); if ($statement === 'select') { -- cgit v1.1 From 04b7e12196d2d33e8da0272a3bf023268a093279 Mon Sep 17 00:00:00 2001 From: Indieteq Date: Tue, 28 Jan 2014 13:15:14 +0100 Subject: PDO slow loading PDO might load slow when the user specifies localhost as host in the settings.ini.php file. Use 127.0.0.1 instead to resolve this problem. --- README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index cba43aa..ebac32d 100644 --- a/README.md +++ b/README.md @@ -7,9 +7,10 @@ If you have any questions go to : http://indieteq.com/index/readmore/how-to-prev ## To use the class #### 1. Edit the database settings in the settings.ini.php +### Note if PDO is loading slow change localhost to -> 127.0.0.1 ! ``` [SQL] -host = localhost +host = 127.0.0.1 user = root password = dbname = yourdatabase -- cgit v1.1 From 34f973c2dafb23d059a588b1a875253cb1ab9b04 Mon Sep 17 00:00:00 2001 From: Indieteq Date: Mon, 14 Apr 2014 11:15:17 +0200 Subject: License shiz --- README.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/README.md b/README.md index ebac32d..7b43f11 100644 --- a/README.md +++ b/README.md @@ -254,3 +254,7 @@ $persons = $person->all(); // SQL Equivalent "SELECT * FROM persons ``` + +## Copyright and license +#### Code released under Beerware + -- cgit v1.1 From 32ba5f8f79e8e321b1cf0c312ca81eacba51fe61 Mon Sep 17 00:00:00 2001 From: Indieteq Date: Mon, 14 Apr 2014 11:23:11 +0200 Subject: BEER LICENSE --- LICENSE | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 LICENSE diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..1a4140c --- /dev/null +++ b/LICENSE @@ -0,0 +1,7 @@ +/* + * ---------------------------------------------------------------------------- + * "THE BEER-WARE LICENSE" + * If we meet some day, and you think + * this stuff is worth it, you can buy me a beer in return. + * ---------------------------------------------------------------------------- + */ \ No newline at end of file -- cgit v1.1 From 6a371c08498990dd12b0e9580deae7ec18e7efd5 Mon Sep 17 00:00:00 2001 From: Alex Date: Wed, 18 Jun 2014 11:36:00 +0300 Subject: Update Db.class.php git url --- Db.class.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Db.class.php b/Db.class.php index 1e67b11..c96434a 100644 --- a/Db.class.php +++ b/Db.class.php @@ -3,7 +3,7 @@ * DB - A simple database class * * @author Author: Vivek Wicky Aswal. (https://twitter.com/#!/VivekWickyAswal) - * @git https://github.com/indieteq-vivek/PHP-MySQL-PDO-Database-Class + * @git https://github.com/indieteq/PHP-MySQL-PDO-Database-Class * @version 0.2ab * */ -- cgit v1.1 From 56dabded9e3ba975b9672f25651ff2e516bf625f Mon Sep 17 00:00:00 2001 From: Alex Date: Wed, 18 Jun 2014 11:36:43 +0300 Subject: Update Log.class.php --- Log.class.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Log.class.php b/Log.class.php index c0ca402..6d6aa63 100644 --- a/Log.class.php +++ b/Log.class.php @@ -2,7 +2,7 @@ /* * * Log A logger class which creates logs when an exception is thrown. * @author Author: Vivek Wicky Aswal. (https://twitter.com/#!/VivekWickyAswal) - * @git https://github.com/indieteq-vivek/PHP-MySQL-PDO-Database-Class + * @git https://github.com/indieteq/PHP-MySQL-PDO-Database-Class * @version 0.1a */ class Log { @@ -67,4 +67,4 @@ file_put_contents($log, $logcontent); } } -?> \ No newline at end of file +?> -- cgit v1.1 From ea9f7d335aff00546e2dbeba2e176d6aa99f88c6 Mon Sep 17 00:00:00 2001 From: Callum West Date: Fri, 27 Jun 2014 11:51:41 +0100 Subject: Update Db.class.php Added option for "SHOW" query, and changed requirement to check for first 6 letters. Not all query types are words with 6 letters. --- Db.class.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Db.class.php b/Db.class.php index c96434a..fe11d36 100644 --- a/Db.class.php +++ b/Db.class.php @@ -172,9 +172,10 @@ class DB $this->Init($query,$params); # The first six letters of the sql statement -> insert, select, etc... - $statement = strtolower(substr($query, 0 , 6)); + $rawStatement = explode(" ",$query); + $statement = strtolower($rawStatement[0]); - if ($statement === 'select') { + if ($statement === 'select' || $statement === 'show') { return $this->sQuery->fetchAll($fetchmode); } elseif ( $statement === 'insert' || $statement === 'update' || $statement === 'delete' ) { -- cgit v1.1 From 66e72318fbac670abe640d62eaf0c87ef9498fb3 Mon Sep 17 00:00:00 2001 From: Indieteq Date: Tue, 1 Jul 2014 11:37:16 +0200 Subject: Update Db.class.php Edited some comments --- Db.class.php | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/Db.class.php b/Db.class.php index fe11d36..9040728 100644 --- a/Db.class.php +++ b/Db.class.php @@ -157,7 +157,7 @@ class DB } } /** - * If the SQL query contains a SELECT statement it returns an array containing all of the result set row + * If the SQL query contains a SELECT or SHOW statement it returns an array containing all of the result set row * If the SQL statement is a DELETE, INSERT, or UPDATE statement it returns the number of affected rows * * @param string $query @@ -171,8 +171,9 @@ class DB $this->Init($query,$params); - # The first six letters of the sql statement -> insert, select, etc... - $rawStatement = explode(" ",$query); + $rawStatement = explode(" ", $query); + + # Which SQL statement is used $statement = strtolower($rawStatement[0]); if ($statement === 'select' || $statement === 'show') { -- cgit v1.1 From 9737cfe130e4a7a56eaa91ba0835efdedb9bceec Mon Sep 17 00:00:00 2001 From: Jamie Cressey Date: Sat, 5 Jul 2014 16:14:09 -0400 Subject: Clean up for my needs --- Db.class.php | 6 +-- SQL dump/testdb.sql | 38 ------------- composer.json | 4 +- easyCRUD/Person.class.php | 13 ----- easyCRUD/easyCRUD.class.php | 129 -------------------------------------------- easyCRUD/index.php | 48 ----------------- settings.ini.php | 6 --- 7 files changed, 5 insertions(+), 239 deletions(-) delete mode 100644 SQL dump/testdb.sql delete mode 100644 easyCRUD/Person.class.php delete mode 100644 easyCRUD/easyCRUD.class.php delete mode 100644 easyCRUD/index.php delete mode 100644 settings.ini.php 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 @@ - \ 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 @@ -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 @@ -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 '
';
-      echo '

' . $t. '

'; - var_dump($v); - echo '
'; - } - -?> 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 @@ -; -[SQL] -host = localhost -user = root -password = -dbname = testdb -- cgit v1.1 From e31300547c71b567159456048a91b4d55f483db8 Mon Sep 17 00:00:00 2001 From: Jamie Cressey Date: Sat, 5 Jul 2014 17:25:41 -0400 Subject: typo --- Db.class.php | 2 +- composer.json | 6 ------ index.php | 60 ----------------------------------------------------------- 3 files changed, 1 insertion(+), 67 deletions(-) delete mode 100644 composer.json delete mode 100644 index.php diff --git a/Db.class.php b/Db.class.php index 7494a7a..cf0e4d8 100644 --- a/Db.class.php +++ b/Db.class.php @@ -57,7 +57,7 @@ class DB try { # Read settings from INI file, set UTF8 - $this->pdo = new PDO($dsn, $ettings["dbuser"], $settings["dbpass"], array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8")); + $this->pdo = new PDO($dsn, $settings["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/composer.json b/composer.json deleted file mode 100644 index 0db4daf..0000000 --- a/composer.json +++ /dev/null @@ -1,6 +0,0 @@ -{ - "name": "jayc89/PHP-MySQL-PDO-Database-Class", - "require": { - "php": ">=5.3.0" - } -} diff --git a/index.php b/index.php deleted file mode 100644 index 623cb9a..0000000 --- a/index.php +++ /dev/null @@ -1,60 +0,0 @@ -bind("firstname","John"); - $db->bind("age","19"); - - // 2. Bind more parameters - $db->bindMore(array("firstname"=>"John","age"=>"19")); - - // 3. Or just give the parameters to the method - $db->query("SELECT * FROM Persons WHERE firstname = :firstname AND age = :age", array("firstname"=>"John","age"=>"19")); - - // Fetching data - $person = $db->query("SELECT * FROM Persons"); - - // If you want another fetchmode just give it as parameter - $persons_num = $db->query("SELECT * FROM Persons", null, PDO::FETCH_NUM); - - // Fetching single value - $firstname = $db->single("SELECT firstname FROM Persons WHERE Id = :id ", array('id' => '3' ) ); - - // Single Row - $id_age = $db->row("SELECT Id, Age FROM Persons WHERE firstname = :f", array("f"=>"Zoe")); - - // Single Row with numeric index - $id_age_num = $db->row("SELECT Id, Age FROM Persons WHERE firstname = :f", array("f"=>"Zoe"),PDO::FETCH_NUM); - - // Column, numeric index - $ages = $db->column("SELECT age FROM Persons"); - - // The following statemens will return the affected rows - - // Update statement - $update = $db->query("UPDATE Persons SET firstname = :f WHERE Id = :id",array("f"=>"Johny","id"=>"1")); - - // Insert statement -// $insert = $db->query("INSERT INTO Persons(Firstname,Age) VALUES(:f,:age)",array("f"=>"Vivek","age"=>"20")); - - // Delete statement -// $delete = $db->query("DELETE FROM Persons WHERE Id = :id",array("id"=>"6")); - - function d($v,$t) - { - echo '
';
-		echo '

' . $t. '

'; - var_dump($v); - echo '
'; - } - d($person, "All persons"); - d($firstname, "Fetch Single value, The firstname"); - d($id_age, "Single Row, Id and Age"); - d($ages, "Fetch Column, Numeric Index"); - -?> -- cgit v1.1 From d8f4ce17848ac5aed7157adff17eecaf78601e49 Mon Sep 17 00:00:00 2001 From: Jamie Cressey Date: Sun, 6 Jul 2014 07:46:21 -0400 Subject: Update logs path --- Log.class.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Log.class.php b/Log.class.php index 6d6aa63..4c066c3 100644 --- a/Log.class.php +++ b/Log.class.php @@ -12,8 +12,9 @@ # @void, Default Constructor, Sets the timezone and path of the log files. public function __construct() { + global $root; date_default_timezone_set('Europe/Amsterdam'); - $this->path = dirname(__FILE__) . $this->path; + $this->path = $root.$this->path; } /** -- cgit v1.1