Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums Other Displaying images from a MySQL Database?

  • This topic is empty.
Viewing 7 posts - 1 through 7 (of 7 total)
  • Author
    Posts
  • #25302
    kipperc
    Member

    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.

    #59876
    TheDoc
    Member

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

    #59877
    kipperc
    Member

    That’s correct.

    #59878
    TheDoc
    Member

    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.

    #59886
    kipperc
    Member

    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.

    #60824

    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.

    #60836
    TheDoc
    Member

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

Viewing 7 posts - 1 through 7 (of 7 total)
  • The forum ‘Other’ is closed to new topics and replies.