Images/PHP and MySql? How does it work?

Discussion in 'PHP' started by scm22ri, Aug 28, 2012.

  1. #1
    scm22ri, Aug 28, 2012 IP
  2. HostPlanz

    HostPlanz Well-Known Member

    Messages:
    449
    Likes Received:
    34
    Best Answers:
    4
    Trophy Points:
    130
    #2
    The best way is storing the images on your server and storing a link to the image in your MYSQL.
    For example I will store 3 images in a folder named "images". The name of each image is "image1.jpg", "image2.jpg" and "image3.jpg".

    /Images
    >image1.jpg
    >image2.jpg
    >image3.jpg


    The in the MYSQL database, I will store a link to the image using a varchar field for example
    +------------+-------------------+
    | Image_ID | Image_Link |
    +------------+-------------------+
    | 0001 | /image/image1.jpg |
    | 0002 | /image/image2.jpg |
    | 0003 | /image/image3.jpg |
    +---------------+-------------------+

    And using php echo the Image_Link path in my html based on the page requested
    
    <?php
    $Image_Link = //the link you stored in the MYSQL database
    $image1 = '<img src="www.example.com'.$Image_Link.'" />';
    ?>
    
    PHP:
     
    HostPlanz, Aug 28, 2012 IP
  3. HostPlanz

    HostPlanz Well-Known Member

    Messages:
    449
    Likes Received:
    34
    Best Answers:
    4
    Trophy Points:
    130
    #3
    why did you give me negative reputation?
     
    HostPlanz, Aug 28, 2012 IP
  4. phpking

    phpking Peon

    Messages:
    24
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    thanks hosplaz
     
    phpking, Aug 30, 2012 IP
  5. jadexe

    jadexe Member

    Messages:
    32
    Likes Received:
    0
    Best Answers:
    1
    Trophy Points:
    26
    #5
    It is possible to store images in mysql as binary but I doubt that's what they are doing. I would just do what HostPlantz suggested.
     
    jadexe, Aug 30, 2012 IP
  6. kalraharsh

    kalraharsh Member

    Messages:
    6
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    31
    #6
    it would be better if you just store the name of the image in the database table instead of the whole path . cause if you ever in future change your image directory path, then you'll have to change everything in the database..
    so a better code would be
    <?php
    $Image_name = //the name you stored in the MYSQL database
    $Image_path=//the path
    $image1 = '<img src="www.example.com/'.$Image_path."/".$Image_name.'" />';
    ?>
     
    kalraharsh, Sep 2, 2012 IP