473,378 Members | 1,383 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Sql retrieve images from folder

Hi, new here

Wonder if anyone can help

I am learning sql and have searched for the answer for over a day, with no luck. I found this site and it seems you all help each other.

I have a database called "listing_photo"

In that there is row "photo_listing" (unique property number)
& row "photo_caption" (holds the caption for the photo)

The images are kept in a folder called mysite/photo_big

I would like to show the relevant photos for each particular property. in the property template in my site. Also show the photo caption under each. There are 8 photos for each property.

Can anyone show me the correct way to do this in sql php?
Mar 6 '10 #1
11 6041
Atli
5,058 Expert 4TB
Hey.

I'm not getting how your photos and captions are linked to the listing. Could you maybe post an example of real data from your database?

In general, we just do something like this:
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. // Location where the images are stored
  3. $file_path = '/path/to/images/';
  4.  
  5. // Fetch the data for the pictures
  6. $sql = "SELECT `file_name`, `title`, `caption`
  7.         FROM `image_table`
  8.         LIMIT 10";
  9. $result = mysql_query($sql) or trigger_error(mysql_query(), E_USER_ERROR);
  10.  
  11. // Display each picture
  12. while($row = mysql_fetch_assoc($row))
  13. {
  14.     $src = $file_path . $row['file_name'];
  15.  
  16.     echo '<div class="Image">';
  17.     echo "<img src="{$src}" alt="{$row['title']}" title="{$row['title']}"><br>"
  18.     echo "<span>{$row['caption']}</span>";
  19.     echo '</div>';
  20. }
  21. ?>
Not sure exactly how well that will fit your particular situation, though.
Mar 7 '10 #2
Hi Atli

Thanks for your help, really appreciate it!

I have attached a view of the database that accesses the pictures.
This is for a single property 127, to give you an idea.

The pictures are in folder
/holidays/files/photo_big

How would I implement this in your code exactly?

I would like the pictures to display...

Pic1 Pic2
Pic3 Pic4
Pic5 Pic6
etc.

Regards
Attached Images
File Type: jpg DB.jpg (25.1 KB, 401 views)
Mar 8 '10 #3
Anyone help with this please, its driving me crazy
Mar 8 '10 #4
Atli
5,058 Expert 4TB
@malc090350
There are only a few modifications you would have to make to the code I posted above so that it would display the images from your database.
  1. On line #3, insert your actual location instead of my example.
  2. On line #6, change the SQL query to fit your database. I'm not sure exactly how you intend to filter your results, though. I will leave that to you to decide.
  3. On line #14. Assuming your images are saved as their respective photo_id from the database, alter this so that the string compiled uses that field rather than the file_name field I used in my example. You should also add the appropriate image extension.
And that should display them. Now all you need to do is get them to display the way you want.

I would like the pictures to display...

