如何从PHp myadim数据库中检索图像并在android列表视图中显示?
如何从PHP my admin数据库中检索图像并在android列表视图中显示? 我有下面的脚本来检索名称和id。但是我也不知道如何检索图像。 如何使用相同的查询检索图像如何从PHp myadim数据库中检索图像并在android列表视图中显示?,php,android,mysql,Php,Android,Mysql,如何从PHP my admin数据库中检索图像并在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 use
<?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));
}
?>
这行得通吗,我在你的评论后编辑了这篇文章
<?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));
}
?>
很可能,每个$row数组都会有额外的字段。其中之一可能是图像的URL或路径;你可以用
print_r($row)
或
var\u dump($row)
在下面的街区
foreach ($rows as $row) { ... }
查看这些数组中还有哪些其他数据
最后,您应该将该数据包括在$response数组中。我假设您的表中有以下列
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());
?>
?>
您可以将结果变量设置为post varialble以绑定到json数组中以进一步使用它图像字段的名称为“contents”,因此我更改了脚本,但它不起作用。我希望您编辑我的脚本以读取json格式的所有数据在json_encode()
之后,我们需要放置我添加的exit()
,请立即尝试。您可以使用给定的详细信息编辑我的脚本吗?因为我是新手,所以我无法正确执行此操作。谢谢,我的图像区域的名称是“内容”,所以我更改了脚本,但它不起作用。我想请你编辑我的脚本,以读取json格式的所有数据。我的图像字段名为“contents”,因此我更改了脚本,但它不起作用。我希望您编辑我的脚本以读取json格式的所有数据–您可以添加特定问题吗?是的,我只需要从服务器获取图像并在android的列表视图中显示。您可以将以下内容添加到问题、数据库结构、安卓代码中吗?
<?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';
}