How to retrieve an images from PHp myadim database and display in android list view?

189

How to retrieve an images from PHP my admin database and display in android list view? I have the following script to retrieve the name and id. But i don't know how to retrieve the image as well. How do i will retrieve image by using the same query?

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

?>
842

Answer

Solution:

Most likely, each $row array will have extra fields. One of these might be a URL or path to the images; you can use

print_r($row)

or

var_dump($row)

in the block below

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

to see what other data is held within those arrays.

Finally, you should include that data in your $response array.

916

Answer

Solution:

I am supposing you have following columns in your table

1) ID 2) Name 3) Image

If images are stored in Database then type ofimage field would beblob

Now here is the code to fetch the image from database

<?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());

?>

The image is fetched from database and is in$result, now it is displayed in Html by setting content type as follow

<?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';
 }

?>

You can set the result variable to post varialble to bind in your json array to use it further

643

Answer

Solution:

Will that work, I have edited this after your comment

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

?>

People are also looking for solutions to the problem: php - How to set push notification receive message target page in windows phone?

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.