1 changed files with 108 additions and 0 deletions
@ -0,0 +1,108 @@ |
|||
<?php |
|||
|
|||
namespace app\common\dm; |
|||
|
|||
class Dm |
|||
{ |
|||
private $host = 'localhost:5236'; // IP 地址 |
|||
private $port = '5236'; // 端口号 |
|||
private $dbname = 'CXK'; // 数据库名 |
|||
private $username = 'SYSDBA'; // 用户名 |
|||
private $password = 'SYSDBA'; // 密码 |
|||
private $charset = 'GBK'; // 字符集 |
|||
|
|||
public $conn; |
|||
|
|||
public function __construct() |
|||
{ |
|||
$database = config('database'); |
|||
$dm = $database['connections']['dm']; |
|||
$this->conn = dm_connect($dm['hostname'].':'.$dm['hostport'], $dm['username'], $dm['password']); |
|||
if (!$this->conn) { |
|||
die('连接数据库失败!'); |
|||
} |
|||
} |
|||
|
|||
public function insert($table, $data) |
|||
{ |
|||
#$fields = implode(',', array_keys($data)); |
|||
$values = "'" . implode("','", array_values($data)) . "'"; |
|||
|
|||
$sql = 'INSERT INTO '.$table.' VALUES ('.$values.')'; |
|||
|
|||
$res = dm_exec($this->conn, $sql); |
|||
|
|||
dm_free_result($res); |
|||
|
|||
return $res; |
|||
} |
|||
|
|||
// 更新数据 |
|||
public function update($table, $data, $where) |
|||
{ |
|||
$set = array(); |
|||
foreach ($data as $key => $value) { |
|||
$set[] = $key . '=' . "'{$value}'"; |
|||
} |
|||
|
|||
$sql = 'UPDATE '.$table." SET " . implode(",", $set) . ' WHERE '.$where; |
|||
|
|||
$res = dm_exec($this->conn, $sql); |
|||
|
|||
dm_free_result($res); |
|||
|
|||
return $res; |
|||
} |
|||
|
|||
// 删除数据 |
|||
public function delete($table, $where) |
|||
{ |
|||
$sql = 'DELETE FROM '.$table.' WHERE '.$where; |
|||
|
|||
$result = dm_exec($this->conn, $sql); |
|||
|
|||
dm_free_result($result); |
|||
|
|||
return $result; |
|||
} |
|||
|
|||
// 清空表 |
|||
public function truncate($table) |
|||
{ |
|||
|
|||
$sql = 'truncate table '.$table; |
|||
|
|||
$result = dm_exec($this->conn, $sql); |
|||
|
|||
dm_free_result($result); |
|||
|
|||
return $result; |
|||
} |
|||
|
|||
// 查询数据 |
|||
public function select($table, $where = null, $fields = '*', $order = null, $limit = null) |
|||
{ |
|||
$sql = 'SELECT '.$fields.' FROM '.$table; |
|||
if ($where != null) { |
|||
$sql .= ' WHERE '.$where; |
|||
} |
|||
if ($order != null) { |
|||
$sql .= ' ORDER BY '.$order; |
|||
} |
|||
if ($limit != null) { |
|||
$sql .= ' LIMIT '.$limit; |
|||
} |
|||
|
|||
$result = dm_exec($this->conn, $sql); |
|||
$data = array(); |
|||
|
|||
while ($row = dm_fetch_array($result)) { |
|||
$data[] = $row; |
|||
} |
|||
|
|||
dm_free_result($result); |
|||
|
|||
return $data; |
|||
} |
|||
|
|||
} |
|||
Loading…
Reference in new issue