gabriel-binotti/connection-pdo

1.0.1 2024-03-23 16:39 UTC

This package is auto-updated.

Last update: 2024-09-23 18:12:51 UTC


README

此库用于PHP中使用PDO进行数据库连接。

要求

  • PHP >= 7.0
  • 已安装Composer

开始

首先需要安装库

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

配置.ini文件

下载后,您需要在database.ini文件中设置数据库连接数据。如果您愿意,可以创建一个新的.ini文件,该文件位于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 => 主机名或IP地址
  • DB_NAME => 数据库名
  • DB_PORT => 连接端口
  • DB_USER => 用户名
  • DB_PASS => 密码
  • DB_TYPE => 数据库连接类型,值可以是mysql、pgsql、srqsrv等

导入

在头部文件中包含autoload.php文件

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

简单结构

try{
    Connection::init('database');
        $pdo = Connection::get();    
        // Command SQL
    Connection::close();
    
}catch(Exception $e){
    echo $e->getMessage();
}
  • Connection::init('database'): 此方法 init() 被调用来初始化数据库连接,您的参数是之前配置的 .ini 文件名。

  • Connection::close(): 此方法应在每次打开数据库连接时调用。

  • $pdo = Connection::get(): get() 方法每次都返回类的相同实例。

所有代码应在 trycatch 之间

事务

如果您需要确保事务成功执行,可以使用命令 Connection::transaction() 初始化事务,然后执行SQL命令。在事务成功执行后,使用命令 Connection::commit() 完成使用,或者使用 Connection::rollback() 返回初始状态。

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

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

为什么使用PDO?

以下是用简单的示例展示如何在PHP中使用PDO。

选择所有

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

选择带有where条件

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

默认返回对象,如果您需要数组,可以在方法fetchAll()或fetch()中设置参数 \PDO::FETCH_ASSOC

插入

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

更新

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

删除

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

bindValue VS bindParam

简单形式的 bindParam 接收变量作为参数,不接受直接值或函数返回值。

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

bindValue() 接受直接值、变量和函数返回值。

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

第三个参数bindValue或bindParam

第三个参数是数据类型,默认为字符串,但您可以根据值类型进行更改。

$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

要使用SQL Server,您需要安装驱动程序并在php.ini中配置一些行,如上面的示例所示

copiando

  • 最后,编辑文件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