raveren/mysql 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

raveren/mysql

Composer 安装命令:

composer require raveren/mysql

包简介

Minimal and useful MYSQL/MariaDb class used for more than a decade!

README 文档

README

Minimal and useful MYSQL/MariaDb class used in production for more than a decade!

composer require raveren/mysql

Usage:

use raveren\mysql;

mysql::config(
    [
        'host'     => '...',
        'database' => '...',
        'username' => '...',
        'password' => '...',
        'init_command' => 'SET NAMES utf8mb4',
    ]
);


// and just use it anywhere now, it is all very self-explanatory:
$my_pies = mysql::getAll("SELECT * FROM user_pies WHERE users_id = 42");
// result format is: 
[
    [ 'id' => 1, 'users_id' => 42, 'pie_name' => 'apple pie' ]
    [ 'id' => 2, 'users_id' => 42, 'pie_name' => 'cherry pie' ]
];

Set your own custom error handler!

mysql::setErrorHandler(
    function (PDOException $e, $query, $boundValues, $boundQuery) {
        !Kint::dump(
            'You just got a mysql error, son',
            $e->getMessage(),
            $query,
            $boundValues,
            $boundQuery
        );
    }
);

Effortless parameter binding! All of these work as you'd expect:

$my_pies = mysql::getAll("SELECT * FROM user_pies WHERE users_id = ?", 42 );
$my_pies = mysql::getAll("SELECT * FROM user_pies WHERE users_id = ?", [42] );
$my_pies = mysql::getAll( 
    "SELECT * FROM user_pies WHERE users_id = ? AND pie_name = ?", 
    [42, 'cherry pie']
);
$my_pies = mysql::getAll( // named parameters supported of course!
    "SELECT * FROM user_pies WHERE users_id = :user_id AND pie_name = :pie", 
    ['user_id' => 42, 'pie' => 'cherry pie']
);

THE RESULT IS ALWAYS ARRAY IN THE FORMAT YOU EXPECT IT TO BE!

Fetch data in your usual and more exotic formats

mysql::getOne("SELECT pie_name FROM user_pies WHERE user_id = ?", 1);
mysql::getRow("SELECT * FROM user_pies WHERE user_id = ?", 1);
mysql::getSingleColumn("SELECT DISTINCT pie_name FROM user_pies WHERE user_id = ?", 1);

// advanced, but you'll wonder how can anyone live without it: 
mysql::getAsAssoc("SELECT * FROM user_pies", 'id=>*'); // second parameter is format of result
$result = [
    [ 1 => ['users_id' => 42, 'pie_name' => 'apple pie']]
    [ 2 => ['users_id' => 42, 'pie_name' => 'cherry pie']]
];

mysql::getAsAssoc("SELECT * FROM user_pies", 'pie_name[]=>*');
$result = [
        'cherry pie' => [
            ['id' => 1, 'users_id' => 42],
        ],
        'apple pie' => [
            ['id' => 2, 'users_id' => 42],
        ],
];

mysql::getAsAssoc("SELECT * FROM user_pies", 'users_id[pie_name]=>pie_name;id'); 
// you should be able deduct the idea by now
$result = [
    [ 42 => [
            'cherry pie' => ['cherry pie', 1]
            'apple pie' => ['apple pie', 2]
        ]
    ]
];

You've got your data manipulation functions, some examples follow

mysql::updateTable(
    'user_has_pies',
    ['has_pie' => mysql::literal('NOT(has_pie)')],
    'id=2'
);
mysql::deleteRows('pies', ['id' => 23]);
mysql::addRow('bakers', [ 'baker_name' => 'Chad', 'pie_level' => 100 ]); 

// third parameter for mysql::addRow can INSERT IGNORE, REPLACE or ON DUPLICATE KEY UPDATE
// this class's got it all baby. Want to optimize chunk inserts for huge amounts of rows? Use

foreach ($data as $row) {
    mysql::addRows('table_name', array('id'=>$row['id']));
}
mysql::addRows('table_name');

Want to just execute a statement?

mysql::exec('SET NAMES utf8mb4');

License

MIT or whatever, just use it in any way you please

统计信息

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

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

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

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固