سینگلتون هست کلاس اتصال به دیتابیس
این کد کاملش هست
class Config {
public $driver = 'sqlsrv';
public $dbhost = '127.0.0.1';
public $dbuser = 'test';
public $dbpass = '123456';
public $dbname = 'db';
}
class database {
private static $instance;
private $stmt;
private $error = array();
private $sentinel = 1;
private $affectedRows = 0;
private $countRows = 0;
private function __construct() {
$config = new Config();
try {
$this->instance = new PDO($config->driver.":Server=".$config->dbhost.';Database='.$config->dbname, $config->dbuser, $config->dbpass);
return $this->instance;
} catch (Exception $e) {
echo "can not connect.";
}
}
public static function getInstance() {
if (!self::$instance instanceof self) {
self::$instance = new self;
}
return self::$instance;
}
public function query($sql,$params = null) {
$this->stmt = $this->instance->prepare($sql);
$this->stmt->execute($params);
$this->checkQuery();
}
public function loadResult(){
$singleResult = $this->stmt->fetch(PDO::FETCH_NUM);
return $singleResult[0];
}
public function loadColumn(){
$columnList = array();
while($row = $this->stmt->fetch(PDO::FETCH_NUM)){
$columnList[] = $row[0];
}
$this->countRows = count($columnList);
return $columnList;
}
public function loadObject($class_name = "stdClass"){
$object = $this->stmt->fetchObject($class_name);
return $object;
}
public function loadObjectList($class_name = "stdClass"){
$objectList = array();
while($object = $this->stmt->fetchObject($class_name)){
$objectList[] = $object;
}
$this->countRows = count($objectList);
return $objectList;
}
public function loadAssocRow(){
$assocRow = $this->stmt->fetch(PDO::FETCH_ASSOC);
return $assocRow;
}
public function loadAssocList(){
$assocList = array();
while($row = $this->stmt->fetch(PDO::FETCH_ASSOC)){
$assocList[] = $row;
}
$this->countRows = count($assocList);
return $assocList;
}
public function loadIndexedRow(){
$indexedRow = $this->stmt->fetch(PDO::FETCH_NUM);
return $indexedRow;
}
public function loadIndexedList(){
$indexedList = array();
while($row = $this->stmt->fetch(PDO::FETCH_NUM)){
$indexedList[] = $row;
}
$this->countRows = count($indexedList);
return $indexedList;
}
public function loadJsonObject(){
$jsonObject = json_encode($this->stmt->fetch(PDO::FETCH_ASSOC));
return $jsonObject;
}
public function loadJsonObjectList(){
$index = 1;
$jsonObjectList = "";
while($row = $this->loadAssocList()){
$jsonObjectList .= json_encode($row);
if($index <= (count($this->loadAssocList())-1)){
$jsonObjectList .= ",";
}
}
return $jsonObjectList;
}
public function loadXmlDocument($file = null, $root = 'query',$elementName = 'entry'){
$xml = new DOMDocument('1.0','utf-8');
$table = $xml->createElement($root);
foreach ($this->loadAssocList() as $entry){
$element = $xml->createElement($elementName);
foreach ($entry as $node => $value) {
if ($this->valideXmlValue($value)) {
$field = $xml->createElement($node,$value);
$element->appendChild($field);
} else {
$field = $xml->createElement($node);
$cdata = $xml->createCDATASection($value);
$field->appendChild($cdata);
$element->appendChild($field);
}
}
$table->appendChild($element);
}
$xml->appendChild($table);
if ($file != null) {
return file_put_contents($file, $xml->saveXML());
} else {
return $xml->saveXML();
}
}
public function loadCSVFile($file = null){
if($file == null){
$file = date('Ymd-His');
}
$writer = fopen($file.'.csv', 'w');
$temp = $this->loadIndexedList();
foreach ($temp as $row){
fputcsv($writer, $row);
}
return fclose($writer);
}
public function startTransaction(){
$this->instance->beginTransaction();
$this->sentinel = 1;
$this->affectedRows = 0;
}
public function endTransaction(){
if ($this->sentinel == 1) {
$this->instance->commit();
} else {
$this->instance->rollBack();
}
return $this->sentinel;
}
public function getAffectedRows(){
return $this->affectedRows;
}
public function getCountRows(){
return $this->countRows;
}
public function getError(){
$e = array();
$e['ref'] = $this->error[0];
$e['code'] = $this->error[1];
$e['desc'] = $this->error[2];
return $e;
}
private function checkQuery(){
$this->error = $this->stmt->errorInfo();
if ($this->error[0] != 00000) {
$this->sentinel = 0;
}
// simpre que se realiza un SELECT esto pone $sentinel a 1
$this->affectedRows = $this->stmt->rowCount();
if ($this->affectedRows == 0){
$this->sentinel = 0;
}
}
private function valideXmlValue($value){
$chars = array('<','>','&');
foreach($chars as $ilegal) {
$state = strpos($value, $ilegal);
if($state !== FALSE){
return FALSE;
}
}
return TRUE;
}
}