-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconnection.php
82 lines (63 loc) · 1.77 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
<?php
class DBHelper {
static private $dbHelper;
public static function getDBHelperInstance() {
if (!self::$dbHelper) {
self::$dbHelper = new dbHelper();
}
return self::$dbHelper;
}
private function getDB()
{
$db = mysqli_init();
$db->options(MYSQLI_OPT_INT_AND_FLOAT_NATIVE, 1);
$db->real_connect("127.0.0.1", "root", "", "se174", 3306);
if ($db->connect_errno) {
echo "Failed to connect to MySQL: (" . $db->connect_errno . ") " . $db->connect_error;
}
echo $db->host_info . "\n";
return $db;
}
private function performQuery($sqlStatement='')
{
return $this->getDB()->query($sqlStatement);
}
private function closeConnection()
{
$this->getDB()->close();
}
public function registerUser($name='', $uname='', $pword='pword YO!', $email='')
{
$sqlAddUser = "INSERT INTO user (name, username, password, email) VALUES ('{$name}', '{$uname}', '{$pword}', '{$email}')";
echo "SQLADDUSER: {$sqlAddUser}";
if ($this->performQuery($sqlAddUser) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sqlAddUser . "<br>" . $this->getDB()->error;
}
$this->closeConnection();
}
public function userAuthentication($uname, $pword)
{
$sql="SELECT username,password FROM user";
$toReturn = false;
if ($result=mysqli_query($this->getDB(),$sql))
{
while ($obj=mysqli_fetch_object($result))
{
printf("\n\nusername: %s \n passwords: ---%s (%s)\n\n\n",$obj->username,$pword,$obj->password);
if ($toReturn = password_verify($pword, $obj->password)) {
return $toReturn;
}
}
// Free result set
mysqli_free_result($result);
}
mysqli_close($this->getDB());
return $toReturn;
}
}
/*
setup the database connection and connect to se174 database
*/
?>