-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfetch_story.php
More file actions
42 lines (35 loc) · 1.14 KB
/
fetch_story.php
File metadata and controls
42 lines (35 loc) · 1.14 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
<?php
// Database connection
$dsn = 'mysql:host=joecool.highpoint.edu;dbname=CSC3212_S24_lhardister_db';
$username = 'lhardister';
$password = '1862270';
try {
$db = new PDO($dsn, $username, $password);
} catch (PDOException $e) {
$error_message = $e->getMessage();
// Handle connection error
echo json_encode(array('error' => 'Failed to connect to the database.'));
exit();
}
// Check if index parameter is provided
if (isset($_GET['index'])) {
// Sanitize the index parameter
$index = filter_input(INPUT_GET, 'index', FILTER_VALIDATE_INT);
// Query to fetch story data by index
$query = "SELECT * FROM story LIMIT :index, 1";
$statement = $db->prepare($query);
$statement->bindValue(':index', $index, PDO::PARAM_INT);
$statement->execute();
// Fetch the row
$row = $statement->fetch(PDO::FETCH_ASSOC);
// Close statement
$statement->closeCursor();
// Close database connection
$db = null;
// Output the fetched row as JSON
echo json_encode($row);
} else {
// Output error if index parameter is missing
echo json_encode(array('error' => 'Index parameter is missing.'));
}
?>