aleksey.nemiro / nemiro.data.php
用于操作数据库 MySql 和 PostgreSQL 的小型辅助类。
Requires
- php: >=5.3.0
This package is not auto-updated.
Last update: 2020-08-24 16:41:13 UTC
README
Nemiro.Data.PHP

Nemiro.Data.PHP 是一套用于操作数据库 MySql 和 PostgreSQL 的实用类。
使用五个简单的方法与数据库交互: ExecuteNonQuery、ExecuteScalar、GetData、GetTable 和 GetRow。
这些类允许您使用参数化查询,这使得与数据库交互更加安全。
Nemiro.Data.PHP 在 Apache License Version 2.0 许可下发布。
功能
- MySql 客户端;
- PostgreSQL 客户端;
- 与不同数据提供者工作的单个接口;
- 自动控制数据库连接;
- 参数化查询。
系统要求
- PHP 5 >= 5.3;
- MySQL >= 5.6;
- PostgreSQL >= 7.4。
注意:尚未测试与早期版本的兼容性。
支持
不计划进一步支持或开发此项目。欢迎使用 .NET ;-)
如何使用此项目?
项目文件是用 Visual Studio 2013 并使用扩展 PHP Tools for Visual Studio 创建的。
要将在您自己的项目中使用这些类,建议将所有解决方案文件放入文件夹 \Nemiro\Data 中(对应于命名空间)。
如何使用这些类?
配置
默认情况下,类使用以下常量的数据库连接设置
// MySql define('MYSQL_DB_NAME', '%your database name here%'); define('MYSQL_DB_USER', '%your database username here%'); define('MYSQL_DB_PASSWORD', '%your database password here%'); define('MYSQL_DB_HOST', 'localhost'); define('MYSQL_DB_PORT', 3306); define('MYSQL_DB_MODE', 2); // PostgreSQL define('PGSQL_DB_NAME', '%your database name here%'); define('PGSQL_DB_USER', '%your database username here%'); define('PGSQL_DB_PASSWORD', '%your database password here%'); define('PGSQL_DB_HOST', 'localhost'); define('PGSQL_DB_PORT', 5432); define('PGSQL_DB_MODE', 1);
DB_MODE 可以为以下之一
- 0 - 手动;
- 1 - 自动 - 对每个请求打开和关闭;
- 2 - 智能型(推荐)。
您可以使用单独的连接设置,这些设置必须在创建数据库客户端实例时指定。
包含文件
要使用数据库客户端,您必须包含以下文件
require_once './Nemiro/Data/Import.php';
或者
require_once './Nemiro/Data/MySql.php'; require_once './Nemiro/Data/PgSql.php';
导入命名空间
为了方便,您可以在代码中导入必要的类
// client for MySql use Nemiro\Data\MySql as MySql; // client for PostgreSQL use Nemiro\Data\PgSql as PgSql; // query builder use Nemiro\Data\DBCommand as DBCommand;
使用示例
以下示例创建一个简单的查询,用于从表 [messages]
中选择所有记录。
通过 GetTable 方法获取的记录,该方法返回一个行数组。
// create client instance for MySql $client = new MySql(); // create a new command $client->Command = new DBCommand('SELECT * FROM messages'); // get table $table = $client->GetTable(); // output the table rows echo '<pre>'; foreach($table as $row) { print_r($row); } echo '</pre>';
以下示例创建一个参数化查询,用于向表 [users]
中添加记录。
查询通过 ExecuteScalar 方法执行,该方法返回添加记录的 ID。
// create client instance for MySql $client = new MySql(); // create a new command $client->Command = new DBCommand ( 'INSERT INTO users (username, date_created) '. 'VALUES (@username, @date_created)' ); // @username and @date_created is parameters name, // add a values for this parameters $client->Command->Parameters->Add('@date_created')->SetValue(date('Y-m-d H-i-s')); $client->Command->Parameters->Add('@username')->SetValue('anyname'); // execute the command $newId = $client->ExecuteScalar(); echo 'ID = '.$newId;
以下示例创建多个查询,并通过 GetData 方法执行,该方法返回一个表数组。
// create client instance for MySql $client = new MySql(); // create commands $firtCommand = new DBCommand('SELECT * FROM users WHERE is_blocked = 0'); $secondCommand = new DBCommand ( 'SELECT * FROM messages WHERE id_users IN '. '(SELECT id_users FROM users WHERE is_blocked = 0) AND '. 'subject LIKE @search_subject' ); $secondCommand->Parameters->Add('@search_subject', '%hello%'); $thirdCommand = 'SELECT * FROM files'; // etc... // add commands to client $client->Command = array($firtCommand, $secondCommand, $thirdCommand); // and execute all command $data = $client->GetData(); // output results echo '<pre>'; foreach ($data as $table) { print_r($table); } echo '</pre>';