如何从mysql数据库中检索图像文件路径并使用php显示图像

时间:2014-02-04 12:59:19

标签: php android mysql image

我正在尝试使用php从mysql数据库中检索图像文件路径并将其显示为一个表单,然后将其用于将图像发送到我们的Android应用程序。我到处搜索,我发现一个看起来像我的问题,但它还没有解决,所以我想找到另一个帮助。

这是我用于上传图片的php表单: inSTAGram.php

<?php

require('admin.config.inc.php');

if(isset($_POST['upload'])){
$image_name = $_FILES['image']['name'];
$image_type = $_FILES['image']['type'];
$image_size = $_FILES['image']['size'];
$image_tmp_name = $_FILES['image']['tmp_name'];

$path = "/home/stagcon2/public_html/StagConnect/admin/pictures/$image_name";

if($image_name==''){
echo "Don't just click! select an image please .";
exit();
}
else{
move_uploaded_file($image_tmp_name, $path);
$mysql_path = $path."/".$image_name;
$query = "INSERT INTO `inSTAGram`(`image_name`,`path`) VALUES ('$image_name','$mysql_path')";

$query_params = array(
    ':image_name' => $image_name,
    ':mysql_path' => $path,
    );

//execute query
try {
    $stmt   = $db->prepare($query);
    $result = $stmt->execute($query_params);
}
catch (PDOException $ex) {
    // For testing, you could use a die and message. 
    //die("Failed to run query: " . $ex->getMessage());

    //or just use this use this one:
    $response["success"] = 0;
    $response["message"] = "Database Error. Couldn't Upload Image!";
    die(json_encode($response));
}

$response["success"] = 1;
$response["message"] = "Image Uploaded Succesfully!";
echo json_encode($response);
}
}   
?>

<form action="inSTAGram.php" method="post" enctype="multipart/form-data">
<input type="file" name="image" >
<input type="submit" name="upload" value="Upload" >
</form>

这个工作得恰到好处!

所以这是我用于显示图像的php表单; inSTAGramDisplay.php

<?php
require("admin.config.inc.php");

//initial query
$query = "Select * FROM inSTAGram";

try {
$stmt   = $db->prepare($query);
$result = $stmt->execute($query_params);
}
catch (PDOException $ex) {
$response["success"] = 0;
$response["message"] = "Database Error!";
die(json_encode($response));
}

// Finally, we can retrieve all of the found rows into an array using fetchAll 
$rows = $stmt->fetchAll();


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

foreach ($rows as $row) {
    $rows             = array();
    $rows['image'] = 'http://www.stagconnect.com/StagConnect/admin/pictures?image_id=' . $rows['image_id'];
    $rows[] = $rows;        

    //update our repsonse JSON data
    array_push($response["images"], $image);
}

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


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

?>

这一次,这个显示图像路径而不是图像,我想要做的是显示图像本身。我只是不知道在这里做什么是正确的。任何帮助都可以。提前谢谢!

2 个答案:

答案 0 :(得分:1)

只需回显图像标记并将图像变量作为源属性:

<?php
//I hard coded the array but pull the images id from the db
$rows  = array();
$rows = array("070415togepi.jpg",
              "1384904_681730675185076_523601772_n.jpg",
              "1388517_681730668518410_508160046_n.jpg",
              "1394889_681730678518409_1155435015_n.jpg", 
              "385-jirachi-g.jpg"); 

foreach ($rows as $value){
 $rows['img'] = 'http://www.stagconnect.com/StagConnect/admin/pictures/'.$value;
 echo "<img src='".$rows['img']."' />";
}

?>

答案 1 :(得分:0)

如果不进行测试,我会说您的一个问题可能是您需要附上图片的位置:

$rows['image'] = 'url(http://www.stagconnect.com/StagConnect/admin/pictures?image_id=' . $rows['image_id'] . ')';

放手一搏。