giovanniramos / pdo4you
使用PDO扩展访问SQL数据库
Requires
- php: >=5.3.2
Requires (Dev)
- phpunit/phpunit: 3.7.*
This package is not auto-updated.
Last update: 2024-09-23 13:42:04 UTC
README
PDO4You
此类基于PDO,它是一个PHP扩展,允许开发者创建可移植的代码,以满足大多数流行的数据库,包括MySQL、PostgreSQL、SQLite、Oracle、Microsoft SQL Server、Sybase。
另外,自3.0版本起已支持MariaDB数据库。MariaDB被视为未来免费替代MySQL的产品。更多信息请参阅:http://bit.ly/MARIADB
从2.6版本开始也支持CUBRID数据库。这是一个针对Web应用高度优化的管理系统数据库。更多信息请参阅:http://bit.ly/CUBRID
PDO4You提供数据访问的抽象层,无论使用哪种数据库,都可以始终使用相同的方法执行查询和获取数据。
采用单例设计模式以优化连接,确保只有一个连接对象实例。
它们的使用优势是什么?
- 使用JSON注解紧凑的SQL指令
- 连接抽象
- 防止SQL注入
- 多数据库连接
- 预定义CRUD方法/命令
- 使用VCAP_SERVICES连接的选项
- 带有堆栈跟踪的错误处理
入门
引导文件负责加载自动加载器和所有项目依赖项。如果不可用,您将收到一个确认消息,提示使用Composer开始安装。
<?php /** * Loads the autoloader and all project dependencies * The data access have been defined in the 'initial configuration file' */ require __DIR__.'/src/bootstrap.php'; ?>
PDO4You.php
:包含PDO对象连接实现的类。
PDO4You.config.php
:初始配置文件,服务器访问和数据库。
PDO4You.settings.ini
:包含数据库连接每个适配器的设置。
Describe.php
:Describe是一个用于列出表中的所有字段及其数据格式的类。
Pagination.php
:Pagination是一个允许您以分页方式列出记录的类,类似于Google。
建立数据库连接
为了抽象我们的数据访问机制,我们使用DSN(数据源名称=数据源)来存储需要与其它数据源进行通信的信息,例如:技术类型、服务器名称或位置、数据库名称、用户、密码以及其他设置。这允许项目的可移植性,便于在迁移过程中交换数据库访问权限。
<?php // Loading all the necessary files require __DIR__.'/src/bootstrap.php'; // Connection class imported use PDO4You\PDO4You; /** * Main ways to start a connection instance */ // Connection instance started and available # DEFAULT PDO4You::getInstance(); // Connecting to other data sources through a DSN # MySQL / MariaDB PDO4You::getInstance('instance_name', 'mysql:host=localhost;dbname=pdo4you;port=3306', 'user', 'pass'); # PostgreSQL PDO4You::getInstance('instance_name', 'pgsql:host=localhost;dbname=pdo4you;port=5432', 'user', 'pass'); # CUBRID PDO4You::getInstance('instance_name', 'cubrid:host=localhost;dbname=pdo4you;port=33000', 'user', 'pass'); ?>
在您的数据库上执行CRUD操作
CRUD术语指数据库中的4个基本操作,其含义为:创建(INSERT)、检索(SELECT)、更新(UPDATE)和销毁(DELETE)。
查询语句
PDO4You::select()
:返回一个按列名称索引的数组。
PDO4You::selectNum()
:返回一个按列的数字位置索引的数组。
PDO4You::selectObj()
:返回一个具有列名称属性的对象。
PDO4You::selectAll()
:返回一个按名称和列的数字位置索引的数组。
以下是执行这些操作的一些示例。
在数据库中选择记录
<?php // Loading all the necessary files require __DIR__.'/src/bootstrap.php'; // Connection class imported use PDO4You\PDO4You; // Starting a connection instance. The default connection is not persistent PDO4You::getInstance(); // Defining a persistent communication with the database PDO4You::setPersistent(true); // Selecting records in the database PDO4You::select('SELECT * FROM books LIMIT 2'); // Selecting records and setting that connection instance will be used PDO4You::select('SELECT * FROM books LIMIT 2', 'instance_name'); // Query statement $sql = 'SELECT * FROM books LIMIT 2'; // Selecting records with PDO::FETCH_ASSOC $result = PDO4You::select($sql); // Selecting records with PDO::FETCH_NUM $result = PDO4You::selectNum($sql); // Selecting records with PDO::FETCH_OBJ $result = PDO4You::selectObj($sql); // Selecting records with PDO::FETCH_BOTH $result = PDO4You::selectAll($sql); // Selecting all records $result = PDO4You::select('SELECT * FROM books'); // Getting the total number of rows affected by the operation $total = PDO4You::rowCount(); // Displaying the query results echo '<pre><h3>Query Result:</h3> ' , print_r($result, true) , '</pre>'; ?>
PDO4You 类的 insert()、update() 和 delete() 方法被放置在事务之间,即 beginTransaction() 和 commit() 之间。这确保了系统可以在操作不成功时回滚,并撤销自事务开始以来所做的所有更改。
在版本 3.1 中添加了 execute() 方法,作为 (insert、update 和 delete) 方法的替代方案。
执行过程中的严重错误将引发 rollBack() 调用,撤销整个操作。因此,将抛出一个异常,追踪涉及操作的所有类和方法,在“生产”环境中加快调试过程,从而确保数据库不会变得不稳定。
在 MySQL 中,InnoDB 类型表支持事务。
PDO4You 类的 SQL 语句(insert、update 和 delete)现在使用 JSON 表示法,这是一种新的查询格式,其规范与 Python、Ruby、C++、Java、JavaScript 等语言非常相似。该类采用的新语法比使用数组的方法更美观、更简洁。此外,指令可以同时操作同一数据库中的不同表。
以下是一些实践中的示例摘录。
向数据库中插入单行数据
<?php // SQL insert in JSON format $json = ' insert : [ { table: "users" , values: { mail: "pdo4you@gmail.com" } } ] '; // The $result variable stores as return of the method, an array with the number of rows affected by the insert operation $result = PDO4You::execute($json); // Just after insertion, use the method PDO4You::lastId() to get the ID of the last insert operation in the database $lastInsertId = PDO4You::lastId(); // If needed, enter the name of the sequence variable, required in some databases $lastInsertId = PDO4You::lastId('table_id_seq'); ?>
插入多行数据
<?php // SQL insert in JSON format $json = ' insert : [ { table: "users" , values: { mail: "mail_1@domain.com" } },{ table: "users" , values: { mail: "mail_2@domain.com" } },{ table: "books" , values: { title: "title", author: "author" } } ] '; // The $result variable stores an array with the number of rows affected by the insert operation $result = PDO4You::execute($json); ?>
更新多行数据
<?php // SQL update in JSON format $json = ' update : [ { table: "users" , values: { mail: "mail_1@domain.com" } , where: { id: 2 } },{ table: "users" , values: { mail: "mail_2@domain.com" } , where: { id: 3 } },{ table: "books" , values: { title: "new-title", author: "new-author" } , where: { id: 1 } } ] '; // The $result variable stores an array with the number of rows affected by the update operation $result = PDO4You::execute($json); ?>
删除多行数据
<?php // SQL delete in JSON format $json = ' delete : [ { table: "users" , where: { id: 2 } },{ table: "users" , where: { id: 5 } },{ table: "users" , where: { id: 10 } },{ table: "books" , where: { id: 10 } } ] '; // The $result variable stores an array with the number of rows affected by the delete operation $result = PDO4You::execute($json); ?>
服务器支持的驱动程序
执行以下方法以检查服务器是否支持针对您的数据库的特定 PDO 驱动程序。支持的驱动程序将在屏幕上显示。
<?php // The method below shows all the drivers installed and that are supported by the server PDO4You::showAvailableDrivers(); ?>
要启用未安装的任何驱动程序,请找到 php.ini 文件,打开它,查找不带引号的 "extension=",然后根据您的数据库取消注释以下行(删除每行的 "分号"),更改后,重启服务器。
;extension=php_pdo.dll ; This DLL is not required as of PHP 5.3 extension=php_pdo_mysql.dll ; MySQL 3.x/4.x/5.x / MariaDB extension=php_pdo_pgsql.dll ; PostgreSQL ;extension=php_pdo_cubrid.dll ; CUBRID ;extension=php_pdo_oci.dll ; Oracle Call Interface ;extension=php_pdo_sqlsrv.dll ; Microsoft SQL Server / SQL Azure ;extension=php_pdo_dblib.dll ; Microsoft SQL Server / Sybase / FreeTDS ;extension=php_pdo_mssql.dll ; Microsoft SQL Server "Old version" ;extension=php_pdo_sqlite.dll ; SQLite 2/3
Xampp 服务器的 PDO 驱动程序
CUBRID (PHP 5.4): http://bit.ly/PDO_CUBRID-PHP54
CUBRID (PHP 5.3): http://bit.ly/PDO_CUBRID-PHP53
MS SQL Server 3.0 (PHP 5.4): http://bit.ly/PDO_SQLSRV-PHP54
MS SQL Server 2.0 (PHP 5.2/5.3): http://bit.ly/PDO_SQLSRV-PHP53
MS SQL Server (旧版本): http://bit.ly/PDO_MSSQL-PHP53
依赖项
PHP >= 5.3.2
PHPUnit >= 3.7.0(用于运行测试套件)
合作者
Giovanni Ramos - giovannilauro@gmail.com - http://twitter.com/giovanni_ramos
请参阅参与此项目的贡献者列表。
许可协议
版权(c)2010-2013 Giovanni Ramos
PDO4You 是开源软件,许可协议为 MIT 许可协议