peterujah/db-controller

PHP PDO 数据库封装器。

1.8 2023-10-22 10:45 UTC

This package is auto-updated.

Last update: 2024-09-22 12:55:29 UTC


README

DBController 是一个 PHP PDO 封装器,它提供了一种方便的方式,通过 PDO 扩展与数据库进行交互。

安装

您可以通过运行以下命令通过 Composer 安装此包:

composer require peterujah/db-controller

USAGES

要使用 DBController,请按照以下简单步骤操作。

  1. 通过传递数组或返回数组的配置文件路径来创建 DBController 类的实例。
use Peterujah\NanoBlock\DBController;
// Pass the configuration as an array
$config = [
    'VERSION' => 'mysql',
    'HOST' => 'localhost',
    'PORT' => 3306,
    'NAME' => 'my_database',
    'USERNAME' => 'root',
    'PASSWORD' => 'password',
];

$handler = new DBController($config);

或者扩展 \Peterujah\NanoBlock\DBController 并设置以下连接详情

class Conn extends \Peterujah\NanoBlock\DBController{ 
	public function __construct(bool $development = false){
 		$config = array(
			"PORT" => 3306,
			"HOST" => "localhost",
			"VERSION" => "mysql",
		);
		if($development){
			$config["USERNAME"] = "root";
			$config["PASSWORD"] = "";
			$config["NAME"] = "dbname";
		}else{
			$config["USERNAME"] = "dbusername";
			$config["PASSWORD"] = "dbpass";
			$config["NAME"] = "dbname";
		} 
		$this->onDebug = $development;
		parent::__construct($config);
	}
}

初始化您的自定义类

$handler = new Conn($_SERVER["HOST_NAME"]=="localhost");

现在可以使用预处理语句执行查询 select、insert、update、delete 等。

$handler->prepare('SELECT * FROM users WHERE username = :username LIMIT 1');
$handler->bind(':username', "Peter");
$handler->execute();
$res = $handler->getOne();
$handler->free();

或者使用查询执行 select、insert、update、delete 等。

$handler->query('SELECT * FROM users');
$res = $handler->getAll();
$handler->free();

定制

根据需要定制配置或启用调试。

// Set a configuration value
$handler->setConfig('VERSION', 'pgsql');

// Enable debugging mode
$handler->setDebug(true);

错误处理

DBController 为数据库操作提供错误处理。您可以使用 error()errorInfo() 方法检索错误信息。

// Get the error information for the last statement execution
$errorInfo = $handler->error();

// Print the error message
if ($errorInfo !== null) {
    echo "Error: " . $errorInfo[2];
}

调试

您可以通过调用 dumpDebug() 方法启用调试模式,以获取有关执行语句的更详细信息。

// Enable debugging mode
$handler->setDebug(true);

// Dump the debug information for the last statement execution
$handler->dumpDebug();

方法

使用 DBController 类提供的各种方法与数据库交互。

// Prepare a statement
$query = 'SELECT * FROM users WHERE id = :id';
$handler->prepare($query);

// Bind values to parameters
$handler->bind(':id', 1);

//Binds a variable to a parameter.
$handler->param(':id', 1, DBController::_INT)

// Execute the statement
$handler->execute();

// Fetch a single row as an object
$user = $handler->getOne();

// Fetch all rows as an array of objects
$users = $handler->getAll();

// Get the number of rows affected by the last statement execution
$rowCount = $handler->rowCount();

// Get the last inserted ID
$lastInsertId = $handler->getLastInsertId();

// Free up the statement cursor
$handler->free();

配置格式

连接配置数组示例

[
     PORT => 3306,
     HOST => "localhost",
     VERSION => "mysql",
     NAME => "dbname",
     USERNAME => "root",
     PASSWORD => ""
]

贡献

欢迎贡献!如果您遇到任何问题或对改进有建议,请打开问题或提交拉取请求。

许可

DBController is open-source software licensed under the MIT license.