我正在从 mysql 获取数据作为 json 编码。除了图像路径之外,一切正常,它不是数据库中的绝对路径 - 它只是一个相对路径,例如 images/starwars.jpg
。我正在开发一个移动应用程序,想要显示图像的绝对路径。
它应该显示为http://sample.com/images/starwars.jpg
。
可以通过更改数据库中的名称路径来完成,但我正在寻找替代解决方案。
获取我正在使用的图像路径的字段是:$row['path']=$row['path'];
编码json对象的php代码是:
<?php
$sql = "SELECT DATE_FORMAT(movie.filmReleaseDate,'%d %b %Y')filmReleaseDate, movie.filmName, movie.filmDirector, movie.url, images.path FROM movie INNER JOIN images ON movie.filmId = images.filmId WHERE movie.filmReleaseDate >= NOW() GROUP by images.filmId ORDER BY DATE(films.filmReleaseDate) LIMIT 0 , 12";
$result = mysqli_query($conn,$sql);
$json = array();
while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC))//loop through the retrieved values
{
$row['filmName']=htmlentities(stripslashes($row['filmName']));
$row['filmDirector']=htmlentities(stripslashes($row['filmDirector']));
$row['filmReleaseDate']=$row['filmReleaseDate'];
$row['url']=$row['url'];
$row['path']=$row['path'];
$json[] = $row;//build an array
}
echo json_encode(array('upcomingFilms' => $json));
?>
最佳答案
尝试在查询请求中使用CONCAT。
http://dev.mysql.com/doc/refman/5.7/en/string-functions.html#function_concat
SELECT ... CONCAT('http://sample.com/images/', images.path) ...
关于php - 从存储的相对路径从mysql php获取图像的绝对路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34530623/