-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathweather_db.php
executable file
·152 lines (140 loc) · 3.37 KB
/
weather_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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
<?php
function db_init()
{
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "WeatherDB";
// Create connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Create database
$sql = "CREATE DATABASE ".$dbname;
$result = $conn->query($sql);
/*
if ($result === TRUE) {
echo "<br>Database created successfully";
} else {
echo "<br>Error creating database: " . $conn->error;
}
*/
$conn->close();
}
function get_conn()
{
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "mymymyDB";
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
return $conn;
}
function db_create($conn)
{
$sql = "CREATE TABLE CityTable (
city VARCHAR(50) NOT NULL
)";
$result = $conn->query($sql);
/*
if ($result === TRUE) {
echo "<br>Table CityTable created successfully";
} else {
echo "<br>Error creating table: " . $conn->error;
}
*/
}
function db_insert($conn, $input_city)
{
$sql = "SELECT * FROM CityTable";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
if ($input_city == $row["city"]) {
//echo "<br>Duplicate city found<br>";
return;
}
}
}
$sql = "INSERT INTO CityTable (city) VALUES ('$input_city')";
$result = $conn->query($sql);
/*
if ($result === TRUE) {
echo "<br>New record created successfully";
} else {
echo "<br>Error: " . $sql . $conn->error;
}
*/
}
function db_update($conn)
{
$sql = "UPDATE CityTable SET lastname='Dmuyyy' WHERE id=11";
$result = $conn->query($sql);
/*
if ($result === TRUE) {
echo "<br>Record updated successfully";
} else {
echo "<br>Error updating record: " . $conn->error;
}
*/
}
function db_delete($conn, $city)
{
$sql = "DELETE FROM CityTable WHERE city='$city'";
$result = $conn->query($sql);
/*
if ($result === TRUE) {
echo "<br>Record deleted successfully";
} else {
echo "<br>Error deleting record: " . $conn->error;
}
*/
}
function db_delete_all($conn)
{
$sql = "DELETE FROM CityTable";
$result = $conn->query($sql);
/*
if ($result === TRUE) {
echo "<br>All Records deleted successfully";
} else {
echo "<br>Error deleting record: " . $conn->error;
}
*/
}
function db_print($conn)
{
$sql = "SELECT * FROM CityTable";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo "<br>City: " . $row["city"];
}
} else {
echo "<br>0 results";
}
}
function db_close($conn) {
$conn->close();
}
/*
db_init();
$conn = get_conn();
db_create($conn);
db_insert($conn, "Chennai");
db_print($conn);
//db_update($conn);
//db_print($conn);
db_delete($conn, "Chennai");
db_print($conn);
db_delete_all($conn);
db_print($conn);
$conn->close();
*/
?>