Jump to content

Fetching & Print Multiple Records From A Table


tahakirmani

Recommended Posts

Hi,

 

There are 10 records in my table who have id=10, I am trying to print all the records from this table where id=1.

I have written the following code, but it is showing only the last inserted record not all the records. I used foreach loop and it starts giving me an error message.

 

<?php

 

mysql_connect('localhost','root','');

mysql_select_db('a_database');

 

 

$query= "SELECT * FROM email_images where id=1" ;

$query_run= mysql_query($query);

 

$result= mysql_fetch_assoc($query_run);

$result=$result['name'];

 

foreach ($result as $output) {

 

echo $output;

 

}

 

?>

 

 

 

 

Warning: Invalid argument supplied for foreach() in F:\xampp\htdocs\Email_address\multiple_images.php on line 13

 

Thanks,

Taha.

Link to comment
Share on other sites

My Above mention problem has been solved, and now i am facing a new problem. I am trying to print all the images which are in database. I am using the following code and its printing out the code of images but not images when i am using header function its giving me an error message. Kindly tell me where to use the header function.

 

<?php
mysql_connect('localhost','root','');
mysql_select_db('a_database');
//$current_id= $_SESSION['user_id'];
$query=  "SELECT * FROM email_images where id=1" ;
$query_run= mysql_query($query);



while ($result= mysql_fetch_assoc($query_run))
{

$result=$result['name'];
//foreach ($result as $output) {
// echo $output;


echo $result;


}
?>

Link to comment
Share on other sites

Each image you put onto a web page must have its own HTML <img src='url_that_corresponds_to_the_image' alt=''> tag.

 

The url_that_corresponds_to_the_image would be a url to a .php file (image.php for example) that outputs the content-type header, followed by the image data for one image. Since you would want this same .php file to work for each possible image, you would put a GET parameter on the end of the url that specifies the id of the image (image.php?id=123 for example.)

 

Why have you stored images in a database? If you had stored the images as files in the file system and stored the file name in the database, all you would need to do is output the file names as the url_that_corresponds_to_the_image in the <img ...> tags.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.