-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathconnection.php
65 lines (45 loc) · 1.53 KB
/
connection.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
<?php
class ConnectDB{
private $servername;
private $username;
private $password;
private $dbname;
public $conn;
public function __construct($servername,$username,$password,$dbname){
$this->setServerName($servername);
$this->setUserName($username);
$this->setPassword($password);
$this->setDatabaseName($dbname);
#echo "$this->servername, $this->username, $this->password, $this->dbname <hr/>";
$this->createConnection();
}
private function setServerName($sn){
$this->servername=$sn;
}
private function setUserName($un){
$this->username = $un;
}
private function setPassword($pw){
$this->password = $pw;
}
private function setDatabaseName($dbn){
$this->dbname = $dbn;
}
private function createConnection(){
$this->conn = new PDO("mysql:host=$this->servername;dbname=$this->dbname",$this->username,$this->password);
$this->conn->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
$this->conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
/*
By using "prepare" and "execute" function of PDO we can avoid sql injection
https://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php
This link is amazing to understand prevent sql injection
*/
}
public function getCon(){
return $this->conn;
}
public function disconnectServer(){
$this->conn=null;
}
}
?>