Skip to content

Commit 1f9fb00

Browse files
committed
.
1 parent 6b65963 commit 1f9fb00

File tree

4 files changed

+138
-0
lines changed

4 files changed

+138
-0
lines changed

db.php

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
<?php
2+
3+
4+
5+
6+
$dbservername = "localhost";
7+
$dbusername = "todo_list";
8+
$dbpassword = "01todo_list";
9+
$dbdatabase = "todo_list";
10+
// $dbtable = "tasks";
11+
// $dbtablefields = "`id` INT DEFAULT NULL AUTO_INCREMENT,
12+
// `title` VARCHAR(255) DEFAULT NULL,
13+
// `description` TEXT DEFAULT NULL,
14+
// PRIMARY KEY(id)";
15+
16+
17+
18+
function sql_init(string $dbservername, string $dbusername, string $dbpassword, string $dbdatabase): mysqli
19+
{
20+
21+
$conn = new mysqli($dbservername, $dbusername, $dbpassword, $dbdatabase);
22+
23+
if ($conn->error) die("Database connection error: ".$conn->error);
24+
25+
return $conn;
26+
27+
}
28+
29+
30+
31+
function giveme(mysqli $conn, string $table, string $column, string $value): array
32+
{
33+
$sql = "SELECT * FROM `$table` WHERE $column = ?";
34+
35+
$stmt = $conn->prepare($sql);
36+
37+
if (!$stmt) return ["Database error!"];
38+
39+
$stmt->bind_param('s', $value);
40+
41+
$stmt->execute();
42+
43+
$response = $stmt->get_result();
44+
45+
$stmt->close();
46+
47+
$result = [];
48+
if ($response->num_rows > 0){
49+
while ($row = $response->fetch_assoc()){
50+
$result[] = ["word" => $row["word"], "translate" => $row["translate"]];
51+
}
52+
}
53+
54+
55+
return $result;
56+
57+
}
58+
59+
60+
61+
function giveall(mysqli $conn, string $table): array
62+
{
63+
$sql = "SELECT * FROM `$table`";
64+
65+
$stmt = $conn->prepare($sql);
66+
67+
if (!$stmt) return ["Database error!"];
68+
69+
$stmt->execute();
70+
71+
$response = $stmt->get_result();
72+
73+
$stmt->close();
74+
75+
$result = [];
76+
if ($response->num_rows > 0){
77+
while ($row = $response->fetch_assoc()){
78+
$result[] = ["word" => $row["word"], "translate" => $row["translate"]];
79+
}
80+
}
81+
82+
83+
return $result;
84+
}

dict.json

Lines changed: 1 addition & 0 deletions
Large diffs are not rendered by default.

dict.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
header("Content-type: application/json");
4+
5+
6+
require("db.php");
7+
8+
9+
10+
11+
$conn = sql_init($dbservername, $dbusername, $dbpassword, $dbdatabase);
12+
13+
$result = giveall($conn, "dict");
14+
15+
16+
if (isset($_GET["word"])){
17+
18+
foreach ($result as $item){
19+
20+
if ($item["word"] == $_GET["word"]) print_r(json_encode($item));
21+
22+
}
23+
} else {print_r(json_encode($result)); }
24+
25+
26+
27+
// $result = json_encode(giveall($conn, "dict"));
28+
// print_r($result);
29+
$conn->close();
30+

main.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
header("Content-type: application/json");
4+
5+
6+
require("db.php");
7+
8+
9+
10+
11+
$conn = sql_init($dbservername, $dbusername, $dbpassword, $dbdatabase);
12+
13+
14+
// $result = giveme($conn, "dict", "word", "text");
15+
// print_r($result);
16+
17+
18+
$result = json_encode(giveall($conn, "dict"));
19+
20+
21+
print_r($result);
22+
$conn->close();
23+

0 commit comments

Comments
 (0)