如何从我的管理数据库中检索PHP图像并在android列表视图中显示? 我有以下脚本来检索名称和ID。但我不知道如何检索图像。 如何使用相同的查询检索图像?
<?php
/*
Our "config.inc.php" file connects to database every time we include or require
it within a php script. Since we want this script to add a new user to our db,
we will be talking with our database, and therefore,
let's require the connection to happen:
*/
require("config.php");
//initial query
$query = "Select * FROM channels";
//execute query
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"] = "Post Available!";
$response["posts"] = array();
foreach ($rows as $row) {
$post = array();
$post["channelname"] = $row["channelname"];
$post["channelid"] = $row["channelid"];
//update our repsonse JSON data
array_push($response["posts"], $post);
}
// echoing JSON response
echo json_encode($response);
} else {
$response["success"] = 0;
$response["message"] = "No Channel Available!";
die(json_encode($response));
}
?>
答案 0 :(得分:0)
这是否有效,我在你的评论之后编辑了这个
<?php
/*
Our "config.inc.php" file connects to database every time we include or require
it within a php script. Since we want this script to add a new user to our db,
we will be talking with our database, and therefore,
let's require the connection to happen:
*/
require("config.php");
//initial query
$query = "Select * FROM channels";
//execute query
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"] = "Post Available!";
$response["posts"] = array();
foreach ($rows as $row) {
$post = array();
$post["channelname"] = $row["channelname"];
$post["channelid"] = $row["channelid"];
$post["image"] = $row["content"];
//update our repsonse JSON data
array_push($response["posts"], $post);
}
// echoing JSON response
echo json_encode($response);
} else {
$response["success"] = 0;
$response["message"] = "No Channel Available!";
die(json_encode($response));
}
?>
答案 1 :(得分:0)
最有可能的是,每个$ row数组都有额外的字段。其中一个可能是图像的URL或路径;你可以用
print_r($row)
或
var_dump($row)
在下面的块中
foreach ($rows as $row) { ... }
查看这些数组中包含的其他数据。
最后,您应该将这些数据包含在$ response数组中。
答案 2 :(得分:0)
我假设你的表中有以下列
1)身份证 2)姓名 3)图像
如果图像存储在数据库中,则image
字段的类型将为blob
现在这里是从数据库中获取图像的代码
<?php
$host="your_hostname";
$user="your_databaseuser";
$pass="your_database_password";
$db="database_name_to_use";
// just so we know it is broken
error_reporting(E_ALL);
// some basic sanity checks
if(isset($_GET['id']) && is_numeric($_GET['id'])) {
//connect to the db
$link = mysql_connect("$host", "$user", "$pass")
or die("Could not connect: " . mysql_error());
// select our database
mysql_select_db("$db") or die(mysql_error());
// get the image from the db
$sql = "SELECT image FROM test_image WHERE id=" .$_GET['id'] . ";";
// the result of the query
$result = mysql_query("$sql") or die("Invalid query: " . mysql_error());
?>
图像是从数据库中获取的,位于$result
,现在通过设置内容类型显示在Html中
<?php
// set the header for the image
header("Content-type: image/jpeg");
echo mysql_result($result, 0);
// close the db link
mysql_close($link);
}
else {
echo 'Please use a real id number';
}
&GT;
您可以将结果变量设置为在json数组中发布varialble以进一步使用