-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdbConnExec.php
More file actions
83 lines (63 loc) · 1.87 KB
/
dbConnExec.php
File metadata and controls
83 lines (63 loc) · 1.87 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
<?php
/*
Purpose: Methods to connect to and execute queries on the RWStudios Database
Author: LV
Date: February 2013
*/
// function to connect to the database
function dbConnect()
{
$serverName = 'bussql2012-cis';
$uName = 'etadmin';
$pWord = 'cis665';
$db = 'EstimationToolDB';
try
{
//instantiate a PDO object and set connection properties
$conn = new PDO("sqlsrv:Server=$serverName; Database=$db", $uName, $pWord, array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION));
//return connection object
return $conn;
}
// if connection fails
catch (PDOException $e)
{
die('Connection failed: ' . $e->getMessage());
}
}
//method to execute a query - the SQL statement to be executed, is passed to it
function executeQuery($query)
{
// call the dbConnect function
$conn = dbConnect();
try
{
// execute query and assign results to a PDOStatement object
$stmt = $conn->query($query);
do
{
if ($stmt->columnCount() > 0) // if rows with columns are returned
{
$results = $stmt->fetchAll(PDO::FETCH_ASSOC); //retreive the rows as an associative array
}
} while ($stmt->nextRowset()); // if multiple queries are executed, repeat the process for each set of results
// echo '<pre style="font-size:large">';
// print_r($results);
// echo '</pre>';
// die;
//call dbDisconnect() method to close the connection
dbDisconnect($conn);
return $results;
}
catch (PDOException $e)
{
//if execution fails
dbDisconnect($conn);
die ('Query failed: ' . $e->getMessage());
}
}
function dbDisconnect($conn)
{
// closes the specfied connection and releases associated resources
$conn = null;
}
?>