Grow your CSS skills. Land your dream job.

Displaying images from a MySQL Database?

  • # June 29, 2009 at 1:21 pm

    Recently one of my clients asked me to make a modification to their store locator page so that when a location is selected from a drop down menu, an image of the location is displayed on the page as opposed to the generic address info that is currently displayed.

    Here is the page so you can see what I’m working with. http://www.cowgirlsespresso.com/locator.php

    I don’t have very much experience with MySQL and I’m still learning my way around PHP so the more detailed help i can get the better.

    Thanks.

    # June 29, 2009 at 1:23 pm

    I’m assuming the address is being pulled from the database as well?

    # June 29, 2009 at 1:25 pm

    That’s correct.

    # June 29, 2009 at 1:53 pm

    Okay, you can store images in the database as well, but that will probably eat up space much quicker than you think (depending on if you start using it for things outside of the Store Locator as well).

    What you should do is store all of the images in a folder and then simply reference them in the database by name. I prefer this method as it makes a lot more sense to people that understand folder hierarchy on a server than trying to understand a database.

    # June 29, 2009 at 3:20 pm

    I think that makes sense. Should I just make a new column in the database called "image" and then include URLs to the images in the table rows associated with the locations?

    After that, how do I write the php function to reference the image? And where do I include that code?

    Do you think you may be able to show me how to do that?

    I really appreciate the help.

    # July 16, 2009 at 10:45 am

    This is the way to do it.

    Firstly get together all the addresses of the images that you want to use. Then get together all the information about your database – you will need your database name, username and password. For the purposes of this you can just create a new .php page and open it once. Here is the code for that page.

    Code:
    $dbhost = “localhost”;
    $dbname = “PUT YOUR DATABASE NAME HERE”;
    $dbuser = “PUT YOUR USERNAME HERE”;
    $dbpass = “PUT YOUR PASSWORD HERE”;

    mysql_connect($dbhost, $dbuser, $dbpass) or die(‘>>Error connecting to Database
    ‘);
    mysql_select_db($dbname) or die(‘>>Unable to select Database’);

    mysql_query(“CREATE TABLE images(id INT NOT NULL AUTO_INCREMENT, PRIMARY KEY(id), name VARCHAR(30), address VARCHAR(200))”) or die(‘>>Error Creating Table
    ‘);

    mysql_query(“INSERT INTO images(name, address)
    VALUES(‘PUT THE IMAGE NAME HERE’, ‘PUT THE ADDRESS HERE’)”) or die(‘>>Unable to insert info
    ‘);

    echo “>>Table Created
    “;

    And when you want to retrieve the information use-

    Code:
    $result = mysql_query(“SELECT address FROM images WHERE name = $selected “) or die(‘>>Unable to select Table’);
    $row = mysql_fetch_array($result) or die(‘>>Unable to Fetch data’);

    $address = $row[‘address’];

    echo “

    The variable $selected needs to be set from the drop down box and needs to be the name of the image. This should work, but PM me if there are any problems.

    # July 16, 2009 at 1:33 pm

    Thanks, Tom, I epicly failed at following up on this.

Viewing 7 posts - 1 through 7 (of 7 total)

You must be logged in to reply to this topic.

*May or may not contain any actual "CSS" or "Tricks".