【问题标题】:Getting absolute path of image from mysql php from relative path which is stored从存储的相对路径从mysql php获取图像的绝对路径
【发布时间】:2015-12-30 23:15:09
【问题描述】:

我从 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));

?>

【问题讨论】:

    标签: php mysql json


    【解决方案1】:

    尝试在查询请求中使用 CONCAT。
    http://dev.mysql.com/doc/refman/5.7/en/string-functions.html#function_concat

    SELECT ... CONCAT('http://sample.com/images/', images.path) ...
    

    【讨论】:

    • 很好的答案...非常感谢 "CONCAT('sample.com/images', images.path) AS path" 完美运行
    猜你喜欢
    • 2011-02-07
    • 1970-01-01
    • 2022-06-10
    • 2012-10-25
    • 1970-01-01
    • 2011-08-11
    • 2010-09-21
    • 2023-03-22
    相关资源
    最近更新 更多