Pic1 Pic2
Pic3 Pic4
Pic5 Pic6
etc.
Again, using the code I posted as a base, you should start by adding the "float: left" CSS style to the "div.Image" class. (You could of course also add it inline, but I wouldn't recommend that.) - That would make the images line up in a horizontal row, one after the other.

Then, to have them drop down to the next "line" every other image, you would have to add a counter to the while loop that goes through the results. Using that counter, add a "<br>" before you print the image every time the counter is an odd number (assuming you start at 0). - You can use the modulus operator (%) to figure that out. (See Arithmetic Operators in the manual.)
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $number = 2;
  3. if($number % 2 == 0) {
  4.     echo "$number is an even number!";
  5. }
  6. else {
  7.     echo "$number is an odd number!";
  8. }
  9. ?>
Let us know if you are having any problems with that!
Mar 9 '10 #5
I have the code below but I am getting the following error...

Parse error: syntax error, unexpected '{', expecting ',' or ';' in /usr/local/home/spainhs/www/holidays/templates/english/public/test.php on line 38
Line 38 is the one in bold...
<Line #21 in the snippet --Atli>
Expand|Select|Wrap|Line Numbers
  1. // Connect to server and select database.
  2. mysql_connect("$host", "$username", "$password")or die("cannot connect");
  3. mysql_select_db("$db_name")or die("cannot select DB");
  4.  
  5. // Location where the images are stored
  6. $file_path = 'http://www.mysite.com/holidays/files/photo_big/';
  7.  
  8. // Fetch the data for the pictures
  9. $sql = "SELECT `photo_id`, `photo_caption_1` , `photo_listing`
  10.         FROM `listing_photo`
  11.     WHERE `photo_listing`= 127
  12.         LIMIT 10";
  13. $result = mysql_query($sql) or trigger_error(mysql_query(), E_USER_ERROR);
  14.  
  15. // Display each picture
  16. while($row = mysql_fetch_assoc($row))
  17. {
  18.     $src = $file_path . $row['photo_id'];
  19.  
  20.     echo '<div class="Image">';
  21.     echo "<img src="{$src}" alt="{$row['photo_caption_1']}" title="{$row['photo_caption_1']}"><br>";
  22.     echo "<span>{$row['photo_caption-1']}</span>";
  23.     echo '</div>';
  24. }
  25. ?>
Just to clarify
listing_photo (is the database)
photo_id (is = to the image file name (i.e. 120.jpg in folder http://www.mysite.com/holidays/files/photo_big/)
photo_caption_1 (is the image caption)
photo_listing (is the unique property id)
See image in above thread.

I guess this line
$src = $file_path . $row['photo_id'];
should include the file type i.e. .jpg or .gif but I am not sure how.

I am not sure how to implement you suggestion to get the images in 2 columns but for now I will be happy just to get the images to show.

Thanks again for you help.
Mar 9 '10 #6
Atli
5,058 Expert 4TB
The error is caused by mismatching quote tags.

That is, when you wrap a string in double-quotes, that string can not contain double-quotes, or they will end the string early. - You can get past this by escaping them (adding a \ in front of them).
Expand|Select|Wrap|Line Numbers
  1. // This is wrong. Will cause an error.
  2. $str = "Simon says: "What?"";
  3.  
  4. // It should be.
  5. $str = "Simon says: \"What?"\";
  6.  
Also, note that on line #22, "photo_caption-1" should be "photo_caption_1".

I guess this line
should include the file type i.e. .jpg or .gif but I am not sure how.
You just add it to the end.
Expand|Select|Wrap|Line Numbers
  1. $src = $file_path . $row['photo_id'] . ".jpg";
Mar 9 '10 #7
That is, when you wrap a string in double-quotes, that string can not contain double-quotes, or they will end the string early. - You can get past this by escaping them (adding a \ in front of them).
So how would this line be displayed?

Expand|Select|Wrap|Line Numbers
  1. echo "<img src="{$src}" alt="{$row['photo_caption_1']}" title="{$row['photo_caption_1']}"><br>";
Mar 9 '10 #8
Atli
5,058 Expert 4TB
You would prefix any double quote that is meant to be a part of the string with a \ (back-slash).
Mar 9 '10 #9
I have got this far and it displays the images in 1 column...

Expand|Select|Wrap|Line Numbers
  1. // Connect to server and select database.
  2. mysql_connect("$host", "$username", "$password")or die("cannot connect");
  3. mysql_select_db("$db_name")or die("cannot select DB");
  4.  
  5. // Location where the images are stored
  6. $file_path = 'http://www.mysite.com/holidays/files/photo_big/';
  7.  
  8. // Fetch the data for the pictures
  9. $sql = "SELECT `photo_id`, `photo_caption_1` , `photo_listing`
  10.         FROM `listing_photo`
  11.         WHERE `photo_listing`= 127
  12.         LIMIT 10";
  13. $result = mysql_query($sql) or trigger_error(mysql_query(), E_USER_ERROR);
  14.  
  15. // Display each picture
  16. while($row = mysql_fetch_assoc($result)){
  17.     $src = $file_path . $row['photo_id'] . ".jpg";
  18.     $number = 2;
  19. if($number % 2 == 0) {
  20.     echo '$number';
  21. }
  22. else {
  23.     echo '$number';
  24. }
  25. ?>
  26.  
  27.     <div class="Image">
  28.     <img src="<?=$src?>" alt="<?=$row['photo_caption_1']?>" title="<?=$row['photo_caption_1']?>"><br>
  29.     <span><?=$row['photo_caption_1']?></span>
  30.     </div>
  31.  
  32. <?php } ?>
Not sure how to fix this...

Expand|Select|Wrap|Line Numbers
  1. $number = 2;
  2. if($number % 2 == 0) {
  3.     echo '$number';
  4. }
  5. else {
  6.     echo '$number';
  7. }
Results http://www.spain-holiday-sun.com/hol...ublic/test.php
Mar 9 '10 #10
Atli
5,058 Expert 4TB
Ok. That works to :)

Not sure how to fix this...
The idea there is to create a counter, to count the number of times the while loop executes. So you would have to create a variable outside the loop, and then increment it inside the loop.
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $i = 0;
  3. while($something == true) {
  4.     $i++;
  5.     echo $i;
  6. }
  7. ?>
This would keep looping while that $something remained true, incrementing $i by one each loop.

Then you could use the modulus (%) to calculate whether or not the number in $i is even or odd, and based on that print a <br> to add a line.

O, and you would also need to add the CSS float: left; to your .Image class (or add it to the <div> style attribute) so the images line up next to each other, rather than below each other.
Mar 9 '10 #11
Thanks Alti

But could you show me an exact example for my code above to show 2 columns
Mar 11 '10 #12

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: | last post by:
Hi, I have a SQL Server database table where one of the columns is of type Image. The problem occurs when I try to retrieve images from the table. I'm using the following C# code: ...
1
by: Jason Shohet | last post by:
In my wsp, i clicked add to project, & selected Primary Output & Content Files. Thats enough to get my site working. But I want to add the images folder too so that image files that the site...
1
by: Luis Esteban Valencia | last post by:
How to retrieve images from sql server? -- LUIS ESTEBAN VALENCIA MICROSOFT DCE 2.
7
by: ABC | last post by:
How can I deny all users directly access image files from images folder?
2
by: FayeC | last post by:
I have created a site in Joomla with a login (no self registration, users are provided with username and password by the admin). The users are supposed to login to a specific page where they can...
1
by: ahujasatna | last post by:
Hi all!! i am working on C#, ASP.Net with Sql server2000 and i am facing a problem, my Question is: "How to insert and retrieve images to/from Sqlserver2000 Database" I created a table...
6
by: LP | last post by:
Hi there, Sorry if this is a little off-topic here but can anyone explain why, when I launch my website from within VS, it can find my image files ok when they're in the root like so: <img...
3
mafaisal
by: mafaisal | last post by:
Hello Experts how to retrieve images From sql2005 to vb.net2005 Also i insert image into database using stored procedures, Is any other way to insert in vb.net2005 Faisal
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.