项目作者: cagartner

项目描述 :
基于PHP类PDO的slqanywhere / sybase连接的客户端
高级语言: PHP
项目地址: git://github.com/cagartner/sql-anywhere-client.git
创建时间: 2014-05-28T16:49:39Z
项目社区:https://github.com/cagartner/sql-anywhere-client

开源协议:MIT License

下载


SQLAnywhereClient

Classe para conexão com banco de dados Sybase com PHP baseada na biblioteca sqlanywhere.
Class for connection with database Sybase with PHP, created for PHP library SqlAnywhere.

The development was based on PDO Native Class.

TODO:

  • More tests.

Installation

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

1- First install sqlanywhere module for PHP Click Here!.

2- Use composer to install this package adding the lines bellow in the require section require:
// …
“require”: {
“cagartner/SQLAnywhereClient”: “dev-master”
},
// …

How to use

Bellow have some examples of how to use this class.

Connection SQLAnywhereClient::__construct:

  1. <?php
  2. require '../vendor/autoload.php';
  3. use Cagartner\SQLAnywhereClient;
  4. try {
  5. $dns = "uid={user};pwd={password};ENG={database-name};commlinks=tcpip{host={host};port={port}}";
  6. $con = new SQLAnywhereClient( $dns );
  7. } catch (Exception $e) {
  8. echo $e->getMessage();
  9. }
  10. ?>

Você pode definir duas opções iniciais junto com a conexão, que são as seguintes: auto_commit e is_persistent.
You can define two initials configuration params with the connection: auto_commit and is_persistent.

  • auto_commit Enable auto commit, default is true;
  • is_persistent Define persistent mode, default is false;
  1. <?php
  2. require '../vendor/autoload.php';
  3. use Cagartner\SQLAnywhereClient;
  4. try {
  5. $dns = "uid={uid};pwd={password};ENG={};commlinks=tcpip{host={host};port={password}}";
  6. $autocommit = false;
  7. $persistent = true;
  8. $con = new SQLAnywhereClient( $dns, $autocommit, $persistent );
  9. } catch (Exception $e) {
  10. echo $e->getMessage();
  11. }
  12. ?>

Executing SQL commands SQLAnywhereClient::exec():

  1. <?php
  2. $sql = "SELECT * FROM users";
  3. $result = $con->exec( $sql );
  4. echo "<pre>";
  5. print_r($result->fetch());
  6. echo "</pre>";
  7. exit;
  8. ?>

Executing SQL commands with retrieve of data SQLAnywhereClient::query() :

This method return an array with the data

  1. <?php
  2. $sql = "SELECT name, email FROM users";
  3. foreach ($con->query( $sql ) as $result) {
  4. print_r($result);
  5. }
  6. exit;
  7. ?>

Retrieve just one line SQLAnywhereQuery::fetch

Return first row

  1. <?php
  2. $sql = "SELECT name, email FROM users";
  3. $result = $con->exec( $sql );
  4. $user = $result->fetch();
  5. print_r($user);
  6. exit;
  7. ?>

Data format returns

You can choose how is the format that your data is retrieve SQLAnywhereClient

  1. <?php
  2. // Retornar em um array com idexação por numero e coluna
  3. SQLAnywhereClient::FETCH_ARRAY;
  4. // Retornar em um array com idexação por coluna
  5. SQLAnywhereClient::FETCH_ASSOC; // Formato Padrão!
  6. // Retornar em um array com idexação por coluna
  7. SQLAnywhereClient::FETCH_OBJECT;
  8. // Retornar em um array com idexação por linha de dados
  9. SQLAnywhereClient::FETCH_ROW;
  10. // Retornar em um array com idexação por colunas
  11. SQLAnywhereClient::FETCH_FIELD;
  12. ?>

Example:

  1. <?php
  2. $sql = "SELECT name, email FROM users";
  3. $result = $con->exec( $sql );
  4. $user = $result->fetch( SQLAnywhereClient::FETCH_OBJECT );
  5. print_r($user);
  6. exit;
  7. ?>

Return all rows SQLAnywhereQuery::fetchAll

Return all selected rows

  1. <?php
  2. $sql = "SELECT name, email FROM users";
  3. $result = $con->exec( $sql );
  4. $user = $result->fetchAll();
  5. print_r($user);
  6. exit;
  7. ?>

In this method you also can choose the format of return too:

  1. <?php
  2. $sql = "SELECT name, email FROM users";
  3. $result = $con->exec( $sql );
  4. $user = $result->fetchAll( SQLAnywhereClient::FETCH_OBJECT );
  5. print_r($user);
  6. exit;
  7. ?>

Row count SQLAnywhereQuery::rowCount

Return the count of rows

  1. <?php
  2. $sql = "SELECT name, email FROM users";
  3. $result = $con->exec( $sql );
  4. echo "We find " . $result->rowCount() . " itens.";
  5. exit;
  6. ?>

Or with count alias:

  1. <?php
  2. $sql = "SELECT name, email FROM user";
  3. $result = $con->exec( $sql );
  4. echo "We find " . $result->count() . " itens.";
  5. exit;
  6. ?>

Field count SQLAnywhereQuery::fieldCount

Return the total of fields

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

Last ID SQLAnywhereClient::lastInsertId()

Return the last inserted ID

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

Prepared Statement SQLAnywhereClient::prepare():

Prepared Statement with ?:

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

And this params names:

  1. <?php
  2. $sql = "INSERT INTO users name, email VALUES (:name, :email)";
  3. $stmnt = $con->prepare( $sql );
  4. if ($stmnt->execute(array(
  5. ':name' => 'Carlos',
  6. ':email' => 'contato@carlosgartner.com.br'
  7. ))) {
  8. echo $con->lastInsertId();
  9. }
  10. exit;
  11. ?>

Bind Param SQLAnywherePrepared::bindParam():

  1. <?php
  2. $sql = "INSERT INTO users name, email VALUES (:name, :email)";
  3. $stmnt = $con->prepare( $sql );
  4. $name = "Carlos A.";
  5. $email = "contato@carlosgartner.com.br";
  6. $stmnt->bindParam(':name', $name);
  7. $stmnt->bindParam(':email', $email);
  8. if ($stmnt->execute()) {
  9. echo $con->lastInsertId();
  10. }
  11. exit;
  12. ?>