php pdo

CODE

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

Class Db {

private static $_db = null;
private function __construct(){
self::$_db = new PDO('mysql:host=127.0.0.1;dbname=tp6', 'root', 'root');
}


private static function _get_db(){
if(self::$_db == null ){
$obj = new self();
}
return self::$_db;
}

private static function _back_msg($errcode, $errmsg){
die( json_encode(['code'=>$errcode,'errmsg'=>$errmsg]) );
}

public static function register($uname, $pwd){
$db = self::_get_db();

$query = $db->prepare("select count(*) from user where `username` = ?");
$query->execute([$uname]);
$count = $query->fetchAll();

if(intval( $count[0]['count(*)']) > 0 ){
self::_back_msg(1005,'username is exists !');
}

if( strlen($pwd) < 8 ){
self::_back_msg(1006,'password is short than 8 !');
}else{
$password = self::_password_generate($pwd);
}

$query = $db->prepare("insert into `user` (username,password,age) VALUES (?,?,?)");
$res = $query->execute([$uname, $password, 21]);
if(!$res){
self::_back_msg(1006,'write into db error!');
}
return true;
}

private static function _password_generate($pwd){
return md5($pwd);
}

}

Db::register('dd1ab','ccsssssssssssssssssss');