matthewbaggett/mysql2pdo

MySQL2PDO适配器

dev-master 2015-07-27 11:48 UTC

This package is not auto-updated.

Last update: 2024-09-14 18:04:11 UTC


README

此包可用于通过PDO包装函数访问MySQL数据库。它提供了一个类,其中的函数可以以与原始MySQL扩展兼容的方式访问MySQL数据库。当原始MySQL扩展在PDO作为后端不可用时,该包提供全局mysql_*函数。

该对象复制了mysql_*函数的所有功能,除了mysql_info方法,我尽可能复制,但由于PDO的限制/我的知识,无法做到完全一致。

实现

在您的引导中包含

// Include the definitions
require_once(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'MySQL_Definitions.php');
 
// Include the object
require_once(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'MySQL.php');
 
// Include the mysql_* functions
require_once(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'MySQL_Functions.php');
 
// Now all of the mysql_* methods will work on a PHP version that has them removed.

(可选:见缺点) 更新您的错误报告为(根据环境)

error_reporting(E_ALL & ~E_NOTICE & ~E_STRICT); // Production
error_reporting(E_ALL & ~E_STRICT); // Development

(可选:见缺点) 如果您正在使用is_resource / get_resource_type为mysql_方法,则需要将它们替换为以下函数:is_resource_custom / get_resource_type_custom。您可以使用您的IDE进行搜索替换或在项目基础上使用以下两行

find ./ -type f | xargs sed -i 's/is_resource( /is_resource_custom(/'
find ./ -type f | xargs sed -i 's/get_resource_type( /get_resource_type_custom(/'

这是给谁的?

此包是为希望将PHP版本升级到移除mysql_connect/mysql_*函数的版本的网站所有者/开发者而设计的,无需重写整个代码库来替换这些函数为PDO或MySQLI。

为什么移除了mysql扩展?

缺乏

  • 存储过程(无法处理多个结果集)
  • 预处理语句
  • 加密(SSL)
  • 压缩
  • 完整的字符集支持
  • 安全性

mysql_*方法易于理解,但难以安全。由于它不提供预处理语句,更多开发者(特别是初学者)容易受到安全风险的影响。并不是说mysql_*方法不安全,但它们使初学者更容易编写不安全的查询。

替代主机

您还可以在此项目中找到该项目的phpclasses.org:http://www.phpclasses.org/package/8221-PHP-Access-MySQL-databases-using-PDO-wrapper-functions.html

以及GitHub:https://github.com/AzizSaleh/mysql

我会尽力保持它们的更新。

此库的缺点

遗憾的是,由于限制,在实现此库之前,有一些事情您应该知道。

资源

由于在PHP中无法动态创建资源,以下方法将无法按预期工作

is_resource get_resource_type

以下mysql函数

Function Name (resource type) (our library type)
mysql_connect (mysql link) (int)
mysql_pconnect (mysql link persistent) (int)
mysql_db_query (mysql result) (PDO Statement)
mysql_list_dbs (mysql result) (PDO Statement)
mysql_list_fields (mysql result) (PDO Statement)
mysql_list_processors (mysql result) (PDO Statement)
mysql_list_dbs (mysql result) (PDO Statement)
mysql_query (mysql result) (PDO Statement)
mysql_unbuffered_query (mysql result) (PDO Statement)

要修复,您需要将它们替换为以下函数:is_resource_custom / get_resource_type_custom。您可以使用您的IDE进行搜索替换或在项目基础上使用以下两行

find ./ -type f | xargs sed -i 's/is_resource( /is_resource_custom(/'
find ./ -type f | xargs sed -i 's/get_resource_type( /get_resource_type_custom(/'

错误报告

如果您向以下方法传递一个常量(例如字符串),在PHP严格错误模式下(errro_reporting(>= 30720))将触发以下错误:“PHP严格标准:只能通过引用传递变量”

mysql_fetch_array
mysql_fetch_assoc
mysql_fetch_row
mysql_fetch_object
mysql_db_name
mysql_dbname
mysql_tablename
mysql_result
mysql_free_result
mysql_freeresult
mysql_field_len
mysql_fieldlen
mysql_field_flags
mysql_fieldflags
mysql_field_name
mysql_fieldname
mysql_field_type
mysql_fieldtype
mysql_field_table
mysql_fieldtable
mysql_field_seek
mysql_fetch_field

要修复,您需要更改错误报告以隐藏严格标准(任何<= 30719)。示例

error_reporting(30719); // <= 30719
error_reporting(E_ALL & ~E_NOTICE & ~E_STRICT); // Production
error_reporting(E_ALL & ~E_STRICT); // Development

单元测试

此项目中提供了2个单元测试

  • MySQL_Test.php - 如果您使用带有mysql_connect()的PHP运行此测试。
  • MySQL_Test_After.php - 如果您拥有的PHP版本没有启用mysql_connect(),则运行此测试。

关于

如果在运行过程中遇到任何问题、错误、功能或使事物变得更好,请发送给我,我会尽快处理。

@author    Aziz S. Hussain <azizsaleh@gmail.com>
@copyright GPL license 
@license   https://gnu.ac.cn/copyleft/gpl.html 
@link      http://www.AzizSaleh.com

版本

当前版本

版本 1.1

版本历史

1.0 - 2013年9月

  • 初始发布。
  • 在 PHP 5.0 上通过单元测试进行测试

1.1 - 2014年2月

  • 初始 GitHub 发布。
  • 由 Domenic LaRosa 报告的严重错误已修复。
  • 在 PHP 5.5.59 上进行测试
  • 添加了 is_resource_custom/get_resource_type_custom 函数。
  • 添加了针对 PHP >= 5.5.5 的单元测试。
  • 修复了在 PHP >= 5.5.5 上未测试的一些逻辑问题。

1.2 - 2014年9月

  • 修复了一个问题(感谢 Martijn Spruit),在该问题中,脚本在 PHP V <= 5.3.8 上会中断。
  • 更新了 MySQL_Stat_Test 的单元测试,允许数字之间有 <= 10 的差异。