如何从PHP中的数据库获取图像URL?

时间:2015-03-16 19:23:18

标签: php

我有问题显示图片url-link到我的showdb.php

我目前的数据库记录:

|的 _id _____ | ____名_____ | _image |
| ____ 1 _____ | __banana ____ |了banana.jpg |
| ____ 2 _____ | __aple ______ | apple.jpg |

当我在record.php中显示时,我想做的是banana.jpg或image下的栏目是www.myhost.com/image/banana.jpg吗?

我现在得到的结果:

{"posts":[
{
    "name":"banana",
    "image":banana.jpg}, // FROM THIS
{
    "name":"apple",
    "image":apple.jpg} // FROM THIS
]}


我想要的是:

{"posts":[
{
    "name":"banana",<br>
    "image":www.myhost.com/image/banana.jpg}, // BECOME THIS
{
    "name":"apple",<br>
    "image":www.myhost.com/image/apple.jpg} // BECOME THIS
]}



这是我目前显示我的数据库记录的代码:

if ($rows) {
$response["success"] = 1;
$response["message"] = "Post Available!";
$response["posts"]   = array();

foreach ($rows as $row) {

$post             = array();
$post["name"]  = $row["name"];
$post["image"]    = $row["image"];


//update our repsonse JSON data
array_push($response["posts"], $post);

}

// echoing JSON response
echo json_encode($response);


} else {
$response["success"] = 0;
$response["message"] = "No Post Available!";
die(json_encode($response));
}

是否可以更改这些代码?
或者如果有任何链接这样做,我很高兴:))

谢谢你的回答:)))*抱歉,如果我的英语不好:)

========================================== ==================

解决:@KhaledBentoumi

添加

'www.yourhost.com/folder/'(dot)$row["image"]; => 'www.yourhost.com'.$row["image"];

但是,它会将/更改为\/'www.yourhost.com\/folder\/file.jpg

添加JSON_UNESCAPED_SLASHES,例如:

$url = 'http://www.example.com/';

echo json_encode($url), "\n";

echo json_encode($url, JSON_UNESCAPED_SLASHES), "\n";

快乐的编码家伙! :))

2 个答案:

答案 0 :(得分:2)

只需将www.myhost.com/image/附加到帖子数组中的图像条目

即可
if ($rows) {
$response["success"] = 1;
$response["message"] = "Post Available!";
$response["posts"]   = array();

foreach ($rows as $row) {

$post             = array();
$post["name"]  = $row["name"];
$post["image"]    = 'www.myhost.com/image/'.$row["image"]; // Append here


//update our repsonse JSON data
array_push($response["posts"], $post);

}

// echoing JSON response
echo json_encode($response);


} else {
$response["success"] = 0;
$response["message"] = "No Post Available!";
die(json_encode($response));
}

答案 1 :(得分:0)

使用简单连接:

$post["image"]    = "www.myhost.com/image/".$row["image"];