my_port.=$type.$this->my_port; // $this->my_host=$type.$this->my_host; // $this->my_dbname=$dbname; // $this->my_user=$user; // $this->my_pwd=$pwd; // } /** * 构造函数 * 私有 */ private function __construct() { $this->my_dbname = TRUE_CONN_DB; $this->my_port = TRUE_CONN_PORT; $this->my_host = TRUE_CONN_HOST; $this->my_user = TRUE_CONN_USER; $this->my_pwd = TRUE_CONN_PASSWORD; // if (CONN_DB_NAME == CSTEST_MYSQL_DB) { // $this->my_port = CSTEST_MYSQL_PORT; // $this->my_host = CSTEST_MYSQL_HOST; // $this->my_dbname = CSTEST_MYSQL_DB; // $this->my_user = CSTEST_MYSQL_USER; // $this->my_pwd = CSTEST_MYSQL_PASSWORD; // } else if (CONN_DB_NAME == CS_MYSQL_DB) { // $this->my_port = CS_MYSQL_PORT; // $this->my_host = CS_MYSQL_HOST; // $this->my_dbname = CS_MYSQL_DB; // $this->my_user = CS_MYSQL_USER; // $this->my_pwd = CS_MYSQL_PASSWORD; // } else if (CONN_DB_NAME == WAICE_MYSQL_DB) { // // } $port = $this->my_port ? $this->my_port : 3306; try { $this->my_pdo = new PDO("mysql:host=" . $this->my_host . ";port=" . $port . ";dbname=" . $this->my_dbname, $this->my_user, $this->my_pwd, array(PDO::MYSQL_ATTR_INIT_COMMAND => "set names utf8")); } catch (PDOException $e) { writeLog('new PDO failed:' . $e->getMessage()); } } /** * 防止被克隆 * */ private function __clone() { } // single public static function getInstance() { if (FALSE == (self::$_instance instanceof self)) { self::$_instance = new self(); } return self::$_instance; } // 关闭数据库 function closeDB() { $this->my_pdo = null; } // 该类销毁时执行 function __destruct() { $this->closeDB(); } // 执行存储过程 返回值是多维数组 eg: $data['rowset']=[[{name:"",age:"18"},{name:"",age:"18"}],[{res_id:"10",res_name:"xxx酒店"},{res_id:"10",res_name:"xxx酒店"}]]; function execProcedure($sql) { $result = $this->my_pdo->query($sql); $data = array(); $temp_data = array(); $data['code'] = "0"; $data['info'] = ""; if (!$result) { $data['code'] = "2"; $data['info'] = "系统异常"; } else { do { $rowset = $result->fetchAll(PDO::FETCH_ASSOC); if ($rowset) { $temp_data[] = $rowset; } else { $temp_data[] = array(); } } while ($result->nextRowset()); $result->closeCursor(); $data['rowset'] = $temp_data; } return $data; } function execProcedure2($sql) { $result = $this->my_pdo->query($sql); $data = array(); $temp_data = array(); $data['code'] = "0"; $data['info'] = ""; if (!$result) { $data['code'] = "2"; $data['info'] = "系统异常"; } else { do { $rowset = $result->fetchAll(PDO::FETCH_ASSOC); if ($rowset && !empty($rowset)) { if (isset($rowset['errcode'])) { $temp_data['code'] = $rowset['errcode']; $temp_data['info'] = $rowset['errinfo']; } elseif (isset($rowset[0])) { $temp_data['code'] = $rowset[0]['errcode']; $temp_data['info'] = $rowset[0]['errinfo']; } } else { $temp_data[] = array(); } } while ($result->nextRowset()); $result->closeCursor(); $data = $temp_data; } return $data; } // 执行单行sql语句 function queryBySql($sql) { $result = $this->my_pdo->query($sql); if (!$result) { $data['code'] = "1"; $data['info'] = "系统异常"; $data['rowset'] = array(); } else { $rowset = $result->fetchAll(PDO::FETCH_ASSOC); $result->closeCursor(); $data['code'] = "0"; $data['info'] = ""; $data['rowset'] = $rowset; } return $data; } // 执行单条增删改sql语句 function execSql($sql) { $result = $this->my_pdo->exec($sql); if ($result === false) { $data['code'] = "1"; $data['info'] = "sql error"; } else { // $result -> closeCursor(); --修改貌似不需要 $data['code'] = "0"; $data['info'] = ""; } return $data; } // 执行增删改多条sql语句 function execSqlArr($sqlArr) { $data = array(); $data['code'] = "0"; $data['info'] = ""; try { $this->my_pdo->beginTransaction(); foreach ($sqlArr as $v) { $data = $this->execSql($v); if ($data['code'] != "0") { throw new PDOException("事务异常"); } } $this->my_pdo->commit(); } catch (PDOException $ex) { $data['code'] = "17"; $data['info'] = "事务异常"; $this->my_pdo->rollBack(); } return $data; } }