-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathconnection.php
More file actions
executable file
·66 lines (46 loc) · 1.54 KB
/
connection.php
File metadata and controls
executable file
·66 lines (46 loc) · 1.54 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
55
56
57
58
59
60
61
62
63
64
65
66
<?php
class createConnection //create a class for make connection
{
protected static $host;
protected static $username;
protected static $password;
protected static $database;
protected static $port ;
var $myconn;
public function __construct() {
$this->host=$_ENV['DB_HOST'] ;
$this->username=$_ENV['DB_USER']; // specify the sever details for mysql
$this->password=$_ENV['DB_PASSWORD'];
$this->database=$_ENV['DB_DATABASE'];
$this->port = $_ENV['DB_PORT'];
}
function connectToDatabase() // create a function for connect database
{
$conn= mysqli_connect($this->host,$this->username,$this->password, $this->database, $this->port);
if(!$conn)// testing the connection
{
die ("Cannot connect to the database");
}
else
{
$this->myconn = $conn;
// echo "Connection established";
}
return $this->myconn;
}
function selectDatabase() // selecting the database.
{
mysqli_select_db($this->myconn, $this->database); //use php inbuild functions for select database
if(mysqli_error($this->myconn)) // if error occured display the error message
{
echo "Cannot find the database ".$this->database;
}
// echo "Database selected..";
}
function closeConnection() // close the connection
{
mysqli_close($this->myconn);
// echo "Connection closed";
}
}
?>