|
| 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