The Database
class provides a simple and effective interface for handling common database operations using PHP's mysqli
extension. It includes methods for connecting to the database, executing queries, and performing basic CRUD (Create, Read, Update, Delete) operations. The class also features password hashing and user authentication.
- Description: Initializes a connection to the database.
- Parameters: None
- Details:
- Connects to a MySQL database using the
mysqli
extension. - Connection parameters are hard-coded:
- Server:
localhost
- Username:
root
- Password:
""
(empty string) - Database Name:
database
- Server:
- Displays an error message and exits if the connection fails.
- Connects to a MySQL database using the
- Description: Closes the database connection when the object is destroyed.
- Parameters: None
- Details:
- Ensures the database connection is properly closed when the object is no longer needed.
- Description: Executes a given SQL query.
- Parameters:
- $sql: The SQL query to be executed.
- Returns: The result of the query.
- Details:
- Displays an error message and exits if the query execution fails.
- This method is a low-level function that allows the execution of any SQL statement.
- Description: Escapes special characters to prevent SQL injection.
- Parameters:
- $data: An associative array of data to be sanitized.
- Returns: The sanitized data array.
- Details:
- Removes whitespace from the beginning and end.
- Removes backslashes.
- Converts special characters to HTML entities to prevent SQL injection and XSS (Cross-Site Scripting) vulnerabilities.
- Description: Retrieves data from the specified table.
- Parameters:
- $table: The name of the table to select data from.
- $columns: A comma-separated list of columns to retrieve (default is
*
for all columns). - $condition: An optional SQL condition (e.g.,
WHERE id = 1
).
- Returns: An associative array of the result set.
- Details:
- Constructs and executes a
SELECT
query to fetch data from the specified table. - If no condition is provided, all rows are selected by default.
- Constructs and executes a
- Description: Inserts data into the specified table.
- Parameters:
- $table: The name of the table to insert data into.
- $data: An associative array of column names and values.
- Returns: The result of the query execution.
- Details:
- Constructs and executes an
INSERT
query to insert data into the table. - Uses
mysqli
'sprepare
andbind_param
methods to prevent SQL injection.
- Constructs and executes an
- Description: Updates data in the specified table.
- Parameters:
- $table: The name of the table to update.
- $data: An associative array of column names and new values.
- $condition: An optional SQL condition (e.g.,
WHERE id = 1
).
- Returns: The result of the query execution.
- Details:
- Constructs and executes an
UPDATE
query to modify existing data in the table. - The
condition
is used to specify which records to update.
- Constructs and executes an
- Description: Deletes data from the specified table.
- Parameters:
- $table: The name of the table to delete data from.
- $condition: An optional SQL condition (e.g.,
WHERE id = 1
).
- Returns: The result of the query execution.
- Details:
- Constructs and executes a
DELETE
query to remove data from the table. - If no condition is provided, all rows in the table will be deleted (use cautiously).
- Constructs and executes a
- Description: Hashes a password using HMAC with SHA-256.
- Parameters:
- $password: The plain text password to be hashed.
- Returns: The hashed password.
- Details:
- Uses the
hash_hmac()
function with SHA-256 hashing algorithm and a hard-coded key"AccountPassword"
for secure password storage. - The resulting hash is intended for password verification purposes.
- Uses the
Here are some examples of how to use the Database
class for car-related data:
include './config.php';
$query = new Database();
$data = [
'make' => 'Toyota',
'model' => 'Corolla',
'year' => 2022,
'price' => 25000.99
];
$result = $query->insert('cars', $data);
if ($result) {
echo "Car added successfully!";
} else {
echo "Error adding car!";
}
include './config.php';
$query = new Database();
$data = [
'price' => 23000.99
];
$result = $query->update('cars', $data, "WHERE model = 'Corolla' AND year = 2022");
if ($result) {
echo "Car data updated successfully!";
} else {
echo "Error updating car data!";
}
include './config.php';
$query = new Database();
$carData = $query->select('cars', '*', "WHERE model = 'Corolla' AND year = 2022");
if ($carData) {
print_r($carData);
} else {
echo "Error retrieving car data!";
}
include './config.php';
$query = new Database();
$result = $query->delete('cars', "WHERE model = 'Corolla' AND year = 2022");
if ($result) {
echo "Car deleted successfully!";
} else {
echo "Error deleting car!";
}
To get started with the Database
class, follow these steps:
-
Download the repository or clone it using Git:
git clone <repository_url>
-
Upload the
config.php
file to your project directory. -
Include the class in your PHP file where you need to interact with the database:
require_once 'config.php';
-
Setup your database connection (MySQL or MariaDB) with the parameters defined in the class (server, username, password, database name).
-
Start using the class methods to interact with your database!
Contributions are welcome! If you have suggestions or want to enhance the project, feel free to fork the repository and submit a pull request.
I love connecting with new people and exploring new opportunities. Feel free to reach out to me through any of the platforms below: