alexsasharegan/database 问题修复 & 功能扩展

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

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

alexsasharegan/database

Composer 安装命令:

composer require alexsasharegan/database

包简介

PHP mysqli utilities to properly handle errors, connections, and make getting data simpler and more DRY.

README 文档

README

Latest Stable Version Total Downloads Latest Unstable Version License

PHP MySQL utilities to properly handle errors, connections, and make getting data simple and DRY.

Installation

With Composer:

composer require alexsasharegan/database

Then require in the vendor autoloader:

<?php

require_once 'path/to/vendor/autoload.php';

Usage

Static Methods

Calling Database::connect( [ string $configFile = './database.json', array $options = [] ] ) without any arguments will look for a configuration file called database.json file in the calling file's directory. To make use of this default behavior, place your database.json next to your php file. If your config file exists elsewhere, pass in the path as the first argument.

An example config file is included in the project. Just change the name from database.example.json to database.json and move it next to your calling php file. Here is the example config:

<?php

// library defaults
$connectionOptions = [
    'DB_HOST'     => '127.0.0.1',
    'DB_NAME'     => 'test',
    'DB_PORT'     => '3306',
    'DB_CHARSET'  => 'utf8',
    'DB_USERNAME' => 'admin',
    'DB_PASSWORD' => 'admin',
];

// library defaults
$pdoOptions = [
	PDO::ATTR_ERRMODE            => PDO::ERRMODE_EXCEPTION,
	PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
	PDO::ATTR_EMULATE_PREPARES   => FALSE,
	PDO::ATTR_STRINGIFY_FETCHES  => FALSE,
];

$mySQL = new \Database\MySQL($connectionOptions, $pdoOptions);

Some other static methods:

<?php

use Database\MySQL;

# Takes a MySQL-formatted date string and returns a string file path
MySQL::SQLDateToPath( string $SQLDate );
# example
echo MySQL::SQLDateToPath( '2016-09-06 14:02:26' );
# Outputs: '2016/09/06'

# Returns a MySQL-formatted timestamp
echo MySQL::now();
# Outputs: '2016-09-06 14:04:15';

Instance Methods

QUERY

<?php
$mySQL = new \Database\MySQL($connectionOptions, $pdoOptions);

# use a try/catch block to handle a bad query
try {
  $mySQL->select(['firstName', 'lastName'])
        ->from('users')
        ->where('id', 'in', [1,2,3])
        # we can chain methods together here
        ->map(
        # this can be any callable type ( will be called with each row )
        # closures let us 'use' vars from parent scope
        # be wary of when you need to pass by reference using &
        function ( array $resultRow ) 
        {
            $users[] = new User($resultRow); # the $resultRow is an associative array
        }
    );
} catch ( Exception $e ) {
  # insert some custom error handling here
  exit( $e );
}

统计信息

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

GitHub 信息

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

其他信息

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

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固