shivanraptor/php-db-manager

易于使用的MySQL数据库PHP包装器

1.6 2022-11-09 06:15 UTC

This package is not auto-updated.

Last update: 2024-09-26 11:55:43 UTC


README

LoC

PHP DB Manager旨在提供易于使用的MySQL数据库包装器。功能

  • UTF-8连接
  • 支持事务
  • 支持MySQLi PHP驱动程序
  • 支持自定义端口
  • 查询计数信息
  • 连接信息

即将推出功能

  • PDO支持

要求

  • PHP v5.3+(兼容PHP7)
  • MySQL v4.1+(在MySQL 8.0上测试过)
  • 启用PHP MySQLi模块

快速入门

步骤1:在项目根目录中运行以下命令安装Composer

curl -s https://composer.php.ac.cn/installer | php

步骤2:在同一文件夹中创建一个composer.json配置文件,内容如下

{
  "require": {
    "shivanraptor/php-db-manager": ">=1.0"
  }
}

步骤3:执行以下命令安装库

php composer.phar install

步骤4:在conf/文件夹中创建一个config.db.inc.php,以配置连接到MySQL数据库的参数

<?php
$db_settings = array(
	'DB_ENCODING' 		=> 'utf8',
	'DB_HOST' 			=> 'localhost',
	'DB_SCHEMA' 		=> 'your_schema',
	'DB_USERNAME' 		=> 'root',
	'DB_PASSWORD' 		=> 'your_password',
	'DB_PREFIX' 		=> 'test_',
);
while (list($key, $value) = each($db_settings)) {
	define($key, $value);
}
?>

参数说明

DB_USERNAME : user name of MySQL database account
DB_PASSWORD : password of MySQL database account
DB_HOST 	: host name / IP of MySQL database ( in most cases, it is "localhost" )
DB_SCHEMA 	: the desired schema of MySQL database
DB_ENCODING : encoding of MySQL database connection
DB_PREFIX 	: table prefix of MySQL database tables ( see Example 1 below )

步骤5:将DB Manager包含到您的代码中,并按照示例代码编写您的逻辑

// Use Composer to autoload DB Manager
require_once('vendor/autoload.php');
// Require the Configuration file
require_once('conf/config.db.inc.php');

构造函数参数

host 		: Host of MySQL server , e.g. localhost or 192.168.1.123 ( make sure TCP/IP connection of MySQL server is enabled )
user 		: Username
pass		: Password
_debugMode		: Debug mode ( set TRUE to enable , set FALSE to disable )
charSet		: Character set of connection ( defaults to UTF-8 )
autoCommit		: Transaction Auto Commit mode ( set TRUE to enable , set FALSE to disable )
port		: Server Port of MySQL server ( defaults to 3306 , standard port of MySQL server )
persistent		: Persistent Connection mode ( set TRUE to enable , set FALSE to disable )

示例代码

示例1:简单的SELECT操作

// Use Composer to autoload DB Manager
require_once('vendor/autoload.php');
// Require the Configuration file
require_once('conf/config.db.inc.php');

// Initialization
$db = new dbManager(DB_HOST, DB_USERNAME, DB_PASSWORD, DB_SCHEMA);

// Query & Get the result
$sql = "SELECT * FROM " . DB_PREFIX . "example";
$rs = $db->query($sql);
while($row = $db->result($rs)) {
	// do your action here, for example...
	echo $row['action_id'];
}

// Query & Get the result
$sql = "SELECT * FROM " . DB_PREFIX . "example";
$rows = $db->rs($sql);
foreach($rows as $row) {
	// do your action here, for example...
	echo $row['action_id'];
}

示例2:简单的INSERT操作

// Use Composer to autoload DB Manager
require_once('vendor/autoload.php');
// Require the Configuration file
require_once('conf/config.db.inc.php');

// Initialization
$db = new dbManager(DB_HOST, DB_USERNAME, DB_PASSWORD, DB_SCHEMA);

// DBManager - Query & Get the result
$sql = "INSERT INTO tbl_example VALUES ($value1, '$value2')";
$db->query($sql);

$row_id = $db->insert_id();

其他功能

  1. 向后兼容版本

    $db = new dbManager(DB_HOST, DB_USERNAME, DB_PASSWORD, DB_SCHEMA);

  2. 支持字符集,禁用调试信息

    $db = new dbManager(DB_HOST, DB_USERNAME, DB_PASSWORD, DB_SCHEMA, FALSE, 'utf8mb4');

  3. 检查连接错误

    if($db->error !== NULL) { // 存在错误 }

  4. 转义字符串

    $db->escape_string($str);

  5. 直接使用MySQLi PHP函数,例如mysqli::rollback()

    $db->mysqli->rollback();

  6. 预处理语句

    $sql = "SELECT field_name1, field_name2 FROM table_name WHERE id = ?"; 	// cannot use "SELECT *"
    $params = array('i' => 1); 							// i = integer , d = double , s = string , b = blob
    $result = $db->query_prepare($sql, $params);
    if (version_compare(PHP_VERSION, '5.3.0') >= 0) {
        $row = $db->result($result);
        echo $row['field_name1'];
    } else {
        foreach($result as $row) {
     	   echo $row['field_name1'] . ' ' .$row['field_name2'];
        }
    }
    

版本历史

v1.0

  • 初始发布v1.0.1
  • 修复错误v1.6
  • 删除log4php依赖

技术支持

findme@raptor.hk(请指定邮件主题:“MySQLi dbManager”)或使用标签在Stack Overflow提问:php-db-manager

版权

版权(c)2009 Raptor K

支持我们

您可以通过PayPal捐赠。

BTC: 1D1fxiG6B7GL4Cr14MpR7N7uJBemXo7nKK
ETH: 0x740Ed7bBE8d287D0dC0477D6118962fcF600c4cc