PHDB is a PHP library for basic database management operations. It offers convenient methods to connect to a database, perform CRUD operations, execute queries, and manage database tables. This library aims to provide ease of use and flexibility for database interaction in PHP applications.
- Connect: Establish a connection to the database.
- Error handling : Error handling mode.
- Disconnect: Close the database connection.
- Query: Execute a SQL query and return the resulting mysqli_result.
- Insert: Insert a record into the database.
- Update: Update records in the database.
- Delete: Delete records from the database.
- Select: Retrieve records from the database based on specified conditions.
- Specific Select: Perform a specific selection from the database.
- Get Value: Retrieve a single value from the database.
- Get Specific Value: Retrieve a specific value from the database.
- Create Table: Create a new table in the database.
- Drop Table: Drop an existing table from the database.
- Alter Table: Alter an existing table in the database.
- Truncate Table: Remove all records from a table.
- Find By: Find records in the database based on specific conditions.
- Paginate: Paginate results from the database.
- Delete By: Delete records from the database based on specific conditions.
To connect to the database:
PHDB::$host = 'localhost';
PHDB::$username = 'username';
PHDB::$password = 'password';
PHDB::$dbname = 'database_name';
PHDB::$error = true;
PHDB::connect();
To insert a record into the database:
$data = ['name' => 'John', 'email' => 'john@example.com'];
PHDB::insert('users', $data);
To update records in the database:
$data = ['email' => 'jane@example.com'];
PHDB::update('users', $data, ['name' => 'Jane']);
sql Copy code
To delete records from the database:
PHDB::delete('users', ['name' => 'John']);
To retrieve records from the database:
$result = PHDB::select('users', '*', ['name' => 'John']);
while ($row = $result->fetch_assoc()) {
// Process the fetched row
}
To perform a specific selection from the database:
$result = PHDB::specificSelect('SELECT * FROM users WHERE name = ?', ['John']);
while ($row = $result->fetch_assoc()) {
// Process the fetched row
}
To retrieve a single value from the database:
$value = PHDB::getValue('users', 'email', ['name' => 'John']);
To retrieve a specific value from the database:
$value = PHDB::getSpecificValue('SELECT email FROM users WHERE name = ?', ['John']);
To create a new table in the database:
$columns = ['id' => 'INT AUTO_INCREMENT PRIMARY KEY', 'name' => 'VARCHAR(255)', 'email' => 'VARCHAR(255)'];
PHDB::createTable('users', $columns);
To drop an existing table from the database:
PHDB::dropTable('users');
To alter an existing table in the database:
$changes = ['ADD COLUMN age INT', 'DROP COLUMN address'];
PHDB::alterTable('users', $changes);
To remove all records from a table:
PHDB::truncateTable('users');
To find records in the database based on specific conditions:
$result = PHDB::findBy('users', ['name' => 'John'], 'name, email');
while ($row = $result->fetch_assoc()) {
// Process the fetched row
}
To delete records from the database based on specific conditions:
PHDB::deleteBy('users', ['name' => 'John']);
To paginate results from database:
paginate($table, $columns = '*', $where = [], $page = 1, $per_page = 10)
To close the database connection:
PHDB::disconnect();
OR
PHDB::close();