-
Notifications
You must be signed in to change notification settings - Fork 2
/
function.php
114 lines (98 loc) · 2.76 KB
/
function.php
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
<?php
function I($param){
if($_POST[$param] != null){
return @$_POST[$param];
}else{
return @$_GET[$param];
}
}
class Db
{
protected $db;
function __construct()
{
// if(isset($_SERVER['HTTP_APPNAME'])){
// $host = SAE_MYSQL_HOST_M;
// $port = SAE_MYSQL_PORT;
// $dbname = 'app_'.$_SERVER['HTTP_APPNAME'];
// $dbuser = SAE_MYSQL_USER;
// $dbpwd = SAE_MYSQL_PASS;
// }else{
// $host = "db4free.net";
// $port = 3306;
// $dbname = "zhihutracker";
// $dbuser = "zhihutracker";
// $dbpwd = '123456';
// }
$host = "localhost";
$port = 3306;
$dbname = "zhihutracker";
$dbuser = "zhihutracker";
$dbpwd = 'sxcuic';
$con_str = sprintf("mysql:host=%s;port=%d;dbname=%s",$host,$port,$dbname);
$this->db = new PDO($con_str,$dbuser, $dbpwd);
$this->db->exec("SET NAMES 'utf8';");
}
}
class User extends Db
{
function __construct()
{
parent::__construct();
}
public function GetUserByHash($uhash){
$stmt = $this->db->prepare('SELECT id FROM zh_user WHERE hash = :hash');
$stmt->execute(array('hash' => $uhash));
return $stmt->fetch();
}
public function GetUserId($uhash){
$row = $this->GetUserByHash($uhash);
return $row['id'];
}
public function AddUser($uhash){
$insert = $this->db->prepare('INSERT INTO zh_user(hash) VALUES(:hash)');
$insert->bindParam(':hash', $uhash);
return $insert->execute();
}
}
class Track extends Db
{
function __construct()
{
parent::__construct();
}
public function AddTrack($uid,$question_id,$answer_id){
$insert = $this->db->prepare('INSERT INTO zh_track(userid,questionid,answerid) VALUES(:uid,:qid,:aid)');
$insert->bindParam(':qid', $question_id);
$insert->bindParam(':aid', $answer_id);
$insert->bindParam(':uid', $uid);
return $insert->execute();
}
public function RemoveTrack($uid,$question_id,$answer_id){
$delete = $this->db->prepare('DELETE FROM zh_track WHERE(userid=:uid AND questionid=:qid AND answerid=:aid)');
$delete->bindParam(':qid', $question_id);
$delete->bindParam(':aid', $answer_id);
$delete->bindParam(':uid', $uid);
if($delete->execute()){
return $delete->rowCount();
} else return false;
}
public function GetAll($uid){
$stmt = $this->db->prepare('SELECT * FROM zh_track WHERE userid = :uid');
$stmt->execute(array('uid' => $uid));
return $stmt->fetchAll();
}
public function UpdateTime($uid){
$stmt = $this->db->prepare('UPDATE zh_track SET num=num+1 WHERE userid = :uid');
$stmt->execute(array('uid' => $uid));
}
public function UpdateOneTime($uid,$question_id,$answer_id){
$stmt = $this->db->prepare('UPDATE zh_track SET num=num+1 WHERE(userid = :uid AND questionid = :qid AND answerid = :aid)');
return $stmt->execute(
array(
'uid' => $uid,
'qid' => $question_id,
'aid' => $answer_id
));
}
}