-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdatabase.php
109 lines (95 loc) · 2.51 KB
/
database.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
<?php
//this Database class is non-static
class Database
{
#config local PC
// private $dbName = 'antiques_dbintegration' ;
// private $dbHost = 'localhost' ;
// private $dbUsername = 'root';
// private $dbUserPassword = '';
// private $db_connection = null;
#config for AntiquesPride.edu.ph
private $dbName = 'antiques_dbintegration' ;
private $dbHost = 'localhost' ;
private $dbUsername = 'root';
private $dbUserPassword = 'toor';
private $db_connection = null;
public function __construct() {
//exit('Init function is not allowed');
//establish a connection right while being constructed
$this->db_connection = $this->connect();
}
/**
* Basic Connect and Disconnect
*/
public function connect()
{
#Config for Heroku ClearDB (mysql addon).
#Just comment these out when moving to AntiquesPride; the default values above will be used.
$url = parse_url(getenv("CLEARDB_DATABASE_URL"));
$this->dbName = substr($url["path"], 1);
$this->dbHost = $url["host"];
$this->dbUsername = $url["user"];
$this->dbUserPassword = $url["pass"];
// One connection through whole application
if ( null == $this->db_connection )
{
try
{
$this->db_connection = new PDO( "mysql:host=".$this->dbHost.";"."dbname=".$this->dbName, $this->dbUsername, $this->dbUserPassword);
$this->db_connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch(PDOException $e)
{
die($e->getMessage());
}
}
return $this->db_connection;
}
public function disconnect()
{
$this->db_connection = null;
}
/**
* Utility for just getting database rows.
*/
public function row_query($sql_query)
{
$this->connect();
//we return the rows as PDOStatement
// $rows = $this->db_connection->query($sql_query);
//we return the rows as an array
$query = $this->db_connection->prepare($sql_query);
$query->execute();
$rows = $query->fetchAll();
$this->disconnect();
if (count($rows)==0) return array(); #return an empty array
#otherwise
return $rows;
}
/**
* Utility for just getting a quick row count.
*/
public function count($sql_query, $parameters_array)
{
$this->connect();
$q = $this->db_connection->prepare($sql_query);
$q->execute($parameters_array);
return $q->rowCount();
}
/**
* Utility for executing sql scripts such as Create and Insert
*/
public function exec($sql_script){
$this->connect();
try {
$this->db_connection->exec($sql_script);
}
catch (PDOException $e)
{
echo $e->getMessage();
die();
}
}
}
?>