Skip to content

Connection client for slqanywhere/sybase based on PHP class PDO

License

Notifications You must be signed in to change notification settings

pxlcore/sql-anywhere-client

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

20 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

SQLAnywhereClient

Class to connect to the Sybase database using the SQLAnywhere extension.

This class is based on the native PDO class.

Installation

  1. Install the SQLAnywhere PHP module first.
  2. Use Composer to install the package. Add th following line to require:
    // ...
    "require": {
        "pxlcore/SQLAnywhereClient": "dev-master"
    },
    // ...

How to use

Initialize the connection SQLAnywhereClient::__construct:

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

    use Pxlcore\SQLAnywhereClient;

    try {
        $dsn = "uid={user};pwd={pwd};eng={eng};commlinks=tcpip{host={host};port={port}}";
        $con = new SQLAnywhereClient( $dsn );
    } catch (Exception $e) {
        echo $e->getMessage();
    }
?>

You have two options: auto_commit or is_persistent.

  • auto_commit autocommit, default is true;
  • is_persistent persistent connection, default is false;
<?php
    require '../vendor/autoload.php';

    use Pxlcore\SQLAnywhereClient;

    try {
        $dsn = "uid={uid};pwd={pwd};eng={};commlinks=tcpip{host={host};port={port}}";
        $autocommit = false;
        $persistent = true;

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

Excecute SQL command SQLAnywhereClient::exec():

<?php

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

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

Execute SQL command with return values SQLAnywhereClient::query() :

Returns an array with multiple values

<?php

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

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

Return one row SQLAnywhereQuery::fetch

Returns the first line

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

    print_r($user);
    exit;
?>

Return format

The return format of class SQLAnywhereClient could be

<?php
    // ...
    SQLAnywhereClient::FETCH_ARRAY;

    // ...
    SQLAnywhereClient::FETCH_ASSOC; // default format!

    // ...
    SQLAnywhereClient::FETCH_OBJECT;

    // ...
    SQLAnywhereClient::FETCH_ROW;

    // ...
    SQLAnywhereClient::FETCH_FIELD;
?>

Example:

<?php

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

    print_r($user);
    exit;
?>

Return all rows SQLAnywhereQuery::fetchAll

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

    print_r($user);
    exit;
?>

You could use different return formats.

<?php

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

    print_r($user);
    exit;
?>

Number of rows SQLAnywhereQuery::rowCount

Returns the number of rows.

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

    echo "Number of rows: " . $result->rowCount();
    exit;
?>

Or like

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

    echo "Number of rows: " . $result->count();
    exit;
?>

Number of columns SQLAnywhereQuery::fieldCount

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

    echo "Found " . $result->fieldCount() . " columns in the table.";
    exit;
?>

Last ID SQLAnywhereClient::lastInsertId()

<?php
    $sql = "INSERT INTO users (name, email) VALUES ('John', 'contact@johndoe.com')";
    if ($con->exec( $sql )) {
        echo $con->lastInsertId();
    }
    exit;
?>

Prepared Statement SQLAnywhereClient::prepare():

Use ? for Prepared Statement:

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

or use named parameters:

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

Bind Parameters SQLAnywherePrepared::bindParam():

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

    $name = "John Doe";
    $email = "contact@johndoe.com";

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

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

About

Connection client for slqanywhere/sybase based on PHP class PDO

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • PHP 100.0%