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.

Recommended Answers

All 12 Replies

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

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.

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}");
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']; 
}

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!

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<br />"; 

// 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) ;

?>

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.

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.

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

i want to display my image in database to my web page pls i nid the code for dat n also i save it as longbolb in database

Member Avatar for diafol

SHow us your code so far.

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.