diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..57872d0
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1 @@
+/vendor/
diff --git a/DBPDO.php b/DBPDO.php
new file mode 100644
index 0000000..5356d2b
--- /dev/null
+++ b/DBPDO.php
@@ -0,0 +1,147 @@
+dbhost = $dbhost;
+ $this->dbname = $dbname;
+ $this->dbuser = $dbuser;
+ $this->dbpass = $dbpass;
+ $this->sqlserver = $sqlserver;
+ $this->connect();
+ }
+
+ // Disallow cloning and unserializing
+ private function __clone() {}
+ private function __wakeup() {}
+
+
+ function prep_query($query)
+ {
+ return $this->pdo->prepare($query);
+ }
+
+
+ function connect()
+ {
+ if (!$this->pdo) {
+ if($this->sqlserver){
+ $dsn = 'sqlsrv:Server=' . $this->dbhost . ';Database=' . $this->dbname . ';Encrypt=no';
+ }else{
+ $dsn = 'mysql:dbname=' . $this->dbname . ';host=' . $this->dbhost . ';charset=utf8mb4';
+ }
+ $user = $this->dbuser;
+ $password = $this->dbpass;
+
+ try {
+ if($this->sqlserver){
+ $this->pdo = new PDO($dsn, $user, $password);
+ }else{
+ $this->pdo = new PDO($dsn, $user, $password, array(PDO::ATTR_PERSISTENT => true));
+ }
+ return true;
+ } catch (PDOException $e) {
+ $this->error = $e->getMessage();
+ return false;
+ }
+ } else {
+ $this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
+ return true;
+ }
+ }
+
+
+ function table_exists($table_name)
+ {
+ $stmt = $this->prep_query('SHOW TABLES LIKE ?');
+ $stmt->execute(array($table_name));
+ return $stmt->rowCount() > 0;
+ }
+
+
+ function execute($query, $values = null, $debug = false)
+ {
+ if ($values == null) {
+ $values = array();
+ } else if (!is_array($values)) {
+ $values = array($values);
+ }
+ $stmt = $this->prep_query($query);
+ if($debug){
+ echo $query;
+ print_r($values);
+ die();
+ }
+ try {
+ $stmt->execute($values);
+ } catch (PDOException $e) {
+ $this->error = $e->getMessage();
+ return false;
+ }
+ return $stmt;
+ }
+
+ function fetch($query, $values = null)
+ {
+ if ($values == null) {
+ $values = array();
+ } else if (!is_array($values)) {
+ $values = array($values);
+ }
+ $stmt = $this->execute($query, $values);
+ return $stmt->fetch(PDO::FETCH_ASSOC);
+ }
+
+ function fetchAll($query, $values = null, $key = null)
+ {
+ if ($values == null) {
+ $values = array();
+ } else if (!is_array($values)) {
+ $values = array($values);
+ }
+ $stmt = $this->execute($query, $values);
+ $results = $stmt->fetchAll(PDO::FETCH_ASSOC);
+
+ // Allows the user to retrieve results using a
+ // column from the results as a key for the array
+ if(!empty($results)){
+ if ($key != null) {
+ $keyed_results = array();
+ foreach ($results as $result) {
+ $keyed_results[$result[$key]] = $result;
+ }
+ $results = $keyed_results;
+ }
+ }
+ return $results;
+ }
+
+ function lastInsertId()
+ {
+ return $this->pdo->lastInsertId();
+ }
+}
diff --git a/class.DBPDO.php b/class.DBPDO.php
deleted file mode 100644
index 1fd2ea4..0000000
--- a/class.DBPDO.php
+++ /dev/null
@@ -1,94 +0,0 @@
-connect();
- }
-
-
- function prep_query($query){
- return $this->pdo->prepare($query);
- }
-
-
- function connect(){
- if(!$this->pdo){
-
- $dsn = 'mysql:dbname=' . DATABASE_NAME . ';host=' . DATABASE_HOST;
- $user = DATABASE_USER;
- $password = DATABASE_PASS;
-
- try {
- $this->pdo = new PDO($dsn, $user, $password, array(PDO::ATTR_PERSISTENT => true));
- return true;
- } catch (PDOException $e) {
- $this->error = $e->getMessage();
- die($this->error);
- return false;
- }
- }else{
- $this->pdo->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING );
- return true;
- }
- }
-
-
- function table_exists($table_name){
- $stmt = $this->prep_query('SHOW TABLES LIKE ?');
- $stmt->execute(array($this->add_table_prefix($table_name)));
- return $stmt->rowCount() > 0;
- }
-
-
- function execute($query, $values = null){
- if($values == null){
- $values = array();
- }else if(!is_array($values)){
- $values = array($values);
- }
- $stmt = $this->pdo->prepare($query);
- $stmt->execute($values);
- return $stmt;
- }
-
- function fetch($query, $values = null){
- if($values == null){
- $values = array();
- }else if(!is_array($values)){
- $values = array($values);
- }
- $stmt = $this->execute($query, $values);
- return $stmt->fetch(PDO::FETCH_ASSOC);
- }
-
- function fetchAll($query, $values = null, $key = null){
- if($values == null){
- $values = array();
- }else if(!is_array($values)){
- $values = array($values);
- }
- $stmt = $this->execute($query, $values);
- $results = $stmt->fetchAll(PDO::FETCH_ASSOC);
-
- // Allows the user to retrieve results using a
- // column from the results as a key for the array
- if($key != null && $results[0][$key]){
- $keyed_results = array();
- foreach($results as $result){
- $keyed_results[$result[$key]] = $result;
- }
- $results = $keyed_results;
- }
- return $results;
- }
-
- function lastInsertId(){
- return $this->pdo->lastInsertId();
- }
-
-}
\ No newline at end of file
diff --git a/class.MySQL.php b/class.MySQL.php
index 442fc82..3429fcd 100644
--- a/class.MySQL.php
+++ b/class.MySQL.php
@@ -103,7 +103,7 @@ private function UseDB(){
// Performs a 'mysql_real_escape_string' on the entire array/string
- private function SecureData($data, $types){
+ private function SecureData($data, $types=array()){
if(is_array($data)){
$i = 0;
foreach($data as $key=>$val){
@@ -166,7 +166,6 @@ private function CleanData($data, $type = ''){
$data = filter_var($data, FILTER_VALIDATE_EMAIL);
break;
default:
- $data = '';
break;
}
return $data;
@@ -184,11 +183,10 @@ public function executeSQL($query){
if($this->result = mysql_query($query, $this->databaseLink)){
if (gettype($this->result) === 'resource') {
$this->records = @mysql_num_rows($this->result);
- $this->affected = @mysql_affected_rows($this->databaseLink);
} else {
$this->records = 0;
- $this->affected = 0;
}
+ $this->affected = @mysql_affected_rows($this->databaseLink);
if($this->records > 0){
$this->arrayResults();
@@ -216,7 +214,7 @@ public function setCharset( $charset = 'UTF8' ) {
}
// Adds a record to the database based on the array key names
- public function insert($table, $vars, $exclude = '', $datatypes){
+ public function insert($table, $vars, $exclude = '', $datatypes=array()){
// Catch Exclusions
if($exclude == ''){
@@ -242,7 +240,7 @@ public function insert($table, $vars, $exclude = '', $datatypes){
}
// Deletes a record from the database
- public function delete($table, $where='', $limit='', $like=false, $wheretypes){
+ public function delete($table, $where='', $limit='', $like=false, $wheretypes=array()){
$query = "DELETE FROM `{$table}` WHERE ";
if(is_array($where) && $where != ''){
// Prepare Variables
@@ -263,12 +261,18 @@ public function delete($table, $where='', $limit='', $like=false, $wheretypes){
$query .= ' LIMIT ' . $limit;
}
- return $this->executeSQL($query);
+ $result = $this->executeSQL($query);
+
+ if($this->affected == 0){
+ return false;
+ }
+
+ return $result;
}
// Gets a single row from $from where $where is true
- public function select($from, $where='', $orderBy='', $limit='', $like=false, $operand='AND',$cols='*', $wheretypes){
+ public function select($from, $where='', $orderBy='', $limit='', $like=false, $operand='AND',$cols='*', $wheretypes=array()){
// Catch Exceptions
if(trim($from) == ''){
return false;
@@ -302,12 +306,14 @@ public function select($from, $where='', $orderBy='', $limit='', $like=false, $o
$query .= ' LIMIT ' . $limit;
}
- return $this->executeSQL($query);
+ $result = $this->executeSQL($query);
+ if(is_array($result)) return $result;
+ return array();
}
// Updates a record in the database based on WHERE
- public function update($table, $set, $where, $exclude = '', $datatypes, $wheretypes){
+ public function update($table, $set, $where, $exclude = '', $datatypes=array(), $wheretypes=array()){
// Catch Exceptions
if(trim($table) == '' || !is_array($set) || !is_array($where)){
return false;
@@ -344,7 +350,13 @@ public function update($table, $set, $where, $exclude = '', $datatypes, $wherety
$query = substr($query, 0, -5);
- return $this->executeSQL($query);
+ $result = $this->executeSQL($query);
+
+ if($this->affected == 0){
+ return false;
+ }
+
+ return $result;
}
// 'Arrays' a single result
diff --git a/composer.json b/composer.json
new file mode 100644
index 0000000..e0a7cca
--- /dev/null
+++ b/composer.json
@@ -0,0 +1,16 @@
+{
+ "name": "a1phanumeric/php-mysql-class",
+ "description": "This is a simple to use PHP MySQL class that easily bolts on to any existing PHP application, streamlining your MySQL interactions.",
+ "type": "library",
+ "license": "MIT",
+ "authors": [
+ {
+ "name": "Ed",
+ "email": "ed.rackham19@gmail.com"
+ }
+ ],
+ "require": {},
+ "autoload": {
+ "psr-4": { "A1phanumeric\\": "" }
+ }
+}
diff --git a/readme.md b/readme.md
index b61ad31..e0b457f 100644
--- a/readme.md
+++ b/readme.md
@@ -12,8 +12,24 @@ PHP MySQL Class
This is a simple to use MySQL class that easily bolts on to any existing PHP application, streamlining your MySQL interactions.
+Setup v2.0+
+-----
+
+Include the class using composer as below:
+
+`composer require a1phanumeric/php-mysql-class`
+
+To use in your project, use the following:
+
+`use A1phanumeric\DBPDO;`
+
+`$DB = new DBPDO('db_host', 'db_name', 'db_user', 'db_pass');`
+
+Or, if wanting to use as a singleton instance:
+
+`$DB = DBPDO::getInstance('db_host', 'db_name', 'db_user', 'db_pass');`
-Setup
+### Setup Before v2.0
-----
Firstly, define four constants for the host, database name, username and password:
@@ -131,4 +147,4 @@ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
-along with this program. If not, see .
\ No newline at end of file
+along with this program. If not, see .