Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

How to display multiple images in php code from mysql database

php code mysql

This topic has been archived. This means that you cannot reply to this topic.
4 replies to this topic

#1 NivasBapanapalli

NivasBapanapalli

    CC Lurker

  • Just Joined
  • Pip
  • 1 posts

Posted 22 February 2014 - 02:44 AM

i was using this code please help me any one : why i have errors 

<?php 

mysql_connect("localhost:3306","root","");
mysql_select_db("main");


$sql="select * from job";
$query=mysql_query($sql);


while($row=mysql_fetch_array($query))
{


$image=$row ["image"];
$imageType=$row['image_type'];
header("Content-type:$imageType") ;
echo '<img src="path/'.$image.'" width="200" height="130">';


echo $row["job_title"];
echo $row["type"];




}


//echo $row["job_title"];


//echo "<br>";




?>

Edited by Roger, 22 February 2014 - 12:32 PM.
added codetags


#2 BlackRabbit

BlackRabbit

    CodeCall Legend

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 3871 posts

Posted 22 February 2014 - 06:16 AM

Welcome aboard Nivas!

 

First of all, what is the error?

second, what are you trying to do?

 

you don't need to use "header" in the loop, and I also think you are missing the "path". Did you intent to use $path?



#3 Poe

Poe

    CC Resident

  • Advanced Member
  • PipPipPipPip
  • 81 posts

Posted 05 March 2014 - 06:04 PM

if your wanting to make an image from a PHP script, look up the Graphics Development Library or PHP:GD. 
 
if your wanting to loop a bunch of images (say for an image gallery) I tend to use a for loop unless i'm JOINing multiple tables.

 

<?php

 

   include "/path/to/file/connect.php";

 

   $my_que[0] = mysql_query("SELECT * FROM `my_images`");

   for( $i = 0; $i < mysql_num_rows( $my_que[0] ); $i++ )

   {

 

      $row = mysql_fetch_array( $my_que[0] );

      print "<div><img src=\"/path/to/file/". $row['image_src'] . ".png\" /></div>\n";

 

   }

 

?>

 

don't include a header unless you are using an image creation library it will make the browser think its an image, yet its not so what does the browser do?


Edited by Poe, 06 March 2014 - 09:25 AM.

"Portability is for those who can't write new programs" - Linus Torvalds


#4 skynett

skynett

    CC Lurker

  • Just Joined
  • Pip
  • 1 posts

Posted 23 March 2014 - 03:55 AM

apart from the header line of code, it looks ok to me.

 

perhaps the path to the image is wrong or the image name field in the $row variable is incorrect?



#5 Orjan

Orjan

    CC Mentor

  • Moderator
  • 2918 posts

Posted 23 March 2014 - 04:19 PM

To do what you want, you need two files. One that returns the image data and one that returns the html pointing to the image returning file.

The image returning file should have the header as you do, and ONLY return the content of the file, nothing else whatsoever! To be able to fetch the correct image you need to add the db key to the image in the database file, something like image.php?id=57398, using the $_GET['id'] to fetch the binary data. In the calling file you generate your html as you do, calling the generating file like <img src="image.php?id=57398">

I'm a System developer at XLENT Consultant Group mainly working with SugarCRM.
Please DO NOT send mail or PM to me with programming questions, post them in the appropriate forum instead, where I and others can answer you.





Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download