定制 mattbit/mysql-compat 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

mattbit/mysql-compat

Composer 安装命令:

composer require mattbit/mysql-compat

包简介

Backward compatibility for old mysql_* functions with PDO

README 文档

README

Build Status Scrutinizer Code Quality SensioLabsInsight

This library tries to provide backward compatibility with the deprecated mysql_* functions.

Caveat

You really should not use this unless strictly needed: it's much better to refactor the existing code to use PDO and prepared statements directly or an ORM like Eloquent.

Although library provides an hackish replacement for mysql_real_escape_string, you ought to refactor your code to use prepared statements.

Requirements

PHP >= 5.6 with the PDO driver is required (PHP 7 is supported).

Installation

You can install mysql-compat via composer:

composer require mattbit/mysql-compat

Usage

The mysql_-equivalent functions are available through the facade class Mattbit\MysqlCompat\Mysql.

require __DIR__ . '/vendor/autoload.php';

use Mattbit\MysqlCompat\Mysql;

Mysql::connect('host', 'user', 'password');
Mysql::selectDb('my_db');

$result = Mysql::query('SELECT * FROM my_table');

$row = Mysql::fetchArray($result);

Note that the static methods are named in a camel-case like version of the original functions, e.g. mysql_fetch_array becomes Mysql::fetchArray.

If you are using PHP7 and want to re-define the old global functions and constants without touching existing code, you can use the Mysql::defineGlobals method:

require __DIR__ . '/vendor/autoload.php';

Mattbit\MysqlCompat\Mysql::defineGlobals();

mysql_connect('host', 'user', 'password');
mysql_select_db('my_db');

$result = mysql_query('SELECT * FROM my_table');

$row = mysql_fetch_array($result, MYSQL_BOTH);

If you need more control over the connections, the database manager allows you to access the underlying objects.

require __DIR__ . '/vendor/autoload.php';

use Mattbit\MysqlCompat\Mysql;

$manager = Mysql::getManager();

// Create a connection by specifying a custom DSN.
$connection = $manager->connect('mysql:dbname=mydatabase;host=myhost', 'user', 'pass');

// You can access the underlying PDO object
$pdo = $connection->getPdo();

// The rest of the code will use the last connection registered in the manager
$res = Mysql::query('SELECT * FROM my_table');

// But you can specify explicitly a connection as well
$res = Mysql::query('SELECT * FROM my_table', $connection);

This is particularly useful if you need to customize connection's DSN (e.g. to specify the charset):

$manager = Mysql::getManager();
$manager->connect('mysql:dbname=database;host=hostname;charset=customCharset', 'user', 'password');

// This will automatically use the connection above, with the right charset.
$res = Mysql::query('SELECT * FROM my_table');

To do

  • mysql_​affected_​rows
  • mysql_​client_​encoding
  • mysql_​close
  • mysql_​connect
  • mysql_​create_​db
  • mysql_​data_​seek (not supported)
  • mysql_​db_​name
  • mysql_​db_​query
  • mysql_​drop_​db
  • mysql_​errno
  • mysql_​error
  • mysql_​escape_​string
  • mysql_​fetch_​array
  • mysql_​fetch_​assoc
  • mysql_​fetch_​field
  • mysql_​fetch_​lengths
  • mysql_​fetch_​object
  • mysql_​fetch_​row
  • mysql_​field_​flags
  • mysql_​field_​len
  • mysql_​field_​name
  • mysql_​field_​seek
  • mysql_​field_​table
  • mysql_​field_​type
  • mysql_​free_​result
  • mysql_​get_​client_​info
  • mysql_​get_​host_​info
  • mysql_​get_​proto_​info
  • mysql_​get_​server_​info
  • mysql_​info
  • mysql_​insert_​id
  • mysql_​list_​dbs
  • mysql_​list_​fields
  • mysql_​list_​processes
  • mysql_​list_​tables
  • mysql_​num_​fields
  • mysql_​num_​rows
  • mysql_​pconnect
  • mysql_​ping
  • mysql_​query
  • mysql_​real_​escape_​string
  • mysql_​result
  • mysql_​select_​db
  • mysql_​set_​charset (see issue #7 for information)
  • mysql_​stat
  • mysql_​tablename
  • mysql_​thread_​id
  • mysql_​unbuffered_​query

mattbit/mysql-compat 适用场景与选型建议

mattbit/mysql-compat 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 56.3k 次下载、GitHub Stars 达 26, 最近一次更新时间为 2015 年 09 月 12 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「mysql」 「PHP7」 「mysql functions」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 mattbit/mysql-compat 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 mattbit/mysql-compat 我们能提供哪些服务?
定制开发 / 二次开发

基于 mattbit/mysql-compat 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 56.3k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 27
  • 点击次数: 25
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 26
  • Watchers: 4
  • Forks: 6
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2015-09-12