Reputation: 11
json return this, images are equals
How to return image data from MySQL? This code returns always first image.When I fetching only one value, everything is ok.
Please help.
require_once 'connection.php';
mysqli_query ($conn,"SET NAMES UTF8");
$sql = "SELECT * FROM posts WHERE authorId = '$id' GROUP BY date DESC ";
$response = mysqli_query($conn, $sql);
$result['posts'] = array();
if ( mysqli_num_rows($response) > 0 ) {
while ($row = mysqli_fetch_array($response)){
$index['id'] = $row['id'];
$index['authorId'] = $row['authorId'];
$index['date'] = $row['date'];
$index['time'] = $row['time'];
$index['description'] = strval($row['description']);
$index['picture'] = base64_encode($row['picture']);
#echo '<img src="data:image/jpeg;base64,'.base64_encode( $row['picture'] ).'"/>';
array_push($result['posts'], $index);
$index['picture'] = null;
}
$result['success'] = "1";
$result['message'] = "success";
echo json_encode($result, JSON_UNESCAPED_UNICODE);
mysqli_close($conn);
} else {
$result['success'] = "0";
$result['message'] = "error";
echo json_encode($result, JSON_UNESCAPED_UNICODE);
mysqli_close($conn);
}
Upvotes: 1
Views: 48
Reputation: 403
you are not resetting $index variable on each row. just add $index=array(); as below.
if ( mysqli_num_rows($response) > 0 ) {
while ($row = mysqli_fetch_array($response)){
$index = array(); // this one.
}
}
Upvotes: 0
Reputation: 490
Set $index to an empty array at the start of the while loop, this should clear any existing values from the previous iteration
Upvotes: 1