├── .gitignore
├── composer.json
├── class.MySQL.README.md
├── DBPDO.php
├── readme.md
└── class.MySQL.php
/.gitignore:
--------------------------------------------------------------------------------
1 | /vendor/
2 |
--------------------------------------------------------------------------------
/composer.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "a1phanumeric/php-mysql-class",
3 | "description": "This is a simple to use PHP MySQL class that easily bolts on to any existing PHP application, streamlining your MySQL interactions.",
4 | "type": "library",
5 | "license": "MIT",
6 | "authors": [
7 | {
8 | "name": "Ed",
9 | "email": "ed.rackham19@gmail.com"
10 | }
11 | ],
12 | "require": {},
13 | "autoload": {
14 | "psr-4": { "A1phanumeric\\": "" }
15 | }
16 | }
17 |
--------------------------------------------------------------------------------
/class.MySQL.README.md:
--------------------------------------------------------------------------------
1 | PHP MySQL Class
2 | ===============
3 |
4 | This is the README for the class.MySQL.php class that I no longer support as the `mysql_*` functions are deprecated.
5 |
6 | This is a simple to use MySQL class that easily bolts on to any existing PHP application, streamlining your MySQL interactions.
7 |
8 |
9 | Latest Changes
10 | --------------
11 |
12 | I have refactored the entire class, and improved the code somewhat. This means that some things now work differently to the original version.
13 |
14 |
15 | Setup
16 | -----
17 |
18 | Simply include this class into your project like so:
19 |
20 | `include_once('/path/to/class.MySQL.php');`
21 |
22 | Then invoke the class in your project using the class constructor (which now sets the db credentials):
23 |
24 | `$oMySQL = new MySQL(MYSQL_NAME, MYSQL_USER, MYSQL_PASS, [MYSQL_HOST]);`
25 |
26 | `MYSQL_NAME` The name of your database
27 |
28 | `MYSQL_USER` Your username for the server / database
29 |
30 | `MYSQL_PASS` Your password for the server / database
31 |
32 | `MYSQL_HOST` The hostname of the MySQL server (*optional*, defaults to 'localhost')
33 |
34 |
35 | Usage
36 | -----
37 |
38 | To use this class, you'd first init the object like so (using example credentials):
39 |
40 | `$oMySQL = new MySQL('my_database','username','password');`
41 |
42 | Provided you see no errors, you are now connected and can execute full MySQL queries using:
43 |
44 | `$oMySQL->ExecuteSQL($query);`
45 |
46 | `ExecuteSQL()` will return an array of results, or a true (if an UPDATE or DELETE).
47 |
48 | There are other functions such as `Insert()`, `Delete()` and `Select()` which may or may not help with your queries to the database.
49 |
50 | Example
51 | -------
52 |
53 | To show you how easy this class is to use, consider you have a table called *admin*, which contains the following:
54 |
55 | ```
56 | +----+--------------+
57 | | id | username |
58 | +----+--------------+
59 | | 1 | superuser |
60 | | 2 | a1phanumeric |
61 | +----+--------------+
62 | ```
63 |
64 | To add a user, you'd simply use:
65 |
66 | ```
67 | $newUser = array('username' => 'Thrackhamator');
68 | $oMySQL->Insert($newUser, 'admin');
69 | ```
70 |
71 | And voila:
72 |
73 | ```
74 | +----+---------------+
75 | | id | username |
76 | +----+---------------+
77 | | 1 | superuser |
78 | | 2 | a1phanumeric |
79 | | 3 | Thrackhamator |
80 | +----+---------------+
81 | ```
82 |
83 | To get the results into a usable array, just use `$oMySQL->Select('admin')` ...for example, doing the following:
84 |
85 | `print_r($oMySQL->Select('admin'));`
86 |
87 | will yield:
88 |
89 | ```
90 | Array
91 | (
92 | [0] => Array
93 | (
94 | [id] => 1
95 | [username] => superuser
96 | )
97 |
98 | [1] => Array
99 | (
100 | [id] => 2
101 | [username] => a1phanumeric
102 | )
103 |
104 | [2] => Array
105 | (
106 | [id] => 3
107 | [username] => Thrackhamator
108 | )
109 |
110 | )
111 | ```
112 |
113 | ### License
114 |
115 | This program is free software: you can redistribute it and/or modify
116 | it under the terms of the GNU General Public License as published by
117 | the Free Software Foundation, either version 3 of the License, or
118 | (at your option) any later version.
119 |
120 | This program is distributed in the hope that it will be useful,
121 | but WITHOUT ANY WARRANTY; without even the implied warranty of
122 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
123 | GNU General Public License for more details.
124 |
125 | You should have received a copy of the GNU General Public License
126 | along with this program. If not, see .
--------------------------------------------------------------------------------
/DBPDO.php:
--------------------------------------------------------------------------------
1 | dbhost = $dbhost;
31 | $this->dbname = $dbname;
32 | $this->dbuser = $dbuser;
33 | $this->dbpass = $dbpass;
34 | $this->sqlserver = $sqlserver;
35 | $this->connect();
36 | }
37 |
38 | // Disallow cloning and unserializing
39 | private function __clone() {}
40 | private function __wakeup() {}
41 |
42 |
43 | function prep_query($query)
44 | {
45 | return $this->pdo->prepare($query);
46 | }
47 |
48 |
49 | function connect()
50 | {
51 | if (!$this->pdo) {
52 | if($this->sqlserver){
53 | $dsn = 'sqlsrv:Server=' . $this->dbhost . ';Database=' . $this->dbname . ';Encrypt=no';
54 | }else{
55 | $dsn = 'mysql:dbname=' . $this->dbname . ';host=' . $this->dbhost . ';charset=utf8mb4';
56 | }
57 | $user = $this->dbuser;
58 | $password = $this->dbpass;
59 |
60 | try {
61 | if($this->sqlserver){
62 | $this->pdo = new PDO($dsn, $user, $password);
63 | }else{
64 | $this->pdo = new PDO($dsn, $user, $password, array(PDO::ATTR_PERSISTENT => true));
65 | }
66 | return true;
67 | } catch (PDOException $e) {
68 | $this->error = $e->getMessage();
69 | return false;
70 | }
71 | } else {
72 | $this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
73 | return true;
74 | }
75 | }
76 |
77 |
78 | function table_exists($table_name)
79 | {
80 | $stmt = $this->prep_query('SHOW TABLES LIKE ?');
81 | $stmt->execute(array($table_name));
82 | return $stmt->rowCount() > 0;
83 | }
84 |
85 |
86 | function execute($query, $values = null, $debug = false)
87 | {
88 | if ($values == null) {
89 | $values = array();
90 | } else if (!is_array($values)) {
91 | $values = array($values);
92 | }
93 | $stmt = $this->prep_query($query);
94 | if($debug){
95 | echo $query;
96 | print_r($values);
97 | die();
98 | }
99 | try {
100 | $stmt->execute($values);
101 | } catch (PDOException $e) {
102 | $this->error = $e->getMessage();
103 | return false;
104 | }
105 | return $stmt;
106 | }
107 |
108 | function fetch($query, $values = null)
109 | {
110 | if ($values == null) {
111 | $values = array();
112 | } else if (!is_array($values)) {
113 | $values = array($values);
114 | }
115 | $stmt = $this->execute($query, $values);
116 | return $stmt->fetch(PDO::FETCH_ASSOC);
117 | }
118 |
119 | function fetchAll($query, $values = null, $key = null)
120 | {
121 | if ($values == null) {
122 | $values = array();
123 | } else if (!is_array($values)) {
124 | $values = array($values);
125 | }
126 | $stmt = $this->execute($query, $values);
127 | $results = $stmt->fetchAll(PDO::FETCH_ASSOC);
128 |
129 | // Allows the user to retrieve results using a
130 | // column from the results as a key for the array
131 | if(!empty($results)){
132 | if ($key != null) {
133 | $keyed_results = array();
134 | foreach ($results as $result) {
135 | $keyed_results[$result[$key]] = $result;
136 | }
137 | $results = $keyed_results;
138 | }
139 | }
140 | return $results;
141 | }
142 |
143 | function lastInsertId()
144 | {
145 | return $this->pdo->lastInsertId();
146 | }
147 | }
148 |
--------------------------------------------------------------------------------
/readme.md:
--------------------------------------------------------------------------------
1 | Important Notice
2 | ===============
3 |
4 | As of December 2014 I decided to upload the PHP MySQL Class I wrote a while back, and now use on a daily basis. It's PDO based (the `mysql_*` functions were due to be deprecated quite a while back now!).
5 |
6 | The old version is still a part of this repo for now, and the readme is still available [here](class.MySQL.README.md).
7 |
8 |
9 |
10 | PHP MySQL Class
11 | ===============
12 |
13 | This is a simple to use MySQL class that easily bolts on to any existing PHP application, streamlining your MySQL interactions.
14 |
15 | Setup v2.0+
16 | -----
17 |
18 | Include the class using composer as below:
19 |
20 | `composer require a1phanumeric/php-mysql-class`
21 |
22 | To use in your project, use the following:
23 |
24 | `use A1phanumeric\DBPDO;`
25 |
26 | `$DB = new DBPDO('db_host', 'db_name', 'db_user', 'db_pass');`
27 |
28 | Or, if wanting to use as a singleton instance:
29 |
30 | `$DB = DBPDO::getInstance('db_host', 'db_name', 'db_user', 'db_pass');`
31 |
32 | ### Setup Before v2.0
33 | -----
34 |
35 | Firstly, define four constants for the host, database name, username and password:
36 |
37 | `define('DATABASE_NAME', 'my_database');`
38 |
39 | `define('DATABASE_USER', 'username');`
40 |
41 | `define('DATABASE_PASS', 'password');`
42 |
43 | `define('DATABASE_HOST', 'localhost');`
44 |
45 | Then, simply include this class into your project like so:
46 |
47 | `include_once('/path/to/class.DBPDO.php');`
48 |
49 | Then invoke the class:
50 |
51 | `$DB = new DBPDO();`
52 |
53 |
54 | Direct Queries
55 | -----
56 |
57 | To perform direct queries where you don't need to return any results (such as update, insert etc...), just do the following:
58 |
59 | `$DB->execute("UPDATE customers SET email = 'newemail@domain.com' WHERE username = 'a1phanumeric'");`
60 |
61 | That's the easiest way to use the class, but we should be utilising prepared statements now. This means no more escaping shizzle! To utilise prepared statements, just change the above code to the following:
62 |
63 | `$DB->execute("UPDATE customers SET email = ? WHERE username = ?", array('newemail@domain.com', 'a1phanumeric'));`
64 |
65 | The class will invoke PDO's prepared statements and put the email and username in their place respectively, as well as escape all values passed to it. **Note:** You don't need to put the speechmarks in on the query, the **?** is enough, and PDO will sort that out for you.
66 |
67 |
68 | Fetching Rows
69 | -----
70 |
71 | To perform select queries with this class, the syntax is similar to the above, but we have two functions we can utilise, `fetch` and `fetchAll`.
72 |
73 | `fetch` simply returns one row, useful for getting a user by their ID for example. This returns an associative array and looks like:
74 |
75 | `$user = $DB->fetch("SELECT * FROM users WHERE id = ?", $id);`
76 |
77 | Now `$user` will contain an array of the fields for the row where there query matches. Oh, what's that? We didn't pass an array as the second parameter we just passed a single variable? That's cool, the class will treat a single variable the same as if you passed `array($id)`. It's just a handy little time-saver.
78 |
79 | `fetchAll` is used to fetch multiple rows, the parameters are similar, but the result returns an array of records:
80 |
81 | `$counties = $DB->fetchAll("SELECT * FROM counties");`
82 |
83 | The above will return a list of counties (in the UK) in my database like so:
84 |
85 | ```
86 | [0] => Array
87 | (
88 | [id] => 1
89 | [county] => London
90 | )
91 |
92 | [1] => Array
93 | (
94 | [id] => 2
95 | [county] => Bedfordshire
96 | )
97 |
98 | [2] => Array
99 | (
100 | [id] => 3
101 | [county] => Buckinghamshire
102 | )
103 | ```
104 |
105 | However, what if I want to loop over some raw data and check if the data matches the county name? To do that means either looping over these results every time, or shifting the key to the root dimension of the multi-dimensional array. However, if we pass a third variable, we can have that column as the key:
106 |
107 | `$counties = $DB->fetchAll("SELECT * FROM counties", null, 'county');`
108 |
109 | **Note:** I passed null as the second paramater as we're not passing any variables into the query to be escaped.
110 |
111 | This will now return an array like the following:
112 |
113 | ```
114 | [London] => Array
115 | (
116 | [id] => 1
117 | [county] => London
118 | )
119 |
120 | [Bedfordshire] => Array
121 | (
122 | [id] => 2
123 | [county] => Bedfordshire
124 | )
125 |
126 | [Buckinghamshire] => Array
127 | (
128 | [id] => 3
129 | [county] => Buckinghamshire
130 | )
131 | ```
132 |
133 | So of course we could now do something like:
134 |
135 | `if(isset($counties[$raw_data['county_name']])){ //Do something }`
136 |
137 | ### License
138 |
139 | This program is free software: you can redistribute it and/or modify
140 | it under the terms of the GNU General Public License as published by
141 | the Free Software Foundation, either version 3 of the License, or
142 | (at your option) any later version.
143 |
144 | This program is distributed in the hope that it will be useful,
145 | but WITHOUT ANY WARRANTY; without even the implied warranty of
146 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
147 | GNU General Public License for more details.
148 |
149 | You should have received a copy of the GNU General Public License
150 | along with this program. If not, see .
151 |
--------------------------------------------------------------------------------
/class.MySQL.php:
--------------------------------------------------------------------------------
1 | .
20 | */
21 |
22 | // MySQL Class v0.8.1
23 | class MySQL {
24 |
25 | // Base variables
26 | public $lastError; // Holds the last error
27 | public $lastQuery; // Holds the last query
28 | public $result; // Holds the MySQL query result
29 | public $records; // Holds the total number of records returned
30 | public $affected; // Holds the total number of records affected
31 | public $rawResults; // Holds raw 'arrayed' results
32 | public $arrayedResult; // Holds an array of the result
33 |
34 | private $hostname; // MySQL Hostname
35 | private $username; // MySQL Username
36 | private $password; // MySQL Password
37 | private $database; // MySQL Database
38 |
39 | private $databaseLink; // Database Connection Link
40 |
41 |
42 |
43 | /* *******************
44 | * Class Constructor *
45 | * *******************/
46 |
47 | function __construct($database, $username, $password, $hostname='localhost', $port=3306, $persistant = false){
48 | $this->database = $database;
49 | $this->username = $username;
50 | $this->password = $password;
51 | $this->hostname = $hostname.':'.$port;
52 |
53 | $this->Connect($persistant);
54 | }
55 |
56 | /* *******************
57 | * Class Destructor *
58 | * *******************/
59 |
60 | function __destruct(){
61 | $this->closeConnection();
62 | }
63 |
64 | /* *******************
65 | * Private Functions *
66 | * *******************/
67 |
68 | // Connects class to database
69 | // $persistant (boolean) - Use persistant connection?
70 | private function Connect($persistant = false){
71 | $this->CloseConnection();
72 |
73 | if($persistant){
74 | $this->databaseLink = mysql_pconnect($this->hostname, $this->username, $this->password);
75 | }else{
76 | $this->databaseLink = mysql_connect($this->hostname, $this->username, $this->password);
77 | }
78 |
79 | if(!$this->databaseLink){
80 | $this->lastError = 'Could not connect to server: ' . mysql_error($this->databaseLink);
81 | return false;
82 | }
83 |
84 | if(!$this->UseDB()){
85 | $this->lastError = 'Could not connect to database: ' . mysql_error($this->databaseLink);
86 | return false;
87 | }
88 |
89 | $this->setCharset(); // TODO: remove forced charset find out a specific management
90 | return true;
91 | }
92 |
93 |
94 | // Select database to use
95 | private function UseDB(){
96 | if(!mysql_select_db($this->database, $this->databaseLink)){
97 | $this->lastError = 'Cannot select database: ' . mysql_error($this->databaseLink);
98 | return false;
99 | }else{
100 | return true;
101 | }
102 | }
103 |
104 |
105 | // Performs a 'mysql_real_escape_string' on the entire array/string
106 | private function SecureData($data, $types=array()){
107 | if(is_array($data)){
108 | $i = 0;
109 | foreach($data as $key=>$val){
110 | if(!is_array($data[$key])){
111 | $data[$key] = $this->CleanData($data[$key], $types[$i]);
112 | $data[$key] = mysql_real_escape_string($data[$key], $this->databaseLink);
113 | $i++;
114 | }
115 | }
116 | }else{
117 | $data = $this->CleanData($data, $types);
118 | $data = mysql_real_escape_string($data, $this->databaseLink);
119 | }
120 | return $data;
121 | }
122 |
123 | // clean the variable with given types
124 | // possible types: none, str, int, float, bool, datetime, ts2dt (given timestamp convert to mysql datetime)
125 | // bonus types: hexcolor, email
126 | private function CleanData($data, $type = ''){
127 | switch($type) {
128 | case 'none':
129 | // useless do not reaffect just do nothing
130 | //$data = $data;
131 | break;
132 | case 'str':
133 | case 'string':
134 | settype( $data, 'string');
135 | break;
136 | case 'int':
137 | case 'integer':
138 | settype( $data, 'integer');
139 | break;
140 | case 'float':
141 | settype( $data, 'float');
142 | break;
143 | case 'bool':
144 | case 'boolean':
145 | settype( $data, 'boolean');
146 | break;
147 | // Y-m-d H:i:s
148 | // 2014-01-01 12:30:30
149 | case 'datetime':
150 | $data = trim( $data );
151 | $data = preg_replace('/[^\d\-: ]/i', '', $data);
152 | preg_match( '/^([\d]{4}-[\d]{2}-[\d]{2} [\d]{2}:[\d]{2}:[\d]{2})$/', $data, $matches );
153 | $data = $matches[1];
154 | break;
155 | case 'ts2dt':
156 | settype( $data, 'integer');
157 | $data = date('Y-m-d H:i:s', $data);
158 | break;
159 |
160 | // bonus types
161 | case 'hexcolor':
162 | preg_match( '/(#[0-9abcdef]{6})/i', $data, $matches );
163 | $data = $matches[1];
164 | break;
165 | case 'email':
166 | $data = filter_var($data, FILTER_VALIDATE_EMAIL);
167 | break;
168 | default:
169 | break;
170 | }
171 | return $data;
172 | }
173 |
174 |
175 |
176 | /* ******************
177 | * Public Functions *
178 | * ******************/
179 |
180 | // Executes MySQL query
181 | public function executeSQL($query){
182 | $this->lastQuery = $query;
183 | if($this->result = mysql_query($query, $this->databaseLink)){
184 | if (gettype($this->result) === 'resource') {
185 | $this->records = @mysql_num_rows($this->result);
186 | } else {
187 | $this->records = 0;
188 | }
189 | $this->affected = @mysql_affected_rows($this->databaseLink);
190 |
191 | if($this->records > 0){
192 | $this->arrayResults();
193 | return $this->arrayedResult;
194 | }else{
195 | return true;
196 | }
197 |
198 | }else{
199 | $this->lastError = mysql_error($this->databaseLink);
200 | return false;
201 | }
202 | }
203 |
204 | public function commit(){
205 | return mysql_query("COMMIT", $this->databaseLink);
206 | }
207 |
208 | public function rollback(){
209 | return mysql_query("ROLLBACK", $this->databaseLink);
210 | }
211 |
212 | public function setCharset( $charset = 'UTF8' ) {
213 | return mysql_set_charset ( $this->SecureData($charset,'string'), $this->databaseLink);
214 | }
215 |
216 | // Adds a record to the database based on the array key names
217 | public function insert($table, $vars, $exclude = '', $datatypes=array()){
218 |
219 | // Catch Exclusions
220 | if($exclude == ''){
221 | $exclude = array();
222 | }
223 |
224 | array_push($exclude, 'MAX_FILE_SIZE'); // Automatically exclude this one
225 |
226 | // Prepare Variables
227 | $vars = $this->SecureData($vars, $datatypes);
228 |
229 | $query = "INSERT INTO `{$table}` SET ";
230 | foreach($vars as $key=>$value){
231 | if(in_array($key, $exclude)){
232 | continue;
233 | }
234 | $query .= "`{$key}` = '{$value}', ";
235 | }
236 |
237 | $query = trim($query, ', ');
238 |
239 | return $this->executeSQL($query);
240 | }
241 |
242 | // Deletes a record from the database
243 | public function delete($table, $where='', $limit='', $like=false, $wheretypes=array()){
244 | $query = "DELETE FROM `{$table}` WHERE ";
245 | if(is_array($where) && $where != ''){
246 | // Prepare Variables
247 | $where = $this->SecureData($where, $wheretypes);
248 |
249 | foreach($where as $key=>$value){
250 | if($like){
251 | $query .= "`{$key}` LIKE '%{$value}%' AND ";
252 | }else{
253 | $query .= "`{$key}` = '{$value}' AND ";
254 | }
255 | }
256 |
257 | $query = substr($query, 0, -5);
258 | }
259 |
260 | if($limit != ''){
261 | $query .= ' LIMIT ' . $limit;
262 | }
263 |
264 | $result = $this->executeSQL($query);
265 |
266 | if($this->affected == 0){
267 | return false;
268 | }
269 |
270 | return $result;
271 | }
272 |
273 |
274 | // Gets a single row from $from where $where is true
275 | public function select($from, $where='', $orderBy='', $limit='', $like=false, $operand='AND',$cols='*', $wheretypes=array()){
276 | // Catch Exceptions
277 | if(trim($from) == ''){
278 | return false;
279 | }
280 |
281 | $query = "SELECT {$cols} FROM `{$from}` WHERE ";
282 |
283 | if(is_array($where) && $where != ''){
284 | // Prepare Variables
285 | $where = $this->SecureData($where, $wheretypes);
286 |
287 | foreach($where as $key=>$value){
288 | if($like){
289 | $query .= "`{$key}` LIKE '%{$value}%' {$operand} ";
290 | }else{
291 | $query .= "`{$key}` = '{$value}' {$operand} ";
292 | }
293 | }
294 |
295 | $query = substr($query, 0, -(strlen($operand)+2));
296 |
297 | }else{
298 | $query = substr($query, 0, -6);
299 | }
300 |
301 | if($orderBy != ''){
302 | $query .= ' ORDER BY ' . $orderBy;
303 | }
304 |
305 | if($limit != ''){
306 | $query .= ' LIMIT ' . $limit;
307 | }
308 |
309 | $result = $this->executeSQL($query);
310 | if(is_array($result)) return $result;
311 | return array();
312 |
313 | }
314 |
315 | // Updates a record in the database based on WHERE
316 | public function update($table, $set, $where, $exclude = '', $datatypes=array(), $wheretypes=array()){
317 | // Catch Exceptions
318 | if(trim($table) == '' || !is_array($set) || !is_array($where)){
319 | return false;
320 | }
321 | if($exclude == ''){
322 | $exclude = array();
323 | }
324 |
325 | array_push($exclude, 'MAX_FILE_SIZE'); // Automatically exclude this one
326 |
327 | $set = $this->SecureData($set, $datatypes);
328 | $where = $this->SecureData($where,$wheretypes);
329 |
330 | // SET
331 |
332 | $query = "UPDATE `{$table}` SET ";
333 |
334 | foreach($set as $key=>$value){
335 | if(in_array($key, $exclude)){
336 | continue;
337 | }
338 | $query .= "`{$key}` = '{$value}', ";
339 | }
340 |
341 | $query = substr($query, 0, -2);
342 |
343 | // WHERE
344 |
345 | $query .= ' WHERE ';
346 |
347 | foreach($where as $key=>$value){
348 | $query .= "`{$key}` = '{$value}' AND ";
349 | }
350 |
351 | $query = substr($query, 0, -5);
352 |
353 | $result = $this->executeSQL($query);
354 |
355 | if($this->affected == 0){
356 | return false;
357 | }
358 |
359 | return $result;
360 | }
361 |
362 | // 'Arrays' a single result
363 | public function arrayResult(){
364 | $this->arrayedResult = mysql_fetch_assoc($this->result) or die (mysql_error($this->databaseLink));
365 | return $this->arrayedResult;
366 | }
367 |
368 | // 'Arrays' multiple result
369 | public function arrayResults(){
370 |
371 | if($this->records == 1){
372 | return $this->arrayResult();
373 | }
374 |
375 | $this->arrayedResult = array();
376 | while ($data = mysql_fetch_assoc($this->result)){
377 | $this->arrayedResult[] = $data;
378 | }
379 | return $this->arrayedResult;
380 | }
381 |
382 | // 'Arrays' multiple results with a key
383 | public function arrayResultsWithKey($key='id'){
384 | if(isset($this->arrayedResult)){
385 | unset($this->arrayedResult);
386 | }
387 | $this->arrayedResult = array();
388 | while($row = mysql_fetch_assoc($this->result)){
389 | foreach($row as $theKey => $theValue){
390 | $this->arrayedResult[$row[$key]][$theKey] = $theValue;
391 | }
392 | }
393 | return $this->arrayedResult;
394 | }
395 |
396 | // Returns last insert ID
397 | public function lastInsertID(){
398 | return mysql_insert_id($this->databaseLink);
399 | }
400 |
401 | // Return number of rows
402 | public function countRows($from, $where=''){
403 | $result = $this->select($from, $where, '', '', false, 'AND','count(*)');
404 | return $result["count(*)"];
405 | }
406 |
407 | // Closes the connections
408 | public function closeConnection(){
409 | if($this->databaseLink){
410 | // Commit before closing just in case :)
411 | $this->commit();
412 | mysql_close($this->databaseLink);
413 | }
414 | }
415 | }
416 |
--------------------------------------------------------------------------------