4 Replies - 2286 Views - Last Post: 07 April 2014 - 10:28 AM Rate Topic: -----

#1 divyanisha   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 06-March 14

image does not get retrieved from database into html form using php

Posted 07 April 2014 - 03:31 AM

Getimage.php

<?php
$hostname="localhost";
$username="root";
$password="tiger";

/* @var $dbhandle type */
 $dbhandle = \mysqli_connect($hostname, $username, $password) 
 or die("Unable to connect to MySQL");

/* @var $select type */
$select= \mysqli_select_db($dbhandle,"sample")
     or mysqli_error($dbhandle);
 /* @var $itemId type */
$itemId= (\filter_input(\INPUT_GET,'name'));
$sql="select img from starterveg where itemId=$itemId";
$res2=mysqli_query($dbhandle,$sql);
$row= mysqli_fetch_assoc($res2);
mysqli_close($dbhandle);
header("Content-type: image/jpeg");
echo $row['img'];
?>

<body>
 <img src="getimage.php?itemId=<?php echo $row['img'];?> " alt="image" id="img1">
</body>


I'm not able to display the image from database into the html for.Instead the alt message only appears inside the html form

This post has been edited by Dormilich: 07 April 2014 - 03:36 AM
Reason for edit:: please use [code] [/code] tags when posting code


Is This A Good Question/Topic? 0
  • +

Replies To: image does not get retrieved from database into html form using php

#2 ge∅   User is offline

  • D.I.C Lover

Reputation: 319
  • View blog
  • Posts: 1,335
  • Joined: 21-November 13

Re: image does not get retrieved from database into html form using php

Posted 07 April 2014 - 03:42 AM

<img src="getimage.php?itemId=<?php echo $row['img'];?> " alt="image" id="img1">


Why don't you include "getimage.php?itemId=$row['img']" directly in the HTML while you are at it ?

First of all, you should watch the HTML output and check your <?php echo $row['img'];?> prints what you expect it to. The error can happen in in your main PHP file as well as in your getimage.php file

This post has been edited by ge∅: 07 April 2014 - 03:44 AM

Was This Post Helpful? 0
  • +
  • -

#3 Dormilich   User is offline

  • 痛覚残留
  • member icon

Reputation: 4288
  • View blog
  • Posts: 13,592
  • Joined: 08-June 10

Re: image does not get retrieved from database into html form using php

Posted 07 April 2014 - 03:45 AM

not sure if the posted snippets are in one file. if so, you invalidate your image data by appending HTML code. get image.php must only return the binary image data.

further, what data type is in the img field? either it is a name, then you assign it to the image URL (but not the image output), or it is binary data then it belongs only in the image output.

note, Prepared Statements protect you from SQL Injection.

note, mysqli doesn’t need to select the db, just create the mysqli instance in one go:
$dbhandle = \mysqli_connect($hostname, $username, $password, "sample);


note, unlike mysql_connect(), mysqli_connect() never returns false. you have to explicitly test against $dbhandle->connect_error to make sure.
Was This Post Helpful? 0
  • +
  • -

#4 divyanisha   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 06-March 14

Re: image does not get retrieved from database into html form using php

Posted 07 April 2014 - 03:55 AM

how can i rectify my code to get the images displayed from the database?
Was This Post Helpful? 0
  • +
  • -

#5 ArtificialSoldier   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2804
  • View blog
  • Posts: 8,149
  • Joined: 15-January 14

Re: image does not get retrieved from database into html form using php

Posted 07 April 2014 - 10:28 AM

A script that outputs an image, or any other kind of file, can only output that data. If you output other data in the same script, like HTML code, then it's going to corrupt the file. You need a script that will get the file data from the database, output the headers, output the data, and stop there.

Also, you're not checking for the correct item from $_GET.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1