-
Notifications
You must be signed in to change notification settings - Fork 0
/
init_switch_db.php
135 lines (97 loc) · 3.69 KB
/
init_switch_db.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
<?php
// This script is used to initialize the database for the switches
//however, it appears we may need to be able to use objects in a second here, which is annoying
//as we did not create objects yet. But I guess I will have to.
include 'switch.php';
//echo "Create some switches<br>";
//we need to create an array of switches however, we only have 5 switches
//I want to pull the switches from mySwitche.json into an array
$myfile = fopen("mySwitchItems.json", "r") or die("Unable to open file!");
//now we need to read the file
$mySwitchItems = fread($myfile,filesize("mySwitchItems.json"));
//now we need to close the file
fclose($myfile);
//now we need to decode the json
$mySwitchItems = json_decode($mySwitchItems, true);
$n=5;
$a0=array();
for ($i=0;$i<$n;$i++) {
$a0[$i]=new SwitchItem();
}
//now we create our database
$servername = "localhost"; //default server name
$username = "AdminLab12"; //user name that you created
$password = "73xp7vQHWFzi6Xta"; //password that you created
$dbname = "myDBSwitches";
// // Create connection
// $conn = new mysqli($servername, $username, $password);
// // Check connection
// if ($conn->connect_error) {
// die("Connection failed: " . $conn->connect_error ."<br>");
// }
// echo "Connected successfully <br>";
// // Creation of the database
// $sql = "CREATE DATABASE ". $dbname;
// if ($conn -> query($sql) === TRUE) {
// echo "Database ". $dbname ." created successfully<br>";
// } else {
// echo "Error creating database: " . $conn->error ."<br>";
// }
// // close the connection
// $conn->close();
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// // we will now create a table with rows that corespond to the data in mySwitchItems.json
// $sql = "CREATE TABLE Switches (
// pkey INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
// weight_ INT(6) NOT NULL,
// brand_ VARCHAR(30) NOT NULL,
// name_ VARCHAR(30) NOT NULL,
// price_ INT(6) NOT NULL,
// silent_ TINYINT(1) NOT NULL,
// type_ VARCHAR(30) NOT NULL,
// img_ VARCHAR(200) NOT NULL,
// link_ VARCHAR(200) NOT NULL
// )";
// if ($conn->query($sql) === TRUE) {
// echo "Table Switches created successfully<br>";
// } else {
// echo "Error creating table: " . $conn->error ."<br>";
// }
// we will now insert the data from mySwitchItems.json into the table
$stmt = $conn->prepare("INSERT INTO Switches
(weight_, brand_, name_, price_, silent_, type_, img_, link_) VALUES (?, ?, ?, ?, ?, ?, ?, ?)");
if ($stmt === FALSE) {
echo "there is a problem with prepare<br>";
echo $conn->error;
}
$stmt->bind_param("ississss", $weight_, $brand_, $name_, $price_, $silent_, $type_, $img_, $link_);
for ($i=0;$i<$n;$i++) {
$switchItem = new SwitchItem();
$switchItem->SetWeight($mySwitchItems[$i]["weight"]);
$switchItem->SetBrand($mySwitchItems[$i]["brand"]);
$switchItem->SetName($mySwitchItems[$i]["name"]);
$switchItem->SetPrice($mySwitchItems[$i]["price"]);
$switchItem->SetSilent($mySwitchItems[$i]["silent"]);
$switchItem->SetType($mySwitchItems[$i]["type"]);
$switchItem->SetImg($mySwitchItems[$i]["img"]);
$switchItem->SetLink($mySwitchItems[$i]["link"]);
echo $switchItem->Display() . "<br>";
$weight_ = $switchItem->weight;
$brand_ = $switchItem->brand;
$name_ = $switchItem->name;
$price_ = $switchItem->price;
$silent_ = $switchItem->silent;
$type_ = $switchItem->type;
$img_ = $switchItem->img;
$link_ = $switchItem->link;
$stmt->execute();
echo "New record " . $i . " created successfully<br>";
}
$stmt->close();
$conn->close();
?>