62 lines
1.5 KiB
PHP
62 lines
1.5 KiB
PHP
<?php
|
|
|
|
class Dbhelper0 {
|
|
private $host = DB_HOST;
|
|
private $user = DB_USERNAME;
|
|
private $pwd = DB_PWD;
|
|
private $dbname = 'qiangwan_hw';
|
|
private $dblink;
|
|
private static $instance;
|
|
|
|
private function __construct() {
|
|
// dolog('open db link');
|
|
$obj = $this->dblink = new mysqli($this->host, $this->user, $this->pwd, $this->dbname);
|
|
if ($obj->connect_error) {
|
|
dolog('connect to db0 failed');
|
|
die('connect to db failed');
|
|
}
|
|
$this->dblink->query("set names utf8");
|
|
}
|
|
|
|
public static function getinstance() {
|
|
if (!(self::$instance instanceof self)) {
|
|
self::$instance = new self();
|
|
}
|
|
return self::$instance;
|
|
}
|
|
|
|
public function exec_db($sql) {
|
|
// dolog($sql);
|
|
$rs = $this->dblink->query($sql) or die(dolog($_SERVER['QUERY_STRING']."\n".$this->dblink->error, $sql));
|
|
return $rs;
|
|
}
|
|
|
|
public function exec_db_insertid($sql) {
|
|
$rs = $this->dblink->query($sql) or die(dolog($_SERVER['QUERY_STRING']."\n".$this->dblink->error, $sql));
|
|
return $this->dblink->insert_id;
|
|
}
|
|
|
|
public function getonerecord($sql) {
|
|
// dolog($sql);
|
|
$result = $this->dblink->query($sql) or die(dolog($_SERVER['QUERY_STRING']."\n".$this->dblink->error, $sql));
|
|
$data = $result->fetch_all(MYSQLI_ASSOC);
|
|
mysqli_free_result($result);
|
|
return $data[0];
|
|
}
|
|
|
|
public function close() {
|
|
// dolog('close db link');
|
|
if ($this->dblink) {
|
|
mysqli_close($this->dblink);
|
|
}
|
|
}
|
|
|
|
private function __clone() {}
|
|
|
|
public function __destruct() {
|
|
$this->close();
|
|
}
|
|
}
|
|
|
|
?>
|