pjanisio / simpledb
简单的数据库抽象层
2.4.36
2024-06-07 14:34 UTC
Requires
- php: >=7.4
Requires (Dev)
- phpunit/php-code-coverage: ^9.2
- phpunit/phpunit: ^9.5
- sebastian/global-state: ^5.0
Suggests
- ext-pcov: PHP extension that provides line coverage
- ext-uopz: PHP extension
- ext-xdebug: PHP extension that provides line coverage as well as branch and path coverage
This package is auto-updated.
Last update: 2024-09-04 13:14:00 UTC
README
主要目标
- 保留1.X.X版本中的简单用法
- 使用PDO
- 更少的方法,更好的维护性
- 兼容PHP 7.4 - 8.3
使用composer安装
使用composer
将包添加到您的composer.json文件中
"require":
{
"pjanisio/simpledb": "^2.4"
}
原始SQL语句执行示例
// Example: Using the execute method to run a raw SQL statement $sql = "UPDATE `users` SET `email` = 'john.doe@example.com' WHERE `username` = 'john_doe'"; $stmt = $db->execute($sql); echo "Number of affected rows: " . $db->rowCount($stmt) . "\n";
示例用法(所有基本方法)
<?php require 'path/to/SimpleDB.php'; // Adjust the path to where your SimpleDB.php is located use SimpleDB\SimpleDB; try { // Create a new instance of the SimpleDB class $db = new SimpleDB(dbName: 'my_database', username: 'username', password: 'password'); // Check if the connection is successful if ($db->isConnected()) { echo "Connected to the database successfully.\n"; } else { echo "Failed to connect to the database.\n"; } // Example: Executing a query to fetch all rows from a table $sql = 'SELECT * FROM `users`'; $result = $db->fetchAll($sql); echo "Data from users table:\n"; foreach ($result as $row) { print_r($row); } // Example: Inserting a new record into the table $insertData = [ 'username' => 'john_doe', 'email' => 'john.doe@example.com', ]; if ($db->insert('users', $insertData)) { echo "Record inserted successfully.\n"; } else { echo "Failed to insert record.\n"; } // Example: Updating a record in the table $updateData = [ 'email' => 'john.new@example.com', ]; $where = "username = 'john_doe'"; if ($db->update('users', $updateData, $where)) { echo "Record updated successfully.\n"; } else { echo "Failed to update record.\n"; } // Example: Deleting a record from the table $where = "username = 'john_doe'"; if ($db->delete('users', $where)) { echo "Record deleted successfully.\n"; } else { echo "Failed to delete record.\n"; } // Example: Truncating the users table if ($db->truncate('users')) { echo "Table truncated successfully.\n"; } else { echo "Failed to truncate table.\n"; } } catch (Exception $e) { echo 'Error: ' . $e->getMessage() . "\n"; }
===变更日志===
- 新版本 2.4.36 可用(2024年6月7日)
- 新版本 1.2.11 可用(2024年5月19日)
- 新版本 0.2.6 可用(2024年5月15日)
- 新版本 0.2.5 可用(2018年3月17日)
- 新版本 0.2.4 可用(2015年5月30日)
- 新版本 0.2.3 可用(2012年3月27日)
- 新版本 0.2.2a 可用(2011年11月2日)
- 新版本 0.2.2 可用(2010年11月12日)
- 新版本 0.2.1 可用(2010年2月19日)
- 新版本 0.2.0 可用(2010年1月9日)
- 新版本 0.1.9 可用(2009年11月23日)
- 新版本 0.1.8 可用(2009年11月12日)
- 仓库清理和重置(2009年11月10日)
- 新版本 0.1.7 可用(2009年10月10日)