469,282 Members | 1,947 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,282 developers. It's quick & easy.

How to display image from mysql

i am doing a site for spinning mills
i need to display all the products that the company offers
Am using mysql database and word press (CMS)

i want to display my images with code number as like my attachment
Attached Images
File Type: jpg shade.jpg (11.9 KB, 567 views)
Mar 9 '10 #1
19 8764
5,058 Expert 4TB

What have you done so far?
And what are you having trouble with?
Mar 9 '10 #2
i want to display my images with that code. and also i want upload tat scanned images in our database.that time i want group that image color vise.
Mar 9 '10 #3
Am doing that manual not writing any queries
Mar 9 '10 #4
5,058 Expert 4TB
Ok. What have you done so far?

I don't know much about the specifics of WordPress, but I do know that in plain PHP this should be a simple matter of querying the data from the database, looping through the results and printing each one.

What do you mean? If you plan on interacting with a MySQL database, you will have to write a few queries.
Mar 9 '10 #5
Expand|Select|Wrap|Line Numbers
  1. <?
  2. $dbcnx = @mysql_connect("localhost", "root", "red");
  4. if (!$dbcnx)
  5. {
  6. echo( "connection to database server failed!" );
  7. exit();
  8. }
  10. if (! @mysql_select_db("my_db") )
  11. {
  12. echo( "Image Database Not Available!" );
  13. exit();
  14. }
  16. $img = $_REQUEST["img"];
  17. $result = @mysql_query("SELECT * FROM images WHERE imgid=" . $img . "");
  19. if (!$result)
  20. {
  21. echo("Error performing query: " . mysql_error() . "");
  22. exit();
  23. }
  25. while ( $row = mysql_fetch_array($result) )
  26. {
  27. $imgid = $row["imgid"];
  28. }
  30. echo base64_decode($encodeddata);
  31. ?>

this is not work what problem in that
Mar 9 '10 #6
5,058 Expert 4TB
I'm not surprised, seeing as this code was copy/pasted from an online tutorial (yes I can use Google too), one that was not meant to be put as-is into a working product. - It's meant to teach, not to be used.

You won't get a working solution by copy/paste alone. You need to actually know, or learn, PHP and MySQL to get this working. - And we will most certainly not be doing it for you.

If you want our help, tell us specifically what you are having trouble with. We aren't just going to take code you copied from some random tutorial and fix it up so you can copy it back into your project.
Mar 9 '10 #7
dear Atli

Please help me. When we want to display our images from database that time we start our script like
<img src="filename.php?id="imageid">
what is the use of this..
and where that image id will retrieve. I think that is our table id. is it correct or not.
Mar 10 '10 #8
5,058 Expert 4TB
The "imageid" there is the ID of an image from your database, which the "filename.php" script takes and retrieves, returning it to the <img>
Mar 10 '10 #9
i cant store my image to database
please check the following code is correct or not because that code did not return any
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $connection=mysql_connect("localhost","root","mypassword");
  3. if(!$connection)
  4. {
  6. die('Not connect'.mysql_error());
  7. }
  8. // Create table
  9. mysql_select_db("my_db", $connection);
  10. $sql = "CREATE TABLE myimage
  11. (
  12. id int(5) NOT NULL auto_increment. ,
  13. image longblob NOT NULL,
  14. description char(50)
  15. )";
  16. mysql_query($sql,$connection);
  17. echo "table Created";
  18. mysql_select_db("my_db",$connection);
  20.     $imgdata = file_get_contents($_FILES['fimage']['tmp_name']);
  21.     $fpo      = fopen($imgdata, 'r'); 
  22.     $imgdata1 = fread($fpo, filesize($imgdata));
  23.     $imgdata1 = mysql_real_escape_string($imgdata1 );
  25. $sql = "INSERT INTO myimage (image,description) VALUES 
  26. ('$_POST['fdescription']','$imgdata1')";
  27. if (!mysql_query($sql,$connection))
  28. {
  30.  die('Error'.mysql_error());
  31. }
  32. print ="Description;$fdescription";
  33. mysql_close($connection);
  34. ?>
Mar 10 '10 #10
5,058 Expert 4TB
What were you trying to achieve?
What exactly were you expecting it to return?
What did it actually return?
Did you get any errors?

What does the form that goes with this code look like?

And why are you so intent on putting the images into the database, rather than on the file-system?
Mar 10 '10 #11
Dear Atli

I have stored images in my database that will display in mysql as like my attachment.. here what is meaning [BLOB - 0 B].
There are 4 images were stored.
Attached Images
File Type: jpg blob.JPG (16.3 KB, 316 views)
Mar 10 '10 #12
Umm for one buddy your creating the table each time you run that script I'm surprised MySQL didn't kick you in the nads for that, if your going to do that at least use IF NOT EXISTS syntax so MySQL doesn't suffer.

Your reselecting the currently selected database, why?

In the SQL for CREATE TABLE why is there a period after auto_increment?

Why are you trying to open a string as a file?

I know PHP closes files automatically but you should still do it manually for good coding practice.

Expand|Select|Wrap|Line Numbers
  1. VALUES ('$_POST['fdescription']','$imgdata1')";
The variable should be enclosed with curly brackets
Expand|Select|Wrap|Line Numbers
  1. VALUES ('{$_POST['fdescription']}','$imgdata1')";
Also my best advice check out w3schools.com, tizag.com, and tutorialspoint.com and learn PHP before messing around with it especially before messing with files delete the wrong one and windows dies of course windows would reject your attempt ( or should ) but still bad idea.

Oh and if <img src="filename.php?id="imageid"> is actually your code check out w3school's excellent HTML tutorial while your there.

And one final person comment, where ever you found this tutorial don't go back there ... ever.

And I apologize if I was rude, but you need to understand PHP before jumping that far into it, believe me I know.
Mar 11 '10 #13
ok ok thanks for your comments.Now i have learned something about php.
Mar 12 '10 #14
hi philip

i have one question, i have more than one images in my mysql database.
i want to display all images color vise. when am selected color in my combo box drop down in my web page.
For Example i have selected red color means only display the images where the images name contain red.
Please find out my attachment. that is my web page.

Thank you
Attached Images
File Type: jpg select.JPG (5.0 KB, 243 views)
Mar 12 '10 #15
I'd say you'd need to add a "color" field to the MySQL table otherwise you'd have to do some pretty hardcore hacking on the images to get there overall color. Then just use a WHERE cause in the SQL "WHERE `color`='red'".
Mar 12 '10 #16
I have color field in my database. but i don't know how can i retrieve that values in my combo box.

Thank you
Mar 12 '10 #17
Please help me. I have color field in my database but i don't know how can i retrieve that colors in my combo box.
give any idea to me.

Thank you
Mar 12 '10 #18
5,058 Expert 4TB
It's fairly simple. You query the database for the colors, loop through them, and print each color as an <option> for your <select> box. - If you read through any one of the literally hundreds of beginner PHP/MySQL tutorials out there, this should be no problem.

Try it. If you run into problems, post them here. (With proper explanations!)
Mar 12 '10 #19
Ok thank you for your help. i will try that.
Mar 12 '10 #20

Post your reply

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

Similar topics

3 posts views Thread by adam | last post: by
7 posts views Thread by eholz1 | last post: by
4 posts views Thread by foss | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.