954,585 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

How do you get php to display more than one image from database?

I'm trying to display in a web page all the images stored in a database. THis is my code, but it only displays the first picture:

<?php
include "cysylltiad.php";
$result = mysql_query("SELECT * FROM files  ORDER BY fid"); 
while($row = mysql_fetch_array($result)){ 
header("Content-Type: {$row['type']}");
   echo $row["content"]; 
}
?>


where content is the name of the picture field.

Any help would be appreciated.

twmprys
Newbie Poster
8 posts since Oct 2008
Reputation Points: 10
Solved Threads: 0
 

hi,

are you storing the images in the database or just the path to the images?

Also I always set the query to die if it fails and output the mysql error when it does IE

$result = mysql_query("SELECT * FROM files ORDER BY fid") or die(mysql_error());

Richard

rickya100
Junior Poster in Training
78 posts since Mar 2008
Reputation Points: 13
Solved Threads: 1
 

Hi, thanks for responding. I'm storing the images actually in the database. I want to be able to allow a user to upload pictures with captions, then output all the pictures with their associated captions on a page.

twmprys
Newbie Poster
8 posts since Oct 2008
Reputation Points: 10
Solved Threads: 0
 

I'm trying to display in a web page all the images stored in a database. THis is my code, but it only displays the first picture:

<?php include "cysylltiad.php"; $result = mysql_query("SELECT * FROM files ORDER BY fid"); while($row = mysql_fetch_array($result)){ header("Content-Type: {$row['type']}"); echo $row["content"]; } ?>

where content is the name of the picture field.

Any help would be appreciated.


in my mind, this should produce a row of images
the content-length header should tell the browser where to break the image,
untested, I do not use blobs
blobs in databases are slow, processor intensive and unneccesarily large, my databases just store the file system pointer to the image

while($row = mysql_fetch_array($result)){ 
header("Content-Type: {$row['type']}");
header("Content-Length: strlen($row['content'])");    
echo $row['content']; 
}
almostbob
Posting Sensei
3,149 posts since Jan 2009
Reputation Points: 571
Solved Threads: 376
 

Hi there!

I have a nice piece of code I use for queries like this, take a look.

function mysql_fetch_values($result, $numass=MYSQL_BOTH)
{
	$i=0;
	$keys=array_keys(mysql_fetch_array($result, $numass));
	mysql_data_seek($result, 0) ;
	while ($row = mysql_fetch_array($result, $numass)){
		foreach ($keys as $speckey) 
			$got[$i][$speckey]=$row[$speckey];
		$i++;
	}
	return $got;
}


This enables you to fetch results in the form $result[row][column]. So if you have a database holding images and want to display all of them you would include this function and then use:

$result = mysql_fetch_values(mysql_query("SELECT * FROM files ORDER BY fid"));

foreach ($result as $image)
echo $image['content'];


Hope this helps!

danielpataki
Light Poster
28 posts since Oct 2008
Reputation Points: 10
Solved Threads: 0
 
Function  output_array14(){// by default we show first page
$page = 1;
// If current page number, use it 
// if not, set one! 
//echo $_GET['page'];
if(!isset($_GET['page'])){ 
    $page = 1; 
} else { 
    $page = $_GET['page']; 
} 

// Define the number of results per page 
$max_results = 13; 

// Figure out the limit for the query based 
// on the current page number. 
$from = (($page * $max_results) - $max_results);  
//echo $from;
// Perform MySQL query on only the current page number's results 
//echo $from,$max_results;


$sql = mysql_query("SELECT * FROM Garant WHERE Portfolio='existing' LIMIT $from, $max_results") ; 
//$sql = mysql_query("SELECT * FROM Garant"); 
//$query= "SELECT * FROM Garant order by Price ASC"
$result = mysql_query($sql);

while($row = mysql_fetch_array($sql))
{ 
    // Build your formatted results here.  
$count = mysql_result(mysql_query("SELECT COUNT(*) as Num FROM Garant"),0); 
if($count > 0) {  
    $max=4; //set the number of images across here

    echo "<table>";  
    
    for($i=0;$i<$max_results;$i+=$max) /* loop through and build the table */  
    {  
        $headings = $pics = $locations = '<tr>';  
        for($j=0;$j<$max;$j++)  
        { 
            if($row = mysql_fetch_assoc($sql))             
            {  

//$pics .="<TD bgcolor=\"#FFFFFF\" width=\"125\" ><A HREF=\"details.php?detail=".$images["Ref"]." \"><IMG SRC=\"resources/".$images["smallpic"]."\"></A></TD>";
$pics .="<TD bgcolor=\"#B74F8F\"  border=\"2\"><A HREF=\"details.php?detail=".$row["Ref"]." \"><IMG  SRC=\"resources/".$row["smallpic"]."\"></A></TD>";
//$locations .= '<td  align="center" bgcolor=#DDEEFF><B>' .$images['Price']. ' Euro - ref - ' .$images['Ref'].'</td>';  
 $locations .= '<td  align="left" bgcolor=#DDEEFF><B> ' .$row['Ref']. '    Price: ' .$row['Price'].' Euro</td>'; 
            } else {  
               // $headings .= '<td>&nbsp;</td>';  
                $pics .= '<td>&nbsp;</td>';  
                $locations .= '<td>&nbsp;</td>';  
            }              
        }  
        $headings .= "</tr>\n";  
        $pics .= "</tr>\n";  
        $locations .= "</tr>";  
        echo $headings; 
        echo $pics; 
        echo $locations; 
    }  
    echo '</table>'; 
} 
}

// Figure out the total number of results in DB: 
$total_results = mysql_result(mysql_query("SELECT COUNT(*) as Num FROM Garant"),0); 

// Figure out the total number of pages. Always round up using ceil() 
$total_pages = ceil($total_results / $max_results); 
//echo $total_pages, $total_results, $max_results;
// Build Page Number Hyperlinks 
echo "<center>Select a Page"; 

// Build Previous Link 
if($page > 1){ 
    $prev = ($page - 1); 
    echo "<a href=\"".$_SERVER['PHP_SELF']."?page=$prev\"><<Previous</a> "; 
} 

for($i = 1; $i <= $total_pages; $i++){ 
    if(($page) == $i){ 
        echo "$i "; 
        } else { 
            echo "<a href=\"".$_SERVER['PHP_SELF']."?page=$i\">$i</a> "; 
    } 
} 

// Build Next Link 
if($page < $total_pages){ 
    $next = ($page + 1); 
    echo "<a href=\"".$_SERVER['PHP_SELF']."?page=$next\">Next>></a>"; 
} 
echo "</center>"; 

}
mysql_free_result($result) ;

?>
phobia1
Light Poster
25 posts since Aug 2007
Reputation Points: 10
Solved Threads: 0
 

No, that doesn't work - nothing shows now. I'm obviously missing something. I think I read somewhere on the net that you need one page to call up the pictures, and another to display them but I can't find an explanation of that anywhere.

twmprys
Newbie Poster
8 posts since Oct 2008
Reputation Points: 10
Solved Threads: 0
 

Tutorial:: get multiple images from sql blobs

its tedious
now I'm really glad I dont store blobs in the database

almostbob
Posting Sensei
3,149 posts since Jan 2009
Reputation Points: 571
Solved Threads: 376
 

I'm not a huge expert on database speed, but I believe it is recommended NOT to use BLOB a lot because file systems are much more quicker at handling files.

Personally I never use blogs, I store the filename in the database. I use the code I posted above to cycle through all the file names and show them on site.

danielpataki
Light Poster
28 posts since Oct 2008
Reputation Points: 10
Solved Threads: 0
 

I've never stored images as BLOB in a DB as every article I've ever read says to not do. I find it much easier (once you learn it) just storing the path in the database and storing the actual files in a folder.

I know that doesn't help. If it's possible I would change to just storing the path, if not I hope someone with experience of BLOBs can help.

Good luck

rickya100
Junior Poster in Training
78 posts since Mar 2008
Reputation Points: 13
Solved Threads: 1
 

you can see an example of image display from a database at http://areabulgaria.net/properties.php let me know if you need other help. This works by storing the filename etc.
best
F

phobia1
Light Poster
25 posts since Aug 2007
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You