PHP code example of ticketsource / sql-anywhere-client

1. Go to this page and download the library: Download ticketsource/sql-anywhere-client library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

ticketsource / sql-anywhere-client example snippets



    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();
    }


    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 = "SELECT * FROM users";
    $result = $con->exec( $sql );

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



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

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


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

    print_r($user);
    exit;


    // 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;



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

    print_r($user);
    exit;


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

    print_r($user);
    exit;



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

    print_r($user);
    exit;


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

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


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

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


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


    $sql = "INSERT INTO user  name, email VALUES ('Carlos', '[email protected]')";
    if ($con->exec( $sql )) {
        echo $con->lastInsertId();
    }
    exit;


    $sql = "INSERT INTO users  name, email VALUES (?, ?)";
    $stmnt = $con->prepare( $sql );
    if ($stmnt->execute(array('Carlos', '[email protected]'))) {
         echo $con->lastInsertId();
    }
    exit;


    $sql = "INSERT INTO users  name, email VALUES (:name, :email)";
    $stmnt = $con->prepare( $sql );
    if ($stmnt->execute(array(
        ':name' => 'Carlos', 
        ':email' => '[email protected]'
    ))) {
         echo $con->lastInsertId();
    }
    exit;


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

    $name = "Carlos A.";
    $email = "[email protected]";

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

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