Skip to main content

Database

In this document, we will explore various operations that can be performed using the Database class. This class provides methods to interact with a database, including initializing connections, retrieving data, updating records, and more. Below are examples of how to use these methods effectively.

Initializing the Database Connection

$db = new Database();

Getting the Count of Rows in a Table

$table = 'users';
$count = $db->info3($table);
echo "Number of users in $table: $count";

Retrieving the Last Inserted ID

$table = 'orders';
$lastId = $db->get_id3($table);
echo "Last inserted order ID in $table: $lastId";

Updating Records in a Table

$table = 'users';
$id = 1;
$db->update3($table, 'id', $id, ['name' => 'John Doe', 'email' => 'john@example.com']);

Inserting a New Record

$table = 'users';
$newUserId = $db->add3($table, ['name' => 'Jane Doe', 'email' => 'jane@example.com']);
echo "New user ID in $table: $newUserId";

Retrieving a Single Column Value

$table = 'users';
$id = 1;
$email = $db->get_one3($table, $id, 'id', 'email');
echo "User email in $table with ID $id: $email";

Retrieving All Values from a Column

$table = 'users';
$emails = $db->get_col3($table, 'email');
print_r($emails);

Checking if a Row Exists

$table = 'users';
$id = 1;
$exists = $db->get_one_exists3($table, $id, 'id');
echo $exists ? "User exists in $table with ID $id" : "User does not exist in $table with ID $id";

Retrieving a Row as an Associative Array

$table = 'users';
$id = 1;
$user = $db->get_array3($table, $id, 'id');
print_r($user);

Updating a Column Value and Returning All Rows

$table = 'users';
$rows = $db->change3($table, 'status', 'inactive', 'active');
print_r($rows);

Updating a Column Value Based on Another Column's Value

$table = 'users';
$db->add_change3($table, 'status', 'inactive', 'last_login', date('Y-m-d H:i:s'));

Retrieving a Row Based on a Column Value

$table = 'users';
$user = $db->get3($table, 'john@example.com', 'email');
print_r($user);

Deleting a Row Based on a Column Value

$table = 'users';
$id = 1;
$db->delete3($table, 'id', $id);

Checking if a Row Exists Based on a Column Value

$table = 'users';
$email = 'john@example.com';
$exists = $db->exists_row3($table, $email, 'email');
echo $exists ? "Email $email exists in $table" : "Email $email does not exist in $table";