· 9 years ago · Nov 02, 2016, 02:32 PM
1$db_host = 'localhost';
2$db_port = '3306';
3$db_username = 'root';
4$db_password = 'root';
5$db_primaryDatabase = 'dsl_ams';
6
7// Connect to the database, using the predefined database variables in /assets/repository/mysql.php
8$dbConnection = new mysqli($db_host, $db_username, $db_password, $db_primaryDatabase);
9
10// If there are errors (if the no# of errors is > 1), print out the error and cancel loading the page via exit();
11if (mysqli_connect_errno()) {
12 printf("Could not connect to MySQL databse: %sn", mysqli_connect_error());
13 exit();
14}
15
16$queryCreateUsersTable = "CREATE TABLE IF NOT EXISTS `USERS` (
17 `ID` int(11) unsigned NOT NULL auto_increment,
18 `EMAIL` varchar(255) NOT NULL default '',
19 `PASSWORD` varchar(255) NOT NULL default '',
20 `PERMISSION_LEVEL` tinyint(1) unsigned NOT NULL default '1',
21 `APPLICATION_COMPLETED` boolean NOT NULL default '0',
22 `APPLICATION_IN_PROGRESS` boolean NOT NULL default '0',
23 PRIMARY KEY (`ID`)
24)";
25
26if(!$dbConnection->query($queryCreateUsersTable)){
27 echo "Table creation failed: (" . $dbConnection->errno . ") " . $dbConnection->error;
28}
29
30$query = "SELECT ID FROM USERS";
31$result = mysqli_query($dbConnection, $query);
32
33if(empty($result)) {
34 $query = "CREATE TABLE USERS (
35 ID int(11) AUTO_INCREMENT,
36 EMAIL varchar(255) NOT NULL,
37 PASSWORD varchar(255) NOT NULL,
38 PERMISSION_LEVEL int,
39 APPLICATION_COMPLETED int,
40 APPLICATION_IN_PROGRESS int,
41 PRIMARY KEY (ID)
42 )";
43 $result = mysqli_query($dbConnection, $query);
44}
45
46$query = "INSERT INTO USERS (EMAIL, PASSWORD, PERMISSION_LEVEL, APPLICATION_COMPLETED, APPLICATION_IN_PROGRESS) VALUES ('foobar@foobar.com', 'fjsdfbsjkbgs', 0, 0, 0)";
47
48$querycheck='SELECT 1 FROM `USERS`';
49
50$query_result=$dbConnection->query($querycheck);
51
52if ($query_result !== FALSE)
53{
54 // table exists
55} else
56{
57// table does not exist, create here.
58}
59
60if(!$dbConnection->query($queryCreateUsersTable)){
61 if($dbConnection->errno != 1050){
62 echo "Table creation failed: (" . $dbConnection->errno . ") " . $dbConnection->error;
63 }
64}