note 118093 added to ref.pdo-dblib
| From: | Johankasselman at live dot com | Date: | Fri, 02 Oct 2015 13:32:54 +0000 |
| Subject: | note 118093 added to ref.pdo-dblib | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-203894@lists.php.net to get a copy of this message | ||
Hi All, I'wrote a class to connect to MSSQL/Azure databases with Transaction support.
Hope this can help anyone!
<?php
/**
* @author Johan Kasselman <johankasselman@live.com>
* @since 2015-09-28 V1
*
*/
class pdo_dblib_mssql{
private $db;
private $cTransID;
private $childTrans = array();
public function __construct($hostname, $port, $dbname, $username, $pwd){
$this->hostname = $hostname;
$this->port = $port;
$this->dbname = $dbname;
$this->username = $username;
$this->pwd = $pwd;
$this->connect();
}
public function beginTransaction(){
$cAlphanum = "AaBbCc0Dd1EeF2fG3gH4hI5iJ6jK7kLlM8mN9nOoPpQqRrSsTtUuVvWwXxYyZz";
$this->cTransID = "T".substr(str_shuffle($cAlphanum), 0, 7);
array_unshift($this->childTrans, $this->cTransID);
$stmt = $this->db->prepare("BEGIN TRAN [$this->cTransID];");
return $stmt->execute();
}
public function rollBack(){
while(count($this->childTrans) > 0){
$cTmp = array_shift($this->childTrans);
$stmt = $this->db->prepare("ROLLBACK TRAN [$cTmp];");
$stmt->execute();
}
return $stmt;
}
public function commit(){
while(count($this->childTrans) > 0){
$cTmp = array_shift($this->childTrans);
$stmt = $this->db->prepare("COMMIT TRAN [$cTmp];");
$stmt->execute();
}
return $stmt;
}
public function close(){
$this->db = null;
}
public function connect(){
try {
$this->db = new PDO
("dblib:host=$this->hostname:$this->port;dbname=$this->dbname",
"$this->username", "$this->pwd");
} catch (PDOException $e) {
$this->logsys .= "Failed to get DB handle: " . $e->getMessage() .
"\n";
}
}
}
?>
----
Server IP: 72.52.91.14
Probable Submitter: 41.160.27.251
----
Manual Page -- http://php.net/manual/en/ref.pdo-dblib.php
Edit -- https://master.php.net/note/edit/118093
Del: integrated -- https://master.php.net/note/delete/118093/integrated
Del: useless -- https://master.php.net/note/delete/118093/useless
Del: bad code -- https://master.php.net/note/delete/118093/bad+code
Del: spam -- https://master.php.net/note/delete/118093/spam
Del: non-english -- https://master.php.net/note/delete/118093/non-english
Del: in docs -- https://master.php.net/note/delete/118093/in+docs
Del: other reasons-- https://master.php.net/note/delete/118093
Reject -- https://master.php.net/note/reject/118093
Search -- https://master.php.net/manage/user-notes.php