0

如何从 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));
}

?>
4

3 回答 3

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));
}

?>
于 2013-09-04T07:29:11.897 回答
0

最有可能的是,每个 $row 数组都会有额外的字段。其中之一可能是图像的 URL 或路径;您可以使用

print_r($row)

或者

var_dump($row)

在下面的块中

foreach ($rows as $row) { ... }

查看这些数组中还保存了哪些其他数据。

最后,您应该将该数据包含在 $response 数组中。

于 2013-09-04T07:30:23.870 回答
0

我假设您的表格中有以下列

1) ID 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';
 }

?>

您可以将结果变量设置为发布变量以绑定到您的 json 数组中以进一步使用它

于 2013-09-04T07:34:55.450 回答