Hi,

I have some pictures in a MySQL database that i am able to retrieve and display. How ever, i want to display them as images in a gallery, but not able to.

The MySQL query is ok, it retrieves the images, i think the problem is with my PHP script. Any help would be greatly appreciated. Here is the code that displays a single image (which works fine)

include ("database.php");

	if (!isset($_GET['id'])) { echo "<br />ERROR: id not specified ";    } 
 
	$id = (int) $_GET['id'];
 
	if ($id <= 0) { echo "<br />ERROR: Invalid id <br />"; } 
 
	$query  = sprintf("SELECT * FROM property_images JOIN property ON property.id = property_images.property_id ORDER BY property_images.id DESC LIMIT 1");	
	$result = mysql_query($query, $db) or die('Error, query failed'); 
	
	if (mysql_num_rows($result) == 0) { echo "Error: property has no images. ";   } 

	$row = mysql_fetch_array($result);			
	extract($row);	
	
	header('Content-type: ' . $row['uploadedimage_type']);
        header('Content-length: ' . $row['uploadedimage_size']);
 
       echo $row['content'];

and this is what i've tried for displaying all images .... with no luck (blank page)

$query  = sprintf("SELECT * FROM property_images JOIN property ON property.id = property_images.property_id ORDER BY property_images.id DESC LIMIT 0 , 30");	
	$result = mysql_query($query, $db) or die('Error, query failed'); 

    if (mysql_num_rows($result) == 0) { echo "Error: property has no images. ";   }
    else {			
        while ($row = mysql_fetch_array($result)) {				
	     extract($row);	}	// end while	

    header('Content-type: ' . $row['uploadedimage_type']);
    header('Content-length: ' . $row['uploadedimage_size']);
 
    echo $row['content']; 	} // end else

easy

<?php

$query="SELECT * FROM database ";
$result=mysql_query($query);
$num=mysql_numrows($result);


$i=0;
while ($i < $num) {

// get your fields in the databse
$field1 =mysql_result($result,$i,"column");

//PUT YOUR STUFF HERE!


$i--;
}

Thank you.

Could you please clarify

// get your fields in the databse
$field1 =mysql_result($result,$i,"column");

I've tried using the column name of the images in both the $field1 and column, and it doesn't work.

why do you have to extract() the result? when you can already traverse the recordset via mysql_fetch_array. and when you do extract you dont use arrays anymore like $row coz extract() assign values to array indexes so instead of echo $row you use $content. you may try this

<?php

    $query  = sprintf("SELECT * FROM property_images JOIN property ON property.id = property_images.property_id ORDER BY property_images.id DESC LIMIT 0 , 30");	
	$result = mysql_query($query, $db) or die('Error, query failed'); 

    if (mysql_num_rows($result) == 0) { echo "Error: property has no images. ";   }
    else {			
        while ($row = mysql_fetch_array($result)) {				
	     header('Content-type: ' . $row['uploadedimage_type']);
    header('Content-length: ' . $row['uploadedimage_size']);
 
    echo $row['content']; }	// end while	

    	} // end else

?>
Member Avatar for rajarajan2017
header('Content-type: ' . $row['uploadedimage_type']);
    header('Content-length: ' . $row['uploadedimage_size']);
 
    echo $row['content']; 	} // end else

Why not use this within the while?

That was the point of mine, you put whatever code you want executed in the while, as for the field name it whatever you want, that's a variable name. You need to change the column to whatever your name it in the database (that you want to pull) for that entry. Therefore if you have a table and it has the values: firstname,lastname to loop through every first and last name you use my code like:


Database setup fields: firstname,lastname

$query="SELECT * FROM database ";
$result=mysql_query($query);
$num=mysql_numrows($result);
 
 
$i=0;
while ($i < $num) {
 
// get your fields in the databse
$fname =mysql_result($result,$i,"firstname");
$lname =mysql_result($result,$i,"lastname");


echo "First Name:$fname, Last Name:$lname";
 
 
$i--;
}

If there's 50 names it'll print one at a time.

Thanks guys.

@ vaultdweller123 and rajarajan07 - that was the first thing i tried. I thought was only logical to loop it to get all results. But the problem is i get a link to the page as output (e.g. http://localhost/...../display_gallery.php?id=1)

Here is the code that gives me that

if (mysql_num_rows($result) == 0) { echo "Error: property has no images. ";   } // end if		
	
else {	
   while ($row = mysql_fetch_array($result)) {					
	header('Content-type: ' . $row['uploadedimage_type']);
	header('Content-length: ' . $row['uploadedimage_size']);
	echo $row['content']; 	
    }	// end while	
 } // end else

@ mediachicken - thanks for the clarification. I get the same output as above, i.e a link to the page

$num = mysql_fetch_array($result);	
  
$i = 0;	
while ($i < $num) {	
     $content = mysql_result($result, $i, "content");	
     header('Content-type: ' . $num['uploadedimage_type']);
     header('Content-length: ' . $num['uploadedimage_size']);		
	
     echo "$content";	
  
     $i--;	}	// end while

I must be doing something wrong - but i can't figure out what :(

PS. why is $i-- and not ++ ?

thanks all :) Good job

what does your $row holds in your database? if it's an image then you dont just echo it directly, you have to put it on an <img> tag

echo "<img src='".$row['content']."' />";

i hope i get your point :)

vaultdweller123 - that was it! :)

Thank you all very much guys.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.