I was only able to fetch a single ID with a value:
{"id":"2147483647","hyperlink":"0"}
My table:
id hyperlink
2147483647 0
2147483647 1
My read.php:
<?php
// Headers
header('Access-Control-Allow-Origin: *');
header('Content-Type: application/json');
include_once '../../config/Database.php';
include_once '../../models/Post.php';
// Instantiate DB & connect
$database = new Database();
$db = $database->connect();
// Instantiate blog post object
$post = new Post($db);
// Get ID
$post->id = isset($_GET['id']) ? $_GET['id'] : die();
// Get post
$post->read_single();
// Create array
$post_arr = array(
'id' => $post->id,
'hyperlink' => $post->hyperlink
);
// Make JSON
print_r(json_encode($post_arr));
My Post.php
<?php
class Post {
// DB stuff
private $conn;
private $table = 'views';
// Post Properties
public $id;
public $hyperlink;
// Constructor with DB
public function __construct($db) {
$this->conn = $db;
}
// Get Single Post
public function read_single() {
// Create query
$query = "SELECT * FROM views p
LEFT JOIN
views c ON p.id = c.id
WHERE
p.id = ?";
// Prepare statement
$stmt = $this->conn->prepare($query);
// Bind ID
$stmt->bindParam(1, $this->id);
// Execute query
$stmt->execute();
$row = $stmt->fetch(PDO::FETCH_ASSOC);
// Set properties
$this->id = $row['id'];
$this->hyperlink = $row['hyperlink'];
}
}
?>
I added comments to explain what the code does, My goal is whenever I send a GET request to this url /read.php?id=2147483647 I get a response back in JSON with the data:
[{"id":"2147483647","hyperlink":"0"}, {"id":"2147483647","hyperlink":"1"}]
And if the same id had more than 2 different values it will be able to fetch all of it