How to display image from mysql

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • selvasoft
    New Member
    • Mar 2010
    • 34

    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 Files
  • Atli
    Recognized Expert Expert
    • Nov 2006
    • 5062

    #2
    Hey.

    What have you done so far?
    And what are you having trouble with?

    Comment

    • selvasoft
      New Member
      • Mar 2010
      • 34

      #3
      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.

      Comment

      • selvasoft
        New Member
        • Mar 2010
        • 34

        #4
        hey
        Am doing that manual not writing any queries

        Comment

        • Atli
          Recognized Expert Expert
          • Nov 2006
          • 5062

          #5
          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.

          Originally posted by selvasoft
          Am doing that manual not writing any queries
          What do you mean? If you plan on interacting with a MySQL database, you will have to write a few queries.

          Comment

          • selvasoft
            New Member
            • Mar 2010
            • 34

            #6
            Code:
            <?
            $dbcnx = @mysql_connect("localhost", "root", "red");
            
            if (!$dbcnx)
            {
            echo( "connection to database server failed!" );
            exit();
            }
            
            if (! @mysql_select_db("my_db") )
            {
            echo( "Image Database Not Available!" );
            exit();
            }
            
            $img = $_REQUEST["img"];
            $result = @mysql_query("SELECT * FROM images WHERE imgid=" . $img . "");
            
            if (!$result)
            {
            echo("Error performing query: " . mysql_error() . "");
            exit();
            }
            
            while ( $row = mysql_fetch_array($result) )
            {
            $imgid = $row["imgid"];
            }
            
            echo base64_decode($encodeddata);
            ?>


            this is not work what problem in that
            Last edited by Atli; Mar 9 '10, 10:38 AM. Reason: Added [code] tags.

            Comment

            • Atli
              Recognized Expert Expert
              • Nov 2006
              • 5062

              #7
              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.

              Comment

              • selvasoft
                New Member
                • Mar 2010
                • 34

                #8
                dear Atli

                Please help me. When we want to display our images from database that time we start our script like
                <img src="filename.p hp?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.

                Comment

                • Atli
                  Recognized Expert Expert
                  • Nov 2006
                  • 5062

                  #9
                  The "imageid" there is the ID of an image from your database, which the "filename.p hp" script takes and retrieves, returning it to the <img>

                  Comment

                  • selvasoft
                    New Member
                    • Mar 2010
                    • 34

                    #10
                    i cant store my image to database
                    please check the following code is correct or not because that code did not return any
                    Code:
                    <?php
                    $connection=mysql_connect("localhost","root","mypassword");
                    if(!$connection)
                    {
                    
                    die('Not connect'.mysql_error());
                    }
                    // Create table
                    mysql_select_db("my_db", $connection);
                    $sql = "CREATE TABLE myimage
                    (
                    id int(5) NOT NULL auto_increment. ,
                    image longblob NOT NULL,
                    description char(50)
                    )";
                    mysql_query($sql,$connection);
                    echo "table Created";
                    mysql_select_db("my_db",$connection);
                    
                        $imgdata = file_get_contents($_FILES['fimage']['tmp_name']);
                        $fpo      = fopen($imgdata, 'r'); 
                        $imgdata1 = fread($fpo, filesize($imgdata));
                        $imgdata1 = mysql_real_escape_string($imgdata1 );
                      
                    $sql = "INSERT INTO myimage (image,description) VALUES 
                    ('$_POST['fdescription']','$imgdata1')";
                    if (!mysql_query($sql,$connection))
                    {
                    
                     die('Error'.mysql_error());
                    }
                    print ="Description;$fdescription";
                    mysql_close($connection);
                    ?>
                    Last edited by Atli; Mar 10 '10, 08:42 AM. Reason: Added [code] tags.

                    Comment

                    • Atli
                      Recognized Expert Expert
                      • Nov 2006
                      • 5062

                      #11
                      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?

                      Comment

                      • selvasoft
                        New Member
                        • Mar 2010
                        • 34

                        #12
                        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 Files

                        Comment

                        • philipwayne
                          New Member
                          • Mar 2010
                          • 50

                          #13
                          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.

                          And
                          Code:
                          VALUES ('$_POST['fdescription']','$imgdata1')";
                          The variable should be enclosed with curly brackets
                          Code:
                          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.p hp?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.

                          Comment

                          • selvasoft
                            New Member
                            • Mar 2010
                            • 34

                            #14
                            ok ok thanks for your comments.Now i have learned something about php.

                            Comment

                            • selvasoft
                              New Member
                              • Mar 2010
                              • 34

                              #15
                              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 Files

                              Comment

                              Working...