定制 gabriel-binotti/connection-pdo 二次开发

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

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

gabriel-binotti/connection-pdo

最新稳定版本:1.0.1

Composer 安装命令:

composer require gabriel-binotti/connection-pdo

包简介

README 文档

README

This library is for database connection in PHP using PDO.

Require

  • PHP >= 7.0
  • Composer installed

Start

The first you need install the library:

// Version 1.0.0
composer require gabriel-binotti/connection-pdo

Configuration .ini

After downloading, you need set the parameters in the database.ini file with your database connection data. If you prefer, you can create a new .ini file, the file is located in vendor/gabriel-binotti/database/databaseApplication/config/database.ini

DB_HOST    = host
DB_NAME    = name
DB_PORT    = port
DB_USER    = username
DB_PASS    = password
DB_TYPE    = type_connection 
  • DB_HOST => hostname or IP
  • DB_NAME => name of database
  • DB_PORT => number porto to connection
  • DB_USER => username
  • DB_PASS => password
  • DB_TYPE => type to batabase coneccetion tha value cad do(mysql, pgsql. srqsrv)

Import

Include the autoload.php file in the header file

use GabrielBinottiDatabase\Connection;
require "vendor/autoload.php";

Simple Struct

try{
    Connection::init('database');
        $pdo = Connection::get();    
        // Command SQL
    Connection::close();
    
}catch(Exception $e){
    echo $e->getMessage();
}
  • Connection::init('database'): This method init() is called to initiate the database connection and your parameter is the file name configured before .ini.

  • Connection::close(): This method should be called every time a database connection is opened.

  • $pdo = Connection::get(): The method get() return the same instance of the class every time

All code should be between try and catch

Transactions

If you need to ensure that the transaction is successfully executed, you can initiate a transaction with the command Connection::transaction() and after execute SQL command. To finish using the command Connection::commit() when the transaction is successfully or Connection::rollback() to return to the initial state.

try{
    Connection::init('database');
    $pdo = Connection::get(); 
    Connection::transaction();
    // Command SQL

    Connection::commit();
    Connection::close();
    
}catch(Exception $e){
    Connection::rollback();
    echo $e->getMessage();
}

What use PDO?

Above I show like to use PDO in PHP with simple examples:

Select All

$stmt = $pdo->prepare("SELECT * FROM TABELA");
$stmt->execute();
$return = $stmt->fetchAll();

Select with where

$stmt = $pdo->prepare("SELECT * FROM TABELA WHERE id=:id");
$stmt->bindValue(':id',1);
$stmt->execute();
$return = $stmt->fetch();

By default is returned a object, if you need an array you can set parameter \PDO::FETCH_ASSOC in method fetchAll() or in method fetch().

Insert

$stmt = $pdo->prepare("INSERT INTO TABELA(nome, telefone) VALUES(:nome, :telefone) ");
$stmt->bindValue(":nome", "Valor");
$stmt->bindValue(":telefone", "Valor");
$return = $stmt->execute();

Update

$stmt = $pdo->prepare("UPDATE TABELA SET nome=:nome WHERE id=:id");
$stmt->bindValue(":id", "Valor");
$stmt->bindValue(":nome", "Valor");
$return = $stmt->execute();

Delete

$stmt = $pdo->prepare("DELETE FROM TABELA WHERE id=:id");
$stmt->bindValue(":id", "Valor");
$return = $stmt->execute();

bindValue VS bindParam

The simple form bindParam receives a variable like argument, not accepting direct values or functions return.

$var = 1;
$stmt->bindParam(':campo', $var);

The bindValue() accept direct values, variables and functions return.

$stmt->bindValue(':campo', 1);
$stmt->bindValue(':campo', $var);
$stmt->bindValue(':campo', $obj->getValor());

Third parameter bindValue or bindParam

The third parameter is the data type, by default is string, but you can change that accord with value type.

$stmt->bindValue(':campo', getValor(), PDO::PARAM_BOOL);               // reference bool value
$stmt->bindValue(':campo', getValor(), PDO::PARAM_NULL);               // reference null value
$stmt->bindValue(':campo', getValor(), PDO::PARAM_INT);                // reference int value 
$stmt->bindValue(':campo', getValor(), PDO::PARAM_STR);                // reference string value
$stmt->bindValue(':campo', getValor(), PDO::PARAM_LOB);                // reference object value to store large binary data.

SQL Server

To use SQL Server you need to install the driver and configure some lines in php.ini, as shown in the example above:

copiando

  • To finish edit the file php.ini
// Version X86
extension=php_pdo_sqlsrv_73_ts_x86.dll
extension=php_sqlsrv_73_ts_x86.dll

// Version x64
extension=php_pdo_sqlsrv_73_ts_x64.dll
extension=php_sqlsrv_73_ts_x64.dll

gabriel-binotti/connection-pdo 适用场景与选型建议

gabriel-binotti/connection-pdo 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 11 次下载、GitHub Stars 达 0, 最近一次更新时间为 2024 年 03 月 21 日, 在 PHP 生态内属于活跃度较高的组件。

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

围绕 gabriel-binotti/connection-pdo 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: GPL-3.0-or-later
  • 更新时间: 2024-03-21