simple image and description diaplay - problems

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
thurstan
Forum Commoner
Posts: 28
Joined: Mon Feb 28, 2005 7:40 am

simple image and description diaplay - problems

Post 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.
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

Post 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.
thurstan
Forum Commoner
Posts: 28
Joined: Mon Feb 28, 2005 7:40 am

Post by thurstan »

thanks codergoblin....but the problem remains with your suggested change. i'll keep investigating.
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

Post 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).
User avatar
n00b Saibot
DevNet Resident
Posts: 1452
Joined: Fri Dec 24, 2004 2:59 am
Location: Lucknow, UP, India
Contact:

Post by n00b Saibot »

your get_image code too contains mysql_fetch_array.
change it to mysql_fetch_assoc.[/b]
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
thurstan
Forum Commoner
Posts: 28
Joined: Mon Feb 28, 2005 7:40 am

[SOLVED] simple image and description diaplay - problems

Post 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>
Post Reply