如何从MySQL数据库映像文件路径和使用PHP显示图像 [英] How to retrieve image file paths from mysql database and display the images using php

查看:146
本文介绍了如何从MySQL数据库映像文件路径和使用PHP显示图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用PHP来获取MySQL数据库映像文件的路径,并显示给将被用来将图像发送到我们​​的Andr​​oid应用程序的形式。我已搜索周围到处都是,我发现了一个看起来像我的问题,但一直没有得到解决,所以我试图找到另外的帮助。

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. 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.

下面是我的PHP形式上传图片: inSTAGram.php

here is my php form for uploading images: 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

so here is my php form for the displaying of image; 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']."' />";
}

?>

这篇关于如何从MySQL数据库映像文件路径和使用PHP显示图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