PHP-SecureDataBaseAccess is a secure, robust PHP class that enhances database interactions using the MySQLi extension. It prioritizes security with prepared statements to prevent SQL injection and features dynamic parameter type detection for flexible query execution. Designed for simplicity and efficiency, this class offers an easy-to-use interface that is ideal for any PHP project requiring secure and reliable database handling.
- Secure Database Connection: Uses MySQLi for improved security and performance.
- Prepared Statements: Automatically prepares SQL statements to prevent SQL injection.
- Dynamic Parameter Binding: Detects parameter types (integer, double, string, blob, null) automatically.
- Last Insert ID: Provides convenient
lastInsertId()method to retrieve the ID of the last inserted record. - Manual Setup: Easy setup without Composer or third-party dependencies.
- PHP 7.4 or higher
- MySQL 5.6 or higher
- MySQLi extension enabled in PHP
-
Clone the Repository
git clone https://github.com/saeed-54996/PHP-SecureDBAccess.git
-
Configure Database Settings
-
Edit the
config.phpfile in the root directory and set your database credentials:<?php return [ 'database' => [ 'host' => 'localhost', 'username' => 'db_user', 'password' => 'db_pass', 'dbname' => 'database_name', ], ];
-
-
Include the Class
require_once 'path/to/Database.php';
-
Create an Instance
$db = new Database();
-
Executing Queries
-
Select Example:
$users = $db->q("SELECT * FROM users WHERE age > ?", [25]); foreach ($users as $user) { echo $user['username'] . PHP_EOL; }
-
Insert Example:
$db->q("INSERT INTO users (username, age) VALUES (?, ?)", ['john_doe', 30]); $lastId = $db->lastInsertId(); echo "Inserted record ID: $lastId";
-
Update/Delete Example:
$db->q("UPDATE users SET age = ? WHERE username = ?", [31, 'john_doe']);
-
Executes a prepared statement and returns an array of results or null.
Returns the ID of the last inserted row.
Contributions, issues, and feature requests are welcome! Feel free to open a pull request.
This project is licensed under the MIT License. See the LICENSE file for details.