My php code is
$result = mysqli_query($link, "SELECT * FROM album");
if($result)
{
while ($row = mysqli_fetch_array($result))
{
$data['path'] = $row['photo_path'];
}
}
echo json_encode($data);
I want to store the all photos path in $data['path']
What Happening is it taking only last inserted value. I am not getting how can I store the all paths and then echo it.
You need to add array otherwise it will overwrite values
$result = mysqli_query($link, "SELECT * FROM album");
if($result)
{
$data = array();
while ($row = mysqli_fetch_array($result))
{
$data[]['path'] = $row['photo_path'];
}
}
echo json_encode($data);
This will also overwrite previous data. So, you are getting last value.
$data['path'] = $row['photo_path'];
Use, array instead something like
$data[$i]['path'] = $row['photo_path'];
$i++;
If you only need the path you can do:
$result = mysqli_query($link, "SELECT photo_path as path FROM album");
if($result)
{
$data = mysqli_fetch_all($result,MYSQLI_ASSOC);
echo json_encode($data);
}
You want to turn the result into an array:
$array = iterator_to_array($result);
The $array
is an array that contains all result rows (again as array, array inside array).
More verbose example:
$result = $link->query("SELECT photo_path as path FROM album");
if ($result) {
$data = iterator_to_array($result);
} else {
$data = ['error' => $link->error];
}
echo json_encode($data);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.