From af9fe9ab6d4ba9083586e105963c91a419e37b42 Mon Sep 17 00:00:00 2001 From: kaysen Date: Mon, 7 Nov 2016 15:51:37 +0800 Subject: [PATCH 01/20] FIX: update and delete --- class.MySQL.php | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/class.MySQL.php b/class.MySQL.php index effe482..0f977bf 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((int) @mysql_affected_rows($this->databaseLink) == 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((int) @mysql_affected_rows($this->databaseLink) == 0){ + return false; + } + + return $result; } // 'Arrays' a single result From 763ae064622c44fe12ae069c7eec4f8ac9822058 Mon Sep 17 00:00:00 2001 From: kaysen Date: Mon, 7 Nov 2016 16:45:21 +0800 Subject: [PATCH 02/20] MOD: class.MySQL.php --- test.php | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 test.php diff --git a/test.php b/test.php new file mode 100644 index 0000000..a4e4801 --- /dev/null +++ b/test.php @@ -0,0 +1,18 @@ +'kaysen', 'age'=>27, 'created'=>time()); +$status = $DB->insert('kaysen_tab', $insertData); +var_dump($status); + */ + +/* +$status = $DB->update('kaysen_tab', array('age'=>666), array('id'=>1)); +var_dump($status); + */ + +$status = $DB->delete('kaysen_tab', array('id'=>1)); +var_dump($status); From 5ccb5210e6bccfe5f5077438a33fb224c5916424 Mon Sep 17 00:00:00 2001 From: kaysen Date: Mon, 7 Nov 2016 16:49:43 +0800 Subject: [PATCH 03/20] Revert "MOD: class.MySQL.php" This reverts commit 763ae064622c44fe12ae069c7eec4f8ac9822058. --- test.php | 18 ------------------ 1 file changed, 18 deletions(-) delete mode 100644 test.php diff --git a/test.php b/test.php deleted file mode 100644 index a4e4801..0000000 --- a/test.php +++ /dev/null @@ -1,18 +0,0 @@ -'kaysen', 'age'=>27, 'created'=>time()); -$status = $DB->insert('kaysen_tab', $insertData); -var_dump($status); - */ - -/* -$status = $DB->update('kaysen_tab', array('age'=>666), array('id'=>1)); -var_dump($status); - */ - -$status = $DB->delete('kaysen_tab', array('id'=>1)); -var_dump($status); From 2e213511ece132429bae95fa1261dc26087173f5 Mon Sep 17 00:00:00 2001 From: kaysen Date: Mon, 7 Nov 2016 16:50:34 +0800 Subject: [PATCH 04/20] MOD: class.MySQL.php --- class.MySQL.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/class.MySQL.php b/class.MySQL.php index 0f977bf..3429fcd 100644 --- a/class.MySQL.php +++ b/class.MySQL.php @@ -263,7 +263,7 @@ public function delete($table, $where='', $limit='', $like=false, $wheretypes=ar $result = $this->executeSQL($query); - if((int) @mysql_affected_rows($this->databaseLink) == 0){ + if($this->affected == 0){ return false; } @@ -352,7 +352,7 @@ public function update($table, $set, $where, $exclude = '', $datatypes=array(), $result = $this->executeSQL($query); - if((int) @mysql_affected_rows($this->databaseLink) == 0){ + if($this->affected == 0){ return false; } From f8c3f52ccb49e8c2bc3eae3e37c049407f63a133 Mon Sep 17 00:00:00 2001 From: Ed Date: Mon, 17 May 2021 15:37:51 +0100 Subject: [PATCH 05/20] Added composer.json --- .gitignore | 1 + composer.json | 14 ++++++++++++++ 2 files changed, 15 insertions(+) create mode 100644 .gitignore create mode 100644 composer.json diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..57872d0 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/vendor/ diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..b3dd7ab --- /dev/null +++ b/composer.json @@ -0,0 +1,14 @@ +{ + "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": "GPL", + "authors": [ + { + "name": "Ed", + "email": "ed@rockfire.co" + } + ], + "minimum-stability": "dev", + "require": {} +} From ab69133546f3f8a301c5a32aac7320267483fd60 Mon Sep 17 00:00:00 2001 From: Ed Date: Mon, 17 May 2021 15:40:28 +0100 Subject: [PATCH 06/20] Updated composer --- composer.json | 2 -- 1 file changed, 2 deletions(-) diff --git a/composer.json b/composer.json index b3dd7ab..857cadf 100644 --- a/composer.json +++ b/composer.json @@ -2,13 +2,11 @@ "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": "GPL", "authors": [ { "name": "Ed", "email": "ed@rockfire.co" } ], - "minimum-stability": "dev", "require": {} } From c6ad610ef2e8c376b77f516a6043cbbf69e6ae82 Mon Sep 17 00:00:00 2001 From: Ed Date: Mon, 17 May 2021 15:44:57 +0100 Subject: [PATCH 07/20] Updated composer license --- composer.json | 1 + 1 file changed, 1 insertion(+) diff --git a/composer.json b/composer.json index 857cadf..181ce2c 100644 --- a/composer.json +++ b/composer.json @@ -2,6 +2,7 @@ "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", From 5909df0be896824982477e5be15c2715981bf5ef Mon Sep 17 00:00:00 2001 From: Ed Rackham Date: Tue, 7 Mar 2023 11:27:51 +0000 Subject: [PATCH 08/20] Update composer.json --- composer.json | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 181ce2c..0d55f9d 100644 --- a/composer.json +++ b/composer.json @@ -9,5 +9,8 @@ "email": "ed@rockfire.co" } ], - "require": {} + "require": {}, + "autoload": { + "psr-4": { "A1phanumeric\\": "A1phanumeric" } + } } From 73ee8d774d37fa59696a6833f099445cbe592f09 Mon Sep 17 00:00:00 2001 From: Ed Rackham Date: Tue, 7 Mar 2023 11:28:11 +0000 Subject: [PATCH 09/20] Update composer.json --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 0d55f9d..e224ec1 100644 --- a/composer.json +++ b/composer.json @@ -6,7 +6,7 @@ "authors": [ { "name": "Ed", - "email": "ed@rockfire.co" + "email": "ed.rackham19@gmail.com" } ], "require": {}, From f6d2ae7916c0357b78d7eb212e4686c035fcc9ae Mon Sep 17 00:00:00 2001 From: Ed Rackham Date: Tue, 7 Mar 2023 11:28:38 +0000 Subject: [PATCH 10/20] Update class.DBPDO.php --- class.DBPDO.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/class.DBPDO.php b/class.DBPDO.php index a1322d5..35d179c 100644 --- a/class.DBPDO.php +++ b/class.DBPDO.php @@ -1,5 +1,7 @@ Date: Fri, 10 Mar 2023 14:39:52 +0000 Subject: [PATCH 11/20] Updated autoload --- composer.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/composer.json b/composer.json index e224ec1..e0a7cca 100644 --- a/composer.json +++ b/composer.json @@ -10,7 +10,7 @@ } ], "require": {}, - "autoload": { - "psr-4": { "A1phanumeric\\": "A1phanumeric" } + "autoload": { + "psr-4": { "A1phanumeric\\": "" } } } From d5e8c3eceda7fb011df7b78365ccb67a06f9cf7a Mon Sep 17 00:00:00 2001 From: Ed Rackham Date: Fri, 10 Mar 2023 14:46:13 +0000 Subject: [PATCH 12/20] Renamed main class file for composer autoloading --- class.DBPDO.php => DBPDO.php | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename class.DBPDO.php => DBPDO.php (100%) diff --git a/class.DBPDO.php b/DBPDO.php similarity index 100% rename from class.DBPDO.php rename to DBPDO.php From 8930083466d1954da855fd99035131e0b60e11a8 Mon Sep 17 00:00:00 2001 From: Ed Rackham Date: Fri, 10 Mar 2023 15:19:40 +0000 Subject: [PATCH 13/20] Release for composer v2.0 --- DBPDO.php | 19 +++++++++++++++---- readme.md | 14 +++++++++++++- 2 files changed, 28 insertions(+), 5 deletions(-) diff --git a/DBPDO.php b/DBPDO.php index 35d179c..990be3e 100644 --- a/DBPDO.php +++ b/DBPDO.php @@ -2,13 +2,24 @@ namespace A1phanumeric; +use \PDO; +use \PDOException; + class DBPDO { public $pdo; private $error; + private $dbname; + private $dbhost; + private $dbuser; + private $dbpass; - function __construct() { + function __construct($dbhost = '', $dbname = '', $dbuser = '', $dbpass= '') { + $this->dbhost = $dbhost; + $this->dbname = $dbname; + $this->dbuser = $dbuser; + $this->dbpass = $dbpass; $this->connect(); } @@ -21,9 +32,9 @@ function prep_query($query){ function connect(){ if(!$this->pdo){ - $dsn = 'mysql:dbname=' . DATABASE_NAME . ';host=' . DATABASE_HOST.';charset=utf8'; - $user = DATABASE_USER; - $password = DATABASE_PASS; + $dsn = 'mysql:dbname=' . $this->dbname . ';host=' . $this->dbhost . ';charset=utf8'; + $user = $this->dbuser; + $password = $this->dbpass; try { $this->pdo = new PDO($dsn, $user, $password, array(PDO::ATTR_PERSISTENT => true)); diff --git a/readme.md b/readme.md index b61ad31..8bd7fb2 100644 --- a/readme.md +++ b/readme.md @@ -12,8 +12,20 @@ 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_name', 'db_host', 'db_user', 'db_pass');` -Setup +###Setup Before v2.0 ----- Firstly, define four constants for the host, database name, username and password: From 1a43e20054d3fdbf5c436e7df285120b9794ef61 Mon Sep 17 00:00:00 2001 From: Ed Rackham Date: Fri, 10 Mar 2023 15:21:48 +0000 Subject: [PATCH 14/20] Update readme.md --- readme.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/readme.md b/readme.md index 8bd7fb2..9dfcf7f 100644 --- a/readme.md +++ b/readme.md @@ -25,7 +25,7 @@ To use in your project, use the following: `$DB = new DBPDO('db_name', 'db_host', 'db_user', 'db_pass');` -###Setup Before v2.0 +### Setup Before v2.0 ----- Firstly, define four constants for the host, database name, username and password: @@ -143,4 +143,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 . From 5e6459cb873a6347e37e6fe04c299cc68a8396b0 Mon Sep 17 00:00:00 2001 From: Ed Rackham Date: Thu, 23 Mar 2023 10:35:10 +0000 Subject: [PATCH 15/20] Update readme.md --- readme.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/readme.md b/readme.md index 9dfcf7f..c7720b4 100644 --- a/readme.md +++ b/readme.md @@ -23,7 +23,7 @@ To use in your project, use the following: `use A1phanumeric\DBPDO;` -`$DB = new DBPDO('db_name', 'db_host', 'db_user', 'db_pass');` +`$DB = new DBPDO('db_host', 'db_name', 'db_user', 'db_pass');` ### Setup Before v2.0 ----- From 9c189d039a68211828100bcd592fc09cb057df86 Mon Sep 17 00:00:00 2001 From: Ed Rackham Date: Wed, 29 Nov 2023 11:01:21 +0000 Subject: [PATCH 16/20] Fixed bug with getting keyed results --- DBPDO.php | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/DBPDO.php b/DBPDO.php index 990be3e..dccd848 100644 --- a/DBPDO.php +++ b/DBPDO.php @@ -90,12 +90,14 @@ function fetchAll($query, $values = null, $key = null){ // 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; + if(!empty($results)){ + if ($key != null && $results[0][$key]) { + $keyed_results = array(); + foreach ($results as $result) { + $keyed_results[$result[$key]] = $result; + } + $results = $keyed_results; } - $results = $keyed_results; } return $results; } From 7f8c3e3b7a7e115f0da3cb4304b0f23592280664 Mon Sep 17 00:00:00 2001 From: Ed Rackham Date: Tue, 7 Jan 2025 09:30:55 +0000 Subject: [PATCH 17/20] Update DBPDO.php Updated DBPDO to use a a singleton instance if needed throughout the app --- DBPDO.php | 90 +++++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 65 insertions(+), 25 deletions(-) diff --git a/DBPDO.php b/DBPDO.php index dccd848..507e206 100644 --- a/DBPDO.php +++ b/DBPDO.php @@ -5,84 +5,124 @@ use \PDO; use \PDOException; -class DBPDO { +class DBPDO +{ + private static $instance = null; public $pdo; private $error; private $dbname; private $dbhost; private $dbuser; private $dbpass; + private $orderwise; + public static function getInstance($dbhost, $dbname, $dbuser, $dbpass, $orderwise = false) + { + if (self::$instance === null) { + self::$instance = new self($dbhost, $dbname, $dbuser, $dbpass, $orderwise); + } + return self::$instance; + } - function __construct($dbhost = '', $dbname = '', $dbuser = '', $dbpass= '') { + function __construct($dbhost = '', $dbname = '', $dbuser = '', $dbpass = '', $orderwise = false) + { $this->dbhost = $dbhost; $this->dbname = $dbname; $this->dbuser = $dbuser; $this->dbpass = $dbpass; + $this->orderwise = $orderwise; $this->connect(); } + // Disallow cloning and unserializing + private function __clone() {} + private function __wakeup() {} + - function prep_query($query){ + function prep_query($query) + { return $this->pdo->prepare($query); } - function connect(){ - if(!$this->pdo){ - - $dsn = 'mysql:dbname=' . $this->dbname . ';host=' . $this->dbhost . ';charset=utf8'; + function connect() + { + if (!$this->pdo) { + if($this->orderwise){ + $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 { - $this->pdo = new PDO($dsn, $user, $password, array(PDO::ATTR_PERSISTENT => true)); + if($this->orderwise){ + $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(); - die($this->error); + // die($this->error); return false; } - }else{ - $this->pdo->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING ); + } else { + $this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING); return true; } } - function table_exists($table_name){ + 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){ + function execute($query, $values = null, $debug = false) + { + if ($values == null) { $values = array(); - }else if(!is_array($values)){ + } else if (!is_array($values)) { $values = array($values); } $stmt = $this->prep_query($query); - $stmt->execute($values); + if($debug){ + echo $query; + print_r($values); + die(); + } + try { + $stmt->execute($values); + } catch (PDOException $e) { + $this->error = $e->getMessage(); + die($query . "
\n" . $this->error); + return false; + } return $stmt; } - function fetch($query, $values = null){ - if($values == null){ + function fetch($query, $values = null) + { + if ($values == null) { $values = array(); - }else if(!is_array($values)){ + } 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){ + function fetchAll($query, $values = null, $key = null) + { + if ($values == null) { $values = array(); - }else if(!is_array($values)){ + } else if (!is_array($values)) { $values = array($values); } $stmt = $this->execute($query, $values); @@ -91,7 +131,7 @@ function fetchAll($query, $values = null, $key = null){ // Allows the user to retrieve results using a // column from the results as a key for the array if(!empty($results)){ - if ($key != null && $results[0][$key]) { + if ($key != null) { $keyed_results = array(); foreach ($results as $result) { $keyed_results[$result[$key]] = $result; @@ -102,8 +142,8 @@ function fetchAll($query, $values = null, $key = null){ return $results; } - function lastInsertId(){ + function lastInsertId() + { return $this->pdo->lastInsertId(); } - } From 7356e616abaa4c11300a9f4f2c1ec464931dddc4 Mon Sep 17 00:00:00 2001 From: Ed Rackham Date: Tue, 7 Jan 2025 09:34:23 +0000 Subject: [PATCH 18/20] Update readme.md --- readme.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/readme.md b/readme.md index c7720b4..e0b457f 100644 --- a/readme.md +++ b/readme.md @@ -25,6 +25,10 @@ To use in your project, use the following: `$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 Before v2.0 ----- From d94dea03c79d902f29d2ca08fdef13bfc8d8e07b Mon Sep 17 00:00:00 2001 From: Ed Rackham Date: Tue, 7 Jan 2025 09:38:09 +0000 Subject: [PATCH 19/20] Update DBPDO.php Removed debug 'dies' --- DBPDO.php | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/DBPDO.php b/DBPDO.php index 507e206..fdb357a 100644 --- a/DBPDO.php +++ b/DBPDO.php @@ -36,8 +36,8 @@ function __construct($dbhost = '', $dbname = '', $dbuser = '', $dbpass = '', $or } // Disallow cloning and unserializing - private function __clone() {} - private function __wakeup() {} + private function __clone() {} + private function __wakeup() {} function prep_query($query) @@ -66,7 +66,6 @@ function connect() return true; } catch (PDOException $e) { $this->error = $e->getMessage(); - // die($this->error); return false; } } else { @@ -101,7 +100,6 @@ function execute($query, $values = null, $debug = false) $stmt->execute($values); } catch (PDOException $e) { $this->error = $e->getMessage(); - die($query . "
\n" . $this->error); return false; } return $stmt; From e74de62049938b80f896082a39de6fabb0eb17d6 Mon Sep 17 00:00:00 2001 From: Ed Rackham Date: Fri, 17 Jan 2025 11:30:26 +0000 Subject: [PATCH 20/20] Removed OW referenced for SQLServer references --- DBPDO.php | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/DBPDO.php b/DBPDO.php index fdb357a..5356d2b 100644 --- a/DBPDO.php +++ b/DBPDO.php @@ -15,23 +15,23 @@ class DBPDO private $dbhost; private $dbuser; private $dbpass; - private $orderwise; + private $sqlserver; - public static function getInstance($dbhost, $dbname, $dbuser, $dbpass, $orderwise = false) + public static function getInstance($dbhost, $dbname, $dbuser, $dbpass, $sqlserver = false) { if (self::$instance === null) { - self::$instance = new self($dbhost, $dbname, $dbuser, $dbpass, $orderwise); + self::$instance = new self($dbhost, $dbname, $dbuser, $dbpass, $sqlserver); } return self::$instance; } - function __construct($dbhost = '', $dbname = '', $dbuser = '', $dbpass = '', $orderwise = false) + function __construct($dbhost = '', $dbname = '', $dbuser = '', $dbpass = '', $sqlserver = false) { $this->dbhost = $dbhost; $this->dbname = $dbname; $this->dbuser = $dbuser; $this->dbpass = $dbpass; - $this->orderwise = $orderwise; + $this->sqlserver = $sqlserver; $this->connect(); } @@ -49,7 +49,7 @@ function prep_query($query) function connect() { if (!$this->pdo) { - if($this->orderwise){ + if($this->sqlserver){ $dsn = 'sqlsrv:Server=' . $this->dbhost . ';Database=' . $this->dbname . ';Encrypt=no'; }else{ $dsn = 'mysql:dbname=' . $this->dbname . ';host=' . $this->dbhost . ';charset=utf8mb4'; @@ -58,7 +58,7 @@ function connect() $password = $this->dbpass; try { - if($this->orderwise){ + if($this->sqlserver){ $this->pdo = new PDO($dsn, $user, $password); }else{ $this->pdo = new PDO($dsn, $user, $password, array(PDO::ATTR_PERSISTENT => true));