-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdb_functions.php
76 lines (60 loc) · 1.76 KB
/
db_functions.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
<?php
/*
displayResult applies the query $sqlQuery to the database connected through $db
and displays the result
*/
function displayResult($db,$sqlQuery) {
/* Apply the query to the given database */
$resultSet = pg_query($db,$sqlQuery);
if (!$resultSet) {
echo "An error occured: ".pg_last_error($db)."\n";
exit;
}
/* Retrieve the numbers of rows and columns of the result */
$rows = pg_num_rows($resultSet);
$fields = pg_num_fields($resultSet);
/* Create the header of the table */
$tableFields = "";
for($field_num = 0; $field_num < $fields; $field_num++){
$tableFields .= "<th>" . pg_field_name($resultSet,$field_num) . "</th>";
}
/* Create the rows of the table */
$tableTxt = "";
for ($row_num = 0; $row_num < $rows; $row_num++){
$myData = pg_fetch_array($resultSet);
$tableTxt .= "<tr>";
for($field_num = 0; $field_num < $fields; $field_num++){
$tableTxt .= "<td>" . $myData[$field_num]. "</td>";
}
$tableTxt .= "</tr>";
}
/* Draw the table */
echo "<table><tr><thead>";
echo $tableFields;
echo "</thead></tr><tbody>";
echo $tableTxt;
echo "</tbody></table>";
}
/*
db_assocArray applies the query $sqlQuery to the database connected through $db
and stores the result in an associative array
*/
function db_assocArray($db,$sqlQuery){
$resultSet = pg_query($db,$sqlQuery);
if (!$resultSet) {
echo "An error occured: ".pg_last_error($db)."\n";
exit;
}
$data = pg_fetch_assoc($resultSet);
return $data;
}
function db_assocArrayAll($db,$sqlQuery){
$resultSet = pg_query($db,$sqlQuery);
if (!$resultSet) {
echo "An error occured: ".pg_last_error($db)."\n";
exit;
}
$data = pg_fetch_all($resultSet);
return $data;
}
?>