-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmytest.php
264 lines (210 loc) · 10.3 KB
/
mytest.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
<!--Test Oracle file for UBC CPSC304 2018 Winter Term 1
Created by Jiemin Zhang
Modified by Simona Radu
Modified by Jessica Wong (2018-06-22)
This file shows the very basics of how to execute PHP commands
on Oracle.
Specifically, it will drop a table, create a table, insert values
update values, and then query for values
IF YOU HAVE A TABLE CALLED "demoTable" IT WILL BE DESTROYED
The script assumes you already have a server set up
All OCI commands are commands to the Oracle libraries
To get the file to work, you must place it somewhere where your
Apache server can run it, and you must rename it to have a ".php"
extension. You must also change the username and password on the
OCILogon below to be your ORACLE username and password -->
<html>
<head>
<title>CPSC 304 PHP/Oracle Demonstration</title>
</head>
<body>
<h2>Reset</h2>
<p>If you wish to reset the table press on the reset button. If this is the first time you're running this page, you MUST use reset</p>
<form method="POST" action="oracle-test.php">
<!-- if you want another page to load after the button is clicked, you have to specify that page in the action parameter -->
<input type="hidden" id="resetTablesRequest" name="resetTablesRequest">
<p><input type="submit" value="Reset" name="reset"></p>
</form>
<hr />
<h2>Insert Values into DemoTable</h2>
<form method="POST" action="oracle-test.php"> <!--refresh page when submitted-->
<input type="hidden" id="insertQueryRequest" name="insertQueryRequest">
Number: <input type="text" name="insNo"> <br /><br />
Name: <input type="text" name="insName"> <br /><br />
<input type="submit" value="Insert" name="insertSubmit"></p>
</form>
<hr />
<h2>Update Name in DemoTable</h2>
<p>The values are case sensitive and if you enter in the wrong case, the update statement will not do anything.</p>
<form method="POST" action="oracle-test.php"> <!--refresh page when submitted-->
<input type="hidden" id="updateQueryRequest" name="updateQueryRequest">
Old Name: <input type="text" name="oldName"> <br /><br />
New Name: <input type="text" name="newName"> <br /><br />
<input type="submit" value="Update" name="updateSubmit"></p>
</form>
<hr />
<h2>Count the Tuples in DemoTable</h2>
<form method="GET" action="oracle-test.php"> <!--refresh page when submitted-->
<input type="hidden" id="countTupleRequest" name="countTupleRequest">
<input type="submit" name="countTuples"></p>
</form>
<?php
//this tells the system that it's no longer just parsing html; it's now parsing PHP
$success = True; //keep track of errors so it redirects the page only if there are no errors
$db_conn = NULL; // edit the login credentials in connectToDB()
$show_debug_alert_messages = False; // set to True if you want alerts to show you which methods are being triggered (see how it is used in debugAlertMessage())
function debugAlertMessage($message) {
global $show_debug_alert_messages;
if ($show_debug_alert_messages) {
echo "<script type='text/javascript'>alert('" . $message . "');</script>";
}
}
function executePlainSQL($cmdstr) { //takes a plain (no bound variables) SQL command and executes it
//echo "<br>running ".$cmdstr."<br>";
global $db_conn, $success;
$statement = OCIParse($db_conn, $cmdstr);
//There are a set of comments at the end of the file that describe some of the OCI specific functions and how they work
if (!$statement) {
echo "<br>Cannot parse the following command: " . $cmdstr . "<br>";
$e = OCI_Error($db_conn); // For OCIParse errors pass the connection handle
echo htmlentities($e['message']);
$success = False;
}
$r = OCIExecute($statement, OCI_DEFAULT);
if (!$r) {
echo "<br>Cannot execute the following command: " . $cmdstr . "<br>";
$e = oci_error($statement); // For OCIExecute errors pass the statementhandle
echo htmlentities($e['message']);
$success = False;
}
return $statement;
}
function executeBoundSQL($cmdstr, $list) {
/* Sometimes the same statement will be executed several times with different values for the variables involved in the query.
In this case you don't need to create the statement several times. Bound variables cause a statement to only be
parsed once and you can reuse the statement. This is also very useful in protecting against SQL injection.
See the sample code below for how this function is used */
global $db_conn, $success;
$statement = OCIParse($db_conn, $cmdstr);
if (!$statement) {
echo "<br>Cannot parse the following command: " . $cmdstr . "<br>";
$e = OCI_Error($db_conn);
echo htmlentities($e['message']);
$success = False;
}
foreach ($list as $tuple) {
foreach ($tuple as $bind => $val) {
//echo $val;
//echo "<br>".$bind."<br>";
OCIBindByName($statement, $bind, $val);
unset ($val); //make sure you do not remove this. Otherwise $val will remain in an array object wrapper which will not be recognized by Oracle as a proper datatype
}
$r = OCIExecute($statement, OCI_DEFAULT);
if (!$r) {
echo "<br>Cannot execute the following command: " . $cmdstr . "<br>";
$e = OCI_Error($statement); // For OCIExecute errors, pass the statementhandle
echo htmlentities($e['message']);
echo "<br>";
$success = False;
}
}
}
function printResult($result) { //prints results from a select statement
echo "<br>Retrieved data from table demoTable:<br>";
echo "<table>";
echo "<tr><th>ID</th><th>Name</th></tr>";
while ($row = OCI_Fetch_Array($result, OCI_BOTH)) {
echo "<tr><td>" . $row["NID"] . "</td><td>" . $row["NAME"] . "</td></tr>"; //or just use "echo $row[0]"
}
echo "</table>";
}
function connectToDB() {
global $db_conn;
// Your username is ora_(CWL_ID) and the password is a(student number). For example,
// ora_platypus is the username and a12345678 is the password.
$db_conn = OCILogon("ora_mhlchina", "a28325181", "dbhost.students.cs.ubc.ca:1522/stu");
if ($db_conn) {
debugAlertMessage("Database is Connected");
return true;
} else {
debugAlertMessage("Cannot connect to Database");
$e = OCI_Error(); // For OCILogon errors pass no handle
echo htmlentities($e['message']);
return false;
}
}
function disconnectFromDB() {
global $db_conn;
debugAlertMessage("Disconnect from Database");
OCILogoff($db_conn);
}
function handleUpdateRequest() {
global $db_conn;
$old_name = $_POST['oldName'];
$new_name = $_POST['newName'];
// you need the wrap the old name and new name values with single quotations
executePlainSQL("UPDATE demoTable SET name='" . $new_name . "' WHERE name='" . $old_name . "'");
OCICommit($db_conn);
}
function handleResetRequest() {
global $db_conn;
// Drop old table
executePlainSQL("DROP TABLE demoTable");
// Create new table
echo "<br> creating new table <br>";
executePlainSQL("CREATE TABLE demoTable (id int PRIMARY KEY, name char(30))");
OCICommit($db_conn);
}
function handleInsertRequest() {
global $db_conn;
//Getting the values from user and insert data into the table
$tuple = array (
":bind1" => $_POST['insNo'],
":bind2" => $_POST['insName']
);
$alltuples = array (
$tuple
);
executeBoundSQL("insert into demoTable values (:bind1, :bind2)", $alltuples);
OCICommit($db_conn);
}
function handleCountRequest() {
global $db_conn;
$result = executePlainSQL("SELECT Count(*) FROM demoTable");
if (($row = oci_fetch_row($result)) != false) {
echo "<br> The number of tuples in demoTable: " . $row[0] . "<br>";
echo "<br> TEST <br>";
}
}
// HANDLE ALL POST ROUTES
// A better coding practice is to have one method that reroutes your requests accordingly. It will make it easier to add/remove functionality.
function handlePOSTRequest() {
if (connectToDB()) {
if (array_key_exists('resetTablesRequest', $_POST)) {
handleResetRequest();
} else if (array_key_exists('updateQueryRequest', $_POST)) {
handleUpdateRequest();
} else if (array_key_exists('insertQueryRequest', $_POST)) {
handleInsertRequest();
}
disconnectFromDB();
}
}
// HANDLE ALL GET ROUTES
// A better coding practice is to have one method that reroutes your requests accordingly. It will make it easier to add/remove functionality.
function handleGETRequest() {
if (connectToDB()) {
if (array_key_exists('countTuples', $_GET)) {
handleCountRequest();
}
disconnectFromDB();
}
}
if (isset($_POST['reset']) || isset($_POST['updateSubmit']) || isset($_POST['insertSubmit'])) {
handlePOSTRequest();
} else if (isset($_GET['countTupleRequest'])) {
handleGETRequest();
}
?>
</body>
</html>