-
Notifications
You must be signed in to change notification settings - Fork 0
Examples
Erik Thiart edited this page Oct 2, 2023
·
1 revision
// Simple Example
$dataToInsert = [
'name' => 'John Doe',
'email' => '[email protected]',
];
$db->insert('users', $dataToInsert);
// Advanced Example with Error Handling
$dataToInsert = [
'name' => 'Jane Smith',
'email' => '[email protected]',
];
if ($db->insert('users', $dataToInsert)) {
echo 'User inserted successfully.';
} else {
echo 'Error inserting user.';
}
// Simple Example
$updateData = [
'status' => 'inactive',
];
$whereConditions = [
'id' => 1,
];
$db->update('users', $updateData, $whereConditions);
// Advanced Example with Error Handling
$updateData = [
'status' => 'active',
];
$whereConditions = [
'id' => 2,
];
if ($db->update('users', $updateData, $whereConditions)) {
echo 'User updated successfully.';
} else {
echo 'Error updating user.';
}
// Simple Example
$deleteConditions = [
'id' => 3,
];
$db->delete('users', $deleteConditions);
// Advanced Example with Error Handling
$deleteConditions = [
'id' => 4,
];
if ($db->delete('users', $deleteConditions)) {
echo 'User deleted successfully.';
} else {
echo 'Error deleting user.';
}
// Simple Example
$conditions = [
'id' => 5,
];
$user = $db->find('users', $conditions);
// Advanced Example with Handling Not Found
$conditions = [
'id' => 6,
];
$user = $db->find('users', $conditions);
if ($user) {
echo 'User found: ' . $user['name'];
} else {
echo 'User not found.';
}
// Simple Example
$conditions = [
'status' => 'active',
];
$activeUsers = $db->findAll('users', $conditions);
// Advanced Example with Looping Through Results
$conditions = [
'status' => 'inactive',
];
$inactiveUsers = $db->findAll('users', $conditions);
foreach ($inactiveUsers as $user) {
echo 'Inactive user: ' . $user['name'];
}
// Simple Example
$conditions = [
'status' => 'active',
];
$activeUserCount = $db->count('users', $conditions);
// Advanced Example with Displaying Count
$conditions = [
'status' => 'inactive',
];
$inactiveUserCount = $db->count('users', $conditions);
echo 'Total inactive users: ' . $inactiveUserCount;
// Simple Example
$newUserId = $db->lastInsertId();
// Advanced Example with Handling
$newUserId = $db->lastInsertId();
if ($newUserId) {
echo 'Last inserted user ID: ' . $newUserId;
} else {
echo 'No records have been inserted.';
}
// Simple Example
if ($db->columnExists('users', 'email')) {
echo 'The "email" column exists in the "users" table.';
} else {
echo 'The "email" column does not exist in the "users" table.';
}
// Advanced Example
$columnName = 'phone';
$tableName = 'contacts';
if ($db->columnExists($tableName, $columnName)) {
echo "The \"$columnName\" column exists in the \"$tableName\" table.";
} else {
echo "The \"$columnName\" column does not exist in the \"$tableName\" table.";
}
// Simple Example
if ($db->tableExists('products')) {
echo 'The "products" table exists in the database.';
} else {
echo 'The "products" table does not exist in the database.';
}
// Advanced Example
$tableName = 'orders';
if ($db->tableExists($tableName)) {
echo "The \"$tableName\" table exists in the database.";
} else {
echo "The \"$tableName\" table does not exist in the database.";
}
// Simple Example
$multipleData = [
[
'name' => 'Alice',
'age' => 30,
],
[
'name' => 'Bob',
'age' => 25,
],
];
$db->insertMultiple('people', $multipleData);
// Advanced Example with Error Handling
$multipleData = [
[
'name' => 'Carol',
'age' => 35,
],
[
'name' => 'Dave',
'age' => 28,
],
];
if ($db->insertMultiple('people', $multipleData)) {
echo 'Multiple rows inserted successfully.';
} else {
echo 'Error inserting multiple rows.';
}
// Simple Example
$sql = "SELECT * FROM products WHERE price > 50";
$results = $db->raw($sql);
// Advanced Example with Parameter Binding
$minPrice = 50;
$sql = "SELECT * FROM products WHERE price > ?";
$results = $db->raw($sql, [$minPrice]);
foreach ($results as $product) {
echo 'Product: ' . $product['name'];
}