cagartner/sql-anywhere-client

与Sybase连接的客户端

dev-master 2018-03-08 15:07 UTC

This package is auto-updated.

Last update: 2024-09-18 17:53:04 UTC


README

用于PHP的基于SqlAnywhere库的Sybase数据库连接类。用于PHP库SqlAnywhere的连接Sybase数据库的类。

开发基于PDO原生类。

待办事项

  • 更多测试。

安装

=================

1- 首先安装PHP的sqlanywhere模块 点击这里!

2- 使用composer安装此包,在require部分添加以下行 require: // ... "require": { "cagartner/SQLAnywhereClient": "dev-master" }, // ...

如何使用

以下是一些使用此类的示例。

连接 SQLAnywhereClient::__construct

<?php
    require '../vendor/autoload.php';

    use Cagartner\SQLAnywhereClient;

    try {
        $dns = "uid={user};pwd={password};ENG={database-name};commlinks=tcpip{host={host};port={port}}";
        $con = new SQLAnywhereClient( $dns );
    } catch (Exception $e) {
        echo $e->getMessage();
    }
?>

您可以在连接时定义两个初始配置参数,分别是:auto_commitis_persistent

  • auto_commit 启用自动提交,默认为true
  • is_persistent 定义持久模式,默认为false
<?php
    require '../vendor/autoload.php';

    use Cagartner\SQLAnywhereClient;

    try {
        $dns = "uid={uid};pwd={password};ENG={};commlinks=tcpip{host={host};port={password}}";
        $autocommit = false;
        $persistent = true;

        $con = new SQLAnywhereClient( $dns, $autocommit, $persistent );
    } catch (Exception $e) {
        echo $e->getMessage();
    }
?>

执行SQL命令 SQLAnywhereClient::exec()

<?php

    $sql = "SELECT * FROM users";
    $result = $con->exec( $sql );

    echo "<pre>";
    print_r($result->fetch());
    echo "</pre>";
    exit;
?>

执行带数据检索的SQL命令 SQLAnywhereClient::query()

此方法返回一个包含数据的数组

<?php

    $sql = "SELECT name, email FROM users";

    foreach ($con->query( $sql ) as $result) {
        print_r($result);
    }
    exit;
?>

检索一行 SQLAnywhereQuery::fetch

返回第一行

<?php
    $sql = "SELECT name, email FROM users";
    $result = $con->exec( $sql );
    $user = $result->fetch();

    print_r($user);
    exit;
?>

数据返回格式

您可以选择以何种格式检索数据 SQLAnywhereClient

<?php
    // Retornar em um array com idexação por numero e coluna
    SQLAnywhereClient::FETCH_ARRAY;

    // Retornar em um array com idexação por coluna
    SQLAnywhereClient::FETCH_ASSOC; // Formato Padrão!

    // Retornar em um array com idexação por coluna
    SQLAnywhereClient::FETCH_OBJECT;

    // Retornar em um array com idexação por linha de dados
    SQLAnywhereClient::FETCH_ROW;

    // Retornar em um array com idexação por colunas
    SQLAnywhereClient::FETCH_FIELD;
?>

示例

<?php

    $sql = "SELECT name, email FROM users";
    $result = $con->exec( $sql );
    $user = $result->fetch( SQLAnywhereClient::FETCH_OBJECT );

    print_r($user);
    exit;
?>

返回所有行 SQLAnywhereQuery::fetchAll

返回所有选择的行

<?php
    $sql = "SELECT name, email FROM users";
    $result = $con->exec( $sql );
    $user = $result->fetchAll();

    print_r($user);
    exit;
?>

在此方法中,您还可以选择返回格式

<?php

    $sql = "SELECT name, email FROM users";
    $result = $con->exec( $sql );
    $user = $result->fetchAll( SQLAnywhereClient::FETCH_OBJECT );

    print_r($user);
    exit;
?>

行数 SQLAnywhereQuery::rowCount

返回行数

<?php
    $sql = "SELECT name, email FROM users";
    $result = $con->exec( $sql );

    echo "We find " . $result->rowCount() . " itens.";
    exit;
?>

或使用count别名

<?php
    $sql = "SELECT name, email FROM user";
    $result = $con->exec( $sql );

    echo  "We find " . $result->count() . " itens.";
    exit;
?>

字段数 SQLAnywhereQuery::fieldCount

返回字段总数

<?php
    $sql = "SELECT name, email FROM user";
    $result = $con->exec( $sql );
    
    echo  "We find " . $result->fieldCount() . " fields.";
    exit;
?>

最后ID SQLAnywhereClient::lastInsertId()

返回最后插入的ID

<?php
    $sql = "INSERT INTO user  name, email VALUES ('Carlos', 'contato@carlosgartner.com.br')";
    if ($con->exec( $sql )) {
        echo $con->lastInsertId();
    }
    exit;
?>

预处理语句 SQLAnywhereClient::prepare()

?的预处理语句

<?php
    $sql = "INSERT INTO users  name, email VALUES (?, ?)";
    $stmnt = $con->prepare( $sql );
    if ($stmnt->execute(array('Carlos', 'contato@carlosgartner.com.br'))) {
         echo $con->lastInsertId();
    }
    exit;
?>

和这些参数名称

<?php
    $sql = "INSERT INTO users  name, email VALUES (:name, :email)";
    $stmnt = $con->prepare( $sql );
    if ($stmnt->execute(array(
        ':name' => 'Carlos', 
        ':email' => 'contato@carlosgartner.com.br'
    ))) {
         echo $con->lastInsertId();
    }
    exit;
?>

绑定参数 SQLAnywherePrepared::bindParam()

<?php
    $sql = "INSERT INTO users  name, email VALUES (:name, :email)";
    $stmnt = $con->prepare( $sql );

    $name = "Carlos A.";
    $email = "contato@carlosgartner.com.br";

    $stmnt->bindParam(':name', $name);
    $stmnt->bindParam(':email', $email);

    if ($stmnt->execute()) {
         echo $con->lastInsertId();
    }
    exit;
?>