11 Replies - 2784 Views - Last Post: 16 December 2011 - 08:07 AM Rate Topic: -----

#1 jeffpoy0816  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 09-December 11

How can I show many images using database?

Posted 11 December 2011 - 08:19 AM

hello sir and mam im a new member in this forum please help me about this showing many image?i have already code for images but I can't show all images if I download a lots of images. please help me how can I show images using database?


<?php
session_start();


$connect =mysql_connect("localhost","root","")  or die("Can not connect to database: ".mysql_error());;
				$db=mysql_select_db("images");  //select database



here's my code
			
$id = 1;
if(!isset($id) || empty($id) ){
die("Please select your image!");
}else{

$querys = mysql_query("SELECT * FROM picture WHERE id='".$id."'");
//$querys = mysql_query("SELECT * FROM picture ");

$row = mysql_fetch_array($querys);

$content = $row['image'];



header('Content-type: image/jpg');


echo $content;
}

?>


MOD EDIT: When posting code...USE CODE TAGS!!!

:code:

This post has been edited by JackOfAllTrades: 11 December 2011 - 09:57 AM


Is This A Good Question/Topic? 0
  • +

Replies To: How can I show many images using database?

#2 Duckington  Icon User is offline

  • D.I.C Addict

Reputation: 164
  • View blog
  • Posts: 599
  • Joined: 12-October 09

Re: How can I show many images using database?

Posted 11 December 2011 - 08:46 AM

If you want to loop through more than 1 result, you can do:

$query = mysql_query("SELECT * FROM `picture`");
while($row = mysql_fetch_assoc($query))
{
$content = $row['image'];
// Then do whatever you want with it
}



I'd put the header at the top of the page though.
Was This Post Helpful? 1
  • +
  • -

#3 jeffpoy0816  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 09-December 11

Re: How can I show many images using database?

Posted 11 December 2011 - 11:33 AM

hi sir I try your code but only one images can show, how can i loop more than one result or images?
Was This Post Helpful? 0
  • +
  • -

#4 aaron1178  Icon User is offline

  • Dovakiin, Dragonborn
  • member icon

Reputation: 169
  • View blog
  • Posts: 1,297
  • Joined: 22-October 08

Re: How can I show many images using database?

Posted 11 December 2011 - 11:40 AM

Actually, Duckington's method does work, it's just you lack experience. Did you try output the image data in the loop? Because if you try to show the image outside the while loop, it will show one row. Also, how many rows are in your database?
Was This Post Helpful? 0
  • +
  • -

#5 jeffpoy0816  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 09-December 11

Re: How can I show many images using database?

Posted 11 December 2011 - 11:51 AM

Actually I am beginner in php and sql so I try harder to understand the code. yes I try to output the images in the loop, it will show one row image, if I output outside the loop will show the other images, but only one row can show.
Was This Post Helpful? 0
  • +
  • -

#6 aaron1178  Icon User is offline

  • Dovakiin, Dragonborn
  • member icon

Reputation: 169
  • View blog
  • Posts: 1,297
  • Joined: 22-October 08

Re: How can I show many images using database?

Posted 11 December 2011 - 11:59 AM

How are you storing the images in your database? If the while loop is not working, it is something you've done wrong.
Was This Post Helpful? 0
  • +
  • -

#7 Duckington  Icon User is offline

  • D.I.C Addict

Reputation: 164
  • View blog
  • Posts: 599
  • Joined: 12-October 09

Re: How can I show many images using database?

Posted 11 December 2011 - 11:59 AM

Well it's because of the way you are doing it. You are telling the php script that it is outputting an image only with that header, so it's like opening up a single image file when you go to the URL, it can't show multiple images in 1 image file, so it loops through the images and displays them, but I imagine you are probably only seeing the last one in the loop, because it can't output them all.

What exactly is it you are trying to do?

If you literally just want to display them on the page, you do not need that header(). You can just do something like:

$query = mysql_query("SELECT * FROM `picture`");
while($row = mysql_fetch_assoc($query))
{
$content = $row['image'];

echo '<img src="http://www.yourwebsite.com/'.$row['picture'].'" alt="Image" />';

}




And display them like a normal HTML image.

This post has been edited by Duckington: 11 December 2011 - 12:00 PM

Was This Post Helpful? 0
  • +
  • -

#8 jeffpoy0816  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 09-December 11

Re: How can I show many images using database?

Posted 11 December 2011 - 12:46 PM

yah that's right I only seeing the last one in the loop...

by using database I dont know how to show mutliple images.

your code is nice, but how I can do multiple images using the loop?
Was This Post Helpful? 0
  • +
  • -

#9 Duckington  Icon User is offline

  • D.I.C Addict

Reputation: 164
  • View blog
  • Posts: 599
  • Joined: 12-October 09

Re: How can I show many images using database?

Posted 11 December 2011 - 01:29 PM

The way you want to do it, you can't.

The header you are sending is effectively telling the php file to display itself like a .jpg image file.

If you open up a .jpg image file, you can't have multiple images in it, you can only have that 1 .jpg image. So what your script is doing is looping through the images and trying to output them to the browser, but it is obviously only showing you the last one.

Like I said before, if you just want to have a page with a series of images displayed on it, remove the header() and display them as HTML images.

If not, you will have to explain what it is you are actually trying to do... I mean, we don't even know what information you have stored in your `picture` table. Is `image` a text link to a file, is it just a file name, is is actually an image stored as a BLOB or something? More information is required and you need to explain what the goal you are trying to achieve is.

This post has been edited by Duckington: 11 December 2011 - 01:31 PM

Was This Post Helpful? 0
  • +
  • -

#10 jeffpoy0816  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 09-December 11

Re: How can I show many images using database?

Posted 11 December 2011 - 07:36 PM

if I removed the header my images show in the page is a text-code not an images, and in my picture table I used a blob.

actually I am trying to achieve to make a shopping cart with an images so I need to show all images for the product.


I've like to thanks to you because I have made a broaden knowledge about the php script and sql.
Was This Post Helpful? 0
  • +
  • -

#11 jeffpoy0816  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 09-December 11

Re: How can I show many images using database?

Posted 16 December 2011 - 06:56 AM

hi sir and mom

anyone can help me about my problem? how can i show many images using database?

here's my code:

but the problem in my code the images did not show. only text can show if I run this code

ha.php
<?php

session_start();


$connect =mysql_connect("localhost","root","")  or die("Can not connect to database: ".mysql_error());;
				$db=mysql_select_db("images");  //select database

function picture()
{
	$get = mysql_query("SELECT * FROM `picture");

while($row = mysql_fetch_assoc($get))

if(mysql_num_rows($get)==0)
{
echo "there are no product to display";
}
else{
	while ($get_row = mysql_fetch_assoc($get))
	{
//	   header("Content-type: image/jpg");

	echo $get_row['codenumber'].'<br/>'.$get_row['description'].'<br/>'.$get_row['price'].'<br/>'.$get_row['image'].'<br/>';
	
	}
}


}

?>



haindex.php
<?php require 'ha.php'; ?>

<html>
<head>


</head>

<body>
<?php // cart(); ?>
<?php  picture(); ?>


</body>
</html>


Was This Post Helpful? 0
  • +
  • -

#12 JackOfAllTrades  Icon User is online

  • Saucy!
  • member icon

Reputation: 5959
  • View blog
  • Posts: 23,229
  • Joined: 23-August 08

Re: How can I show many images using database?

Posted 16 December 2011 - 08:07 AM

Merged duplicate topics. Do not create a new topic on the same issue you already have one for.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1