-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.php
More file actions
71 lines (57 loc) · 2.11 KB
/
script.php
File metadata and controls
71 lines (57 loc) · 2.11 KB
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
<?php
// Veritabanı bilgileri
$dbHost = "127.0.0.1";
$dbUser = "root";
$dbPass = "";
$dbName = "slimapi";
$mysqli = new mysqli($dbHost, $dbUser, $dbPass, $dbName);
// Bağlantı hata kontrolü
if ($mysqli->connect_error) {
die("Veritabanı bağlantı hatası: " . $mysqli->connect_error);
}
// Database.sql dosyasını çalıştır
$databaseScript = file_get_contents("database.sql");
if ($mysqli->multi_query($databaseScript) === TRUE) {
do {
// Sonuç setini al
if ($result = $mysqli->store_result()) {
$result->free();
}
} while ($mysqli->more_results() && $mysqli->next_result());
echo "Database.sql dosyası başarıyla çalıştırıldı.<br>";
} else {
echo "Hata: " . $mysqli->error;
}
// Posts tablosuna verileri ekle
$json_data = file_get_contents("https://jsonplaceholder.typicode.com/posts");
$data = json_decode($json_data, true);
foreach ($data as $item) {
$userId = $item['userId'];
$id = $item['id'];
$title = $mysqli->real_escape_string($item['title']);
$body = $mysqli->real_escape_string($item['body']);
$query = "INSERT INTO posts (userId, id, title, body) VALUES ('$userId', '$id', '$title', '$body')";
if ($mysqli->query($query) === TRUE) {
echo "Veri başarıyla eklendi.<br>";
} else {
echo "Hata: " . $query . "<br>" . $mysqli->error . "<br>";
}
}
// Comments tablosuna verileri ekle
$json_data = file_get_contents("https://jsonplaceholder.typicode.com/comments");
$data = json_decode($json_data, true);
foreach ($data as $item) {
$postId = $item['postId'];
$id = $item['id'];
$name = $mysqli->real_escape_string($item['name']);
$email = $mysqli->real_escape_string($item['email']);
$body = $mysqli->real_escape_string($item['body']);
$query = "INSERT INTO comments (postId, id, name, email, body) VALUES ('$postId', '$id', '$name', '$email', '$body')";
if ($mysqli->query($query) === TRUE) {
echo "Veri başarıyla eklendi.<br>";
} else {
echo "Hata: " . $query . "<br>" . $mysqli->error . "<br>";
}
}
// Veritabanı bağlantısını kapat
$mysqli->close();