| | |
How do you get php to display more than one image from database?
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
•
•
Join Date: Oct 2008
Posts: 8
Reputation:
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:
where content is the name of the picture field.
Any help would be appreciated.
php Syntax (Toggle Plain Text)
<?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.
Last edited by peter_budo; Mar 8th, 2009 at 7:05 am. Reason: Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks) and [icode] (inline code) tags.
•
•
Join Date: Mar 2008
Posts: 78
Reputation:
Solved Threads: 1
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
Richard
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
php Syntax (Toggle Plain Text)
$result = mysql_query("SELECT * FROM files ORDER BY fid") or die(mysql_error());
Richard
Last edited by peter_budo; Mar 8th, 2009 at 7:06 am. Reason: Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks) and [icode] (inline code) tags.
•
•
•
•
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.
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
php Syntax (Toggle Plain Text)
while($row = mysql_fetch_array($result)){ header("Content-Type: {$row['type']}"); header("Content-Length: strlen($row['content'])"); echo $row['content']; }
Last edited by almostbob; Mar 5th, 2009 at 8:38 pm.
Failure is not an option It's included free
If at first you dont succeed, join the club
Of course its always in the last place you look, you dont keep looking after you find it
Please mark solved problems, solved
If at first you dont succeed, join the club
Of course its always in the last place you look, you dont keep looking after you find it
Please mark solved problems, solved
Hi there!
I have a nice piece of code I use for queries like this, take a look.
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:
Hope this helps!
I have a nice piece of code I use for queries like this, take a look.
php Syntax (Toggle Plain Text)
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:
php Syntax (Toggle Plain Text)
$result = mysql_fetch_values(mysql_query("SELECT * FROM files ORDER BY fid")); foreach ($result as $image) echo $image['content'];
Hope this helps!
Last edited by peter_budo; Mar 8th, 2009 at 7:06 am. Reason: Just adding code tags for second code example
•
•
Join Date: Aug 2007
Posts: 25
Reputation:
Solved Threads: 0
php Syntax (Toggle Plain Text)
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> </td>'; $pics .= '<td> </td>'; $locations .= '<td> </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) ; ?>
Last edited by peter_budo; Mar 8th, 2009 at 7:07 am. Reason: Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks) and [icode] (inline code) tags.
Tutorial:: get multiple images from sql blobs
its tedious
now I'm really glad I dont store blobs in the database
its tedious
now I'm really glad I dont store blobs in the database
Failure is not an option It's included free
If at first you dont succeed, join the club
Of course its always in the last place you look, you dont keep looking after you find it
Please mark solved problems, solved
If at first you dont succeed, join the club
Of course its always in the last place you look, you dont keep looking after you find it
Please mark solved problems, solved
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.
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.
•
•
Join Date: Mar 2008
Posts: 78
Reputation:
Solved Threads: 1
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 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
![]() |
Similar Threads
- displaying image from database (PHP)
- Trying to display an image rom mysql: whats wrong (PHP)
- thumbimage.php errors when image missing (PHP)
- Image Scrolling using PHP (PHP)
- how to display image in wml page using php (PHP)
- display image file from mysql databse using php (PHP)
- uploading image in database (PHP)
Other Threads in the PHP Forum
- Previous Thread: what is best method to get response from a service?
- Next Thread: Session share across multiple domains same IP
| Thread Tools | Search this Thread |
# 5.2.10 alexa apache api array beginner binary broken cakephp checkbox class clean clients cms code cron curl database date directory display dissertation dynamic echo echo$_get[x]changingitintovariable... email encode error fairness file files folder form forms function functions google href htaccess html image images include indentedsubcategory insert ip javascript joomla legislation limit link local login mail memberships menu mlm multiple multipletables mysql mysqlquery newsletters oop open paypal pdf persist php problem provider query radio random recursion remote rss script search server sessions sms sockets source space spam sql syntax system table tutorial update upload url validator variable variables video web youtube






