简体   繁体   English

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

[英]How to retrieve image file paths from mysql database and display the images using php

I'm trying to retrieve an image file path from mysql database using php and display it to a form that will then be used to send the images to our android app. 我正在尝试使用php从mysql数据库检索图像文件路径,并将其显示为一种表单,该表单随后将用于将图像发送到我们的android应用程序。 I have search around everywhere and i found one that looks like my problem but it has not been solved so I'm trying to find another help. 我到处都有搜索,发现一个看起来像我的问题,但尚未解决,因此我试图寻找另一种帮助。

here is my php form for uploading images: inSTAGram.php 这是我上传图片的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>

this one works just right! 这个工作正好!

so here is my php form for the displaying of image; 所以这是我的用于显示图像的php表单; inSTAGramDisplay.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));
}

?>

this time, this one shows the image path and not the image, what i wanted to do is display the images itself. 这次,这只显示图像路径而不是图像,我想做的是显示图像本身。 I just don't have any idea what the right thing to do here. 我只是不知道该怎么做。 any help will do. 任何帮助都可以。 Thanks in advance! 提前致谢!

Just echo an image tag and put the image variable as it source attribute: 只需回显图像标签,然后将图像变量作为源属性即可:

<?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']."' />";
}

?>

Without testing it, I'd say that one of your issues may be that you need to enclose the location of the image: 如果不进行测试,我会说您的问题之一可能是您需要封闭图像的位置:

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

Give that a go. 放手

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM