In my table medical units, I have a field pic. I created a rest API, but I don't know how to convert blob to JSON and send it to my react front end. I tried to bind the param in read function but it doesn't work.
php read method:
public function read() {
// Create query
$query = 'SELECT * FROM ' . $this->table ;
// Prepare statement
$stmt = $this->conn->prepare($query);
$stmt->bindParam(':pic', $this->pic, PDO::PARAM_LOB);
// Execute query
$stmt->execute();
return $stmt;
}
php api:
<?php
// Headers
header('Access-Control-Allow-Origin: *');
header('Content-Type: application/json');
header('Content-Type: images/jpeg');
include_once '../../config/Database.php';
include_once '../../models/Post2.php';
// Instantiate DB & connect
$database = new Database();
$db = $database->connect();
// Instantiate post object
$post = new Post2($db);
// post query
$result = $post->read();
// Get row count
$num = $result->rowCount();
// Check if any posts
if ($num > 0) {
// Post array
// $posts_arr = array();
$posts_arr['data'] = array();
while ($row = $result->fetch(PDO::FETCH_ASSOC)) {
extract($row);
$post_item = array(
'id' => $id,
'pic' => $pic,
'name' => $name,
'address' => $address,
'type' => $type,
);
// Push to "data"
//array_push($posts_arr, $post_item);
array_push($posts_arr['data'], $post_item);
}
// Turn to JSON & output
echo json_encode($posts_arr);
} else {
// No Posts
echo json_encode(
array('message' => 'No Posts Found')
);
}