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 a1322d5..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.';charset=utf8';
- $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($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->prep_query($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();
- }
-
-}
diff --git a/class.MySQL.php b/class.MySQL.php
index effe482..3429fcd 100644
--- a/class.MySQL.php
+++ b/class.MySQL.php
@@ -261,7 +261,13 @@ public function delete($table, $where='', $limit='', $like=false, $wheretypes=ar
$query .= ' LIMIT ' . $limit;
}
- return $this->executeSQL($query);
+ $result = $this->executeSQL($query);
+
+ if($this->affected == 0){
+ return false;
+ }
+
+ return $result;
}
@@ -344,7 +350,13 @@ public function update($table, $set, $where, $exclude = '', $datatypes=array(),
$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 .