Class to connect to the Sybase database using the SQLAnywhere extension.
This class is based on the native PDO class.
- Install the SQLAnywhere PHP module first.
- Use Composer to install the package. Add th following line to
require
:
// ...
"require": {
"pxlcore/SQLAnywhereClient": "dev-master"
},
// ...
<?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 istrue
;is_persistent
persistent connection, default isfalse
;
<?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();
}
?>
<?php
$sql = "SELECT * FROM users";
$result = $con->exec( $sql );
echo "<pre>";
print_r($result->fetch());
echo "</pre>";
exit;
?>
Returns an array with multiple values
<?php
$sql = "SELECT name, email FROM users";
foreach ($con->query( $sql ) as $result) {
print_r($result);
}
exit;
?>
Returns the first line
<?php
$sql = "SELECT name, email FROM users";
$result = $con->exec( $sql );
$user = $result->fetch();
print_r($user);
exit;
?>
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;
?>
<?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;
?>
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;
?>
<?php
$sql = "SELECT name, email FROM users";
$result = $con->exec( $sql );
echo "Found " . $result->fieldCount() . " columns in the table.";
exit;
?>
<?php
$sql = "INSERT INTO users (name, email) VALUES ('John', 'contact@johndoe.com')";
if ($con->exec( $sql )) {
echo $con->lastInsertId();
}
exit;
?>
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;
?>
<?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;
?>