-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSqlHelper.class.php
More file actions
54 lines (50 loc) · 1.58 KB
/
SqlHelper.class.php
File metadata and controls
54 lines (50 loc) · 1.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
<?php
class SqlHelper{
private $host;
private $port;
private $username;
private $password;
private $db;
private $link;
public function __construct(){
$ini_path='e:/xampp/htdocs/xiaoshuocurl/db.ini';
$db_ini_arr=parse_ini_file($ini_path);//从配置文件夹读取数据库配置信息
$this->host=$db_ini_arr['host'];
$this->port=$db_ini_arr['port'];
$this->username=$db_ini_arr['username'];
$this->password=$db_ini_arr['password'];
$this->db=$db_ini_arr['db'];
$dsn="mysql:dbname={$this->db};host={$this->host};port={$this->port}";
try {
$this->link = new PDO($dsn, $this->username,$this->password);
} catch (PDOException $e) {
echo 'Connection failed: ' . $e->getMessage();
}
$this->link->exec("set names utf8");
}
//执行查询语句,并以二维数组的形式进行返回
public function dml($sql){
$res=$this->link->exec($sql);
if($res){
return 1;//执行成功
}else{
if($res==0){
return 2;//影响行数为0
}
return 0;//执行失败
}
}
public function dql_arr($sql){
$arr=array();
$res=$this->link->query($sql);
while($row=$res->fetch()){
$arr[]=$row;//把得到的结果保存到数组里面
}
$res=null;
$this->close_connect();//关闭连接
return $arr;//返回数据
}
public function close_connect(){
$this->link=null;//关闭数据库连接
}
}