-1

I have a code that fetches images from database and displays them. However, it only displays the first image. I need to modify it to display multiple images.

Here is the code snippet:

   if(isset($_GET['submit-id']))
  {
    if(isset($_GET['image-name']))
    {
        $query="SELECT `image` FROM `temp_image` where `name` like '%Chi%'";

        if($run_query=mysql_query($query))
        {
            if(mysql_num_rows($run_query)>0)
            {
            while($row=mysql_fetch_assoc($run_query))
            {
                header("Content-type: image/jpeg");
                $image=$row['image'];
                echo  $image;
            }
            }
            else
                        echo "Incorrect id";

        }
    }
}
  • 1
    [Little Bobby](http://bobby-tables.com/) says ***[your script is at risk for SQL Injection Attacks.](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php)***. Even [escaping the string](http://stackoverflow.com/questions/5741187/sql-injection-that-gets-around-mysql-real-escape-string) is not safe! [Don't believe it?](http://stackoverflow.com/q/38297105/1011527) – Jay Blanchard Jul 18 '16 at 16:17
  • 1
    ***Please [stop using `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php).*** [These extensions](http://php.net/manual/en/migration70.removed-exts-sapis.php) have been removed in PHP 7. Learn about [prepared](http://en.wikipedia.org/wiki/Prepared_statement) statements for [PDO](http://php.net/manual/en/pdo.prepared-statements.php) and [MySQLi](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) and consider using PDO, [it's really pretty easy](http://jayblanchard.net/demystifying_php_pdo.html). – Jay Blanchard Jul 18 '16 at 16:17
  • Maybe you have only one image with `\`name\` like '%Chi%'`? (As per your query) – FirstOne Jul 18 '16 at 16:17
  • @FirstOne I have multiple images. I checked that using mysql_num rows query, It returns 4 results – Chirag Makhija Jul 18 '16 at 16:42
  • Maybe you'd get `headers already sent`, then. Try, for now, commenting out this line: `header("Content-type: image/jpeg");` – FirstOne Jul 18 '16 at 16:45
  • To find out if it's a data problem, run the test on another column which just has text. If all the records echo correctly, then the problem is in your image storage. For images I usually encode/decode them. – john elemans Jul 18 '16 at 16:49
  • @FirstOne The same query returns 4 results in localhost.Commenting the header returns binary data.. Also I am not getting headers already sent. That is suspicious. – Chirag Makhija Jul 18 '16 at 17:21
  • Check if this is somehow helpful: [php: recreate and display an image from binary data](http://stackoverflow.com/questions/2070603/php-recreate-and-display-an-image-from-binary-data) (or something related to that idea) – FirstOne Jul 18 '16 at 17:35

1 Answers1

0

There are many ways to do this. I'll mention two of them.

  1. Use your code to fetch only one image (ex. A.php) and use it for accessing another image from another page. (Using img tag with A.php as src)

  2. Using ImageMagick to convert blob to images directly. Looking from your code, i think you saved image to database with blob formats. Please look for its documentation on PHP.net documentation.

Please let me know if there's other way to do that. Thank you.

Ruswan
  • 3
  • 1
  • 5