Skip to content

Commit 4c48d2f

Browse files
authored
Merge pull request #1 from a1phanumeric/master
update
2 parents ec3d9af + 0b2916a commit 4c48d2f

File tree

4 files changed

+602
-280
lines changed

4 files changed

+602
-280
lines changed

class.DBPDO.php

+94
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
<?php
2+
3+
class DBPDO {
4+
5+
public $pdo;
6+
private $error;
7+
8+
9+
function __construct() {
10+
$this->connect();
11+
}
12+
13+
14+
function prep_query($query){
15+
return $this->pdo->prepare($query);
16+
}
17+
18+
19+
function connect(){
20+
if(!$this->pdo){
21+
22+
$dsn = 'mysql:dbname=' . DATABASE_NAME . ';host=' . DATABASE_HOST.';charset=utf8';
23+
$user = DATABASE_USER;
24+
$password = DATABASE_PASS;
25+
26+
try {
27+
$this->pdo = new PDO($dsn, $user, $password, array(PDO::ATTR_PERSISTENT => true));
28+
return true;
29+
} catch (PDOException $e) {
30+
$this->error = $e->getMessage();
31+
die($this->error);
32+
return false;
33+
}
34+
}else{
35+
$this->pdo->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING );
36+
return true;
37+
}
38+
}
39+
40+
41+
function table_exists($table_name){
42+
$stmt = $this->prep_query('SHOW TABLES LIKE ?');
43+
$stmt->execute(array($table_name));
44+
return $stmt->rowCount() > 0;
45+
}
46+
47+
48+
function execute($query, $values = null){
49+
if($values == null){
50+
$values = array();
51+
}else if(!is_array($values)){
52+
$values = array($values);
53+
}
54+
$stmt = $this->prep_query($query);
55+
$stmt->execute($values);
56+
return $stmt;
57+
}
58+
59+
function fetch($query, $values = null){
60+
if($values == null){
61+
$values = array();
62+
}else if(!is_array($values)){
63+
$values = array($values);
64+
}
65+
$stmt = $this->execute($query, $values);
66+
return $stmt->fetch(PDO::FETCH_ASSOC);
67+
}
68+
69+
function fetchAll($query, $values = null, $key = null){
70+
if($values == null){
71+
$values = array();
72+
}else if(!is_array($values)){
73+
$values = array($values);
74+
}
75+
$stmt = $this->execute($query, $values);
76+
$results = $stmt->fetchAll(PDO::FETCH_ASSOC);
77+
78+
// Allows the user to retrieve results using a
79+
// column from the results as a key for the array
80+
if($key != null && $results[0][$key]){
81+
$keyed_results = array();
82+
foreach($results as $result){
83+
$keyed_results[$result[$key]] = $result;
84+
}
85+
$results = $keyed_results;
86+
}
87+
return $results;
88+
}
89+
90+
function lastInsertId(){
91+
return $this->pdo->lastInsertId();
92+
}
93+
94+
}

class.MySQL.README.md

+126
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
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 <http://www.gnu.org/licenses/>.

0 commit comments

Comments
 (0)