The first thing you'll notice about this class is that it is not a class (but it does interface with one). A class in this case is just too much trouble for every time you want to interact with your database. It is much easier to have these standard functions that you can use anywhere and everywhere, and not have to worry about a $mysqli or class object. These functions will:
A good place to store these functions (along with your sensitive database information) is in your website's root directory. When you need to connect to your database, just include the file:
include_once (BASE . 'database.php');
This function will escape any problematic characters, and make a string safe to insert into your database. This function is used by default when validating data in our Form Class.
| $data | The data to be escaped. |
| Returns | Your escaped data. |
| Example | $username = escape_data ("Hacker'); DROP TABLE users;--"); |
This function executes your $query, let's you know if there's a problem, and returns the $result.
| $query | Your database query. |
| Returns | The $result of your query. |
| Example | $result = db_query ("SELECT data FROM table WHERE column='{$value}'");
while (list($data) = $result->fetch_row()) {
$html .= $data . '<br />';
} |
This function makes it a lot easier to put together an insert query, and to see at a glance what is going on.
| $table | The database table you're inserting records into. |
| $array | Key (column) and value pairs of data. |
| Returns | The auto generated id of your inserted data. |
| Example | $insert = array();
$insert['name'] = 'username';
$insert['password'] = 'password';
$insert['email'] = 'email@address.com';
$insert['registered'] = 'NOW()';
$userid = db_insert('users', $insert); |
This function makes it a lot easier to put together an update query, and to see at a glance what is going on.
| $table | The database table whose record(s) you're updating. |
| $array | Key (column) and value pairs of data. |
| $column | The column you're using to define what exactly should be updated (usually the table's auto generated id column). |
| $id | The id (or value) of the column you'd like to update. |
| $add | If you have any other conditional statements that you would like to make then you can say so here. |
| Returns | The number of rows your update affected. |
| Example | $update = array();
$update['confirmed'] = 'Y';
db_update ('users', $update, 'user_id', 1); |