Skip to content

The Database class is a versatile PHP tool using mysqli for database management. It handles essential CRUD operations (Create, Read, Update, Delete) and includes methods for user authentication and password hashing, making database interactions and user management simple and secure.

Notifications You must be signed in to change notification settings

Iqbolshoh/php-database-manager

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

24 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Database Class

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.

Banner Image

Table of Contents


Constructor and Destructor

__construct()

  • 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
    • Displays an error message and exits if the connection fails.

__destruct()

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

Methods

  • 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.
  • 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's prepare and bind_param methods to prevent SQL injection.
  • 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.
  • 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).
  • 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.

Examples

Here are some examples of how to use the Database class for car-related data:

Inserting Car 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!";
}

Updating Car Data

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!";
}

Selecting 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!";
}

Deleting 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!";
}

Technologies Used

PHP MySQL

Installation

To get started with the Database class, follow these steps:

  1. Download the repository or clone it using Git:

    git clone <repository_url>
  2. Upload the config.php file to your project directory.

  3. Include the class in your PHP file where you need to interact with the database:

    require_once 'config.php';
  4. Setup your database connection (MySQL or MariaDB) with the parameters defined in the class (server, username, password, database name).

  5. Start using the class methods to interact with your database!


Contributing

Contributions are welcome! If you have suggestions or want to enhance the project, feel free to fork the repository and submit a pull request.

Connect with Me

I love connecting with new people and exploring new opportunities. Feel free to reach out to me through any of the platforms below:

GitHub Telegram LinkedIn instagram WhatsApp Twitter Email

About

The Database class is a versatile PHP tool using mysqli for database management. It handles essential CRUD operations (Create, Read, Update, Delete) and includes methods for user authentication and password hashing, making database interactions and user management simple and secure.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages