Help - Search - Members - Calendar
Full Version: What's the best way to store Images/URL's in mySQL
Invision Power Services > Community Forums > Community Web Design and Coding
Lemon Head
Hi,
Being a newbie to PHP/mySQL, can someone give me an idea how to store and retrieve images and URL's in a mySQL database? Would I be correct in thinking that URL's are stored the same as other text items and then parsed to display in the output page as active URL links?

When it comes to images, is it best to store them in a series of folders on my webspace and just store the Image URL's in the database as opposed to the storing the actual images themselves?

I'd appreciate any advice or hints.
FormulaC
i store the image file on the webserver in a directory of my choice. i edit the name so it doesnt have duplicates. then i save filename and details in the database.

you can save the file to the database, but then your database becomes packed, as ive experienced on a vbb board i am a techie admin at. (im cursing it too much)
Antony
I would suggest storing Images on the Server. Thats what th filesystem is for.
Nash12
QUOTE(Swords @ May 5 2005, 09:59 AM) *
I would suggest storing Images on the Server. Thats what th filesystem is for.


Thats right, if the images are stored in the database, it will become huge ... and slow. wink.gif
.Reko
If you still want to do it, look up BLOB and make sure you escape it properly.
Lemon Head
Thanks guys much appreciated. Can anyone recommend a tutorial on how to print to the screen or use stored URL's for webpages and images? This would be great. I want to 'parse' the image URL's on a php page so that the visitor would see the image when visiting the page, and not see the link to the image. Similary, is it just a matter of printing the URL to the screen to provide a clickable URL for a visitor to use (eg. to link to another website)?
Michael K.
You would just use HTML. Something like:

CODE
<?php
  
      $conn = mysql_connect( 'localhost', 'username', 'password');
      mysql_select_db('database', $conn);
            
      $query = mysql_query("SELECT img_url, url FROM table");
      $row = mysql_fetch_array($query, MYSQL_ASSOC);
  
      echo "<a href='{$row['url']}'>Click me!</a>";
      echo "<img src='{$row['img_url']}' alt='' />";
  
  ?>
Lemon Head
Thanks Gornakle, much apreciated.

Is the MYSQL_ASSOC a PHP or mySQL command or a variable?

Thanks again!
Brendon Koz
CODE
$row = mysql_fetch_array($query, MYSQL_ASSOC);

mysql_fetch_array() is a PHP command that can take on one, or two parameters. If the second, optional parameter is not used, the function will return an array with both associative values, and numbered values, where the index 0 would be the first element found (img_url, in this example) that could also be indexed by using row['img_url']. In this example, there would be two elements in the array on each call because the query is only asking for two elements from the database at any given time.

However, if you are to use the MYSQL_ASSOC parameter with the mysql_fetch_array() function, you might as well just use mysql_fetch_assoc() which does the same thing as using mysql_fetch_array but without requiring the parameter...it's a little less confusing if another programmer were to look at your code in the future...and is not that much slower.

HTH
Lemon Head
Thanks for the explanation, much appreciated. In fact both the image url and standard url would have been seperate queries. At the moment we are setting up the database and running these test queries to get them right and to make sure we can extract the data which is mostly data such episode titles but will also include photo's and links to crew websites.
Brendon Koz
Goodluck with your endeavor. Perhaps you'll let us take a look at it when you're done.
Lemon Head
Certainly! I just hope that I can do justice to the amount of database work my friend is putting into it!
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2009 Invision Power Services, Inc.