Page 1 of 1

simple image and description diaplay - problems

Posted: Mon Feb 28, 2005 7:48 am
by thurstan
hi

i am trying to something real simple - add a function to a CMS to add an image and description to the databse, and output this on a webpage.

i've got a form to do this, which upload the image and description to the databse ok.

the problem is displaying this on a page. i can get get the page to display the description (but no image) and the image (but not description) - can't do both !

here's my 'display image' code:

Code: Select all

<html><head><title>Show All Images</title></head><body><p>
    <?
	include("dbconnect.php");
    $results = mysql_query("SELECT * FROM img_table");
    while ($row = mysql_fetch_array($results))
	echo "<img src="get_image.php?id=" . $row&#1111;'img_id'] . ""><br> \n";
	echo "<br />" . $row&#1111;'description'] . "</div> \n";
	echo "<br />" . $row&#1111;'project'] . "</div> \n";
	?>
  </p>
  </body></html>
this shows all the images in the dbase but no descriptions.

here's my 'get image' code:

Code: Select all

<?
include("dbconnect.php");
  $result = mysql_query("SELECT img_data FROM img_table WHERE img_id=" . $_GET&#1111;'id']);
  $result_array = mysql_fetch_array($result);
  header("Content-Type: image/jpeg");
  echo $result_array&#1111;'img_data'];
  ?>
i'm very confused, and new to php, can anybody help? thanks.

Posted: Mon Feb 28, 2005 7:59 am
by CoderGoblin
First glance....

Code: Select all

while ($row = mysql_fetch_array($results))
Should be

Code: Select all

while ($row = mysql_fetch_assoc($results))
As it stands the row is returning an standard array, not one indexed by fieldname.

Posted: Mon Feb 28, 2005 8:11 am
by thurstan
thanks codergoblin....but the problem remains with your suggested change. i'll keep investigating.

Posted: Mon Feb 28, 2005 8:29 am
by CoderGoblin
Next step then is to try and narrow down the problem...

Build an example page such as :

Code: Select all

<html>
  <head>
    <title>Show All Images</title>
  </head>
  <body>
    <p>
      <img src="get_image.php?id=5"><br>
      Image description<br /></div>
      Project<br /></div>
   </p>
  </body>
</html>
(Replace the id for a valid one).
Does this work in getting the image ?

If yes then view source the result of the main source and confirm it is as expected (Note you have a couple of </div> when you do not open any).

As an aside...
On the image code, you may want to change $_GET['id'] to floor($_GET['id']) if it should be numeric to prevent what is known as SQL Injection attacks. (Never trust parameters passed in).

Posted: Mon Feb 28, 2005 8:48 am
by n00b Saibot
your get_image code too contains mysql_fetch_array.
change it to mysql_fetch_assoc.[/b]

Posted: Mon Feb 28, 2005 8:58 am
by feyd
it's advisable to note that storing images in the database can be very tasking on your server because each image requires a seperate page request that performs a database lookup and fetch (of potentially large amounts of data).

I have suggested in the past and will do so again that you store the image itself in the filesystem, storing the filename and maybe the content-type if you have trouble "discovering" it with the filename.


mysql_fetch_array() returns named and numeric indexed sets.. so it's technically okay.

The originally posted code doesn't use braces to enclose the loop to include the description and project outputs, so they will be displayed after the loop is finished. By that point $row is false.. so you will recieve empty bits in the html.

[SOLVED] simple image and description diaplay - problems

Posted: Mon Feb 28, 2005 9:21 am
by thurstan
thanks for all your tips people - i now have it working!

the additon of the curly braces around the echo's and changing the array seemed to do the trick. the 'show image' page now displays the image and text. i changed the code to:

Code: Select all

<html><head><title>Show All Images</title></head><body>
    <?
	include("dbconnect.php");
    $results = mysql_query("SELECT * FROM img_table");
    while ($row = mysql_fetch_assoc($results))
	&#123; echo "<img src="get_image.php?id=" . $row&#1111;'img_id'] . ""><br> \n";
	echo "<br />" . $row&#1111;'description'] . " \n";
	echo "<br />" . $row&#1111;'project'] . " \n"; &#125;
	?>
  </body></html>