|
Custom Search
| |
|
Inside This Article
Implement file directory based image management Unlike Northwind in Access database where photos are stored in database tables, our MySQL northwind database will store employees photos and categories pictures in file directories. Only image file names (eg. seafood.gif) are saved in the Photo column. The actual image files are stored in file system. Photos in Employees table The table below lists data of a few columns in employees table. It's a very simple exercises of PHP with MySQL but it demonstrates how easy it is to code when we only need to manage image file names in database and save the actual images in file directories. This is how to do it:
<?
// Assume you have define your mysql database parameters // Connect to mysql server $link = mysql_connect($host, $user, $pass) or die("Could not connect to database"); // Select the database mysql_select_db($db_name, $link) or die("Could not select database"); // Select employees $sql = "select EmployeeID, FirstName, LastName, Photo from employees"; // Fetch the result set $result = mysql_query($sql, $link); // Loop thru each employee in the result set and build html table row while($row = mysql_fetch_array($result, MYSQL_ASSOC)) { $emp_id = $row["EmployeeID"]; $fname = $row["FirstName"]; $lname = $row["LastName"]; $photo_filename = $row["Photo"]; // Concatenate to create html table row $tr .= "<tr><td align=middle valign=top>". $emp_id. "</td>". "<td valign=top>". $fname. "</td>". "<td valign=top>". $lname. "</td>". "<td valign=top>". $photo. "</td>". "<td valign=top><img src='/img/article/northwind/". $photo_filename. "'></td></tr>"; } ?> <br> <table cellpadding=5 cellspacing=0 border=1> <tr> <th>EmployeeID</th> <th>FirstName</th> <th>LastName</th> <th>Image file name</th> <th>Photo</th> </tr> <?=$tr?> </table> Here is what the code produced:
Photos in Categories table:
By using a similar code, we can display pictures for product categories.
<?
// Display categoris pictures $sql = "select CategoryID, CategoryName, Picture from ". DB_NAME_NORTHWIND. ".categories"; $result = mysql_query($sql, $link); while($cat_row = mysql_fetch_array($result, MYSQL_ASSOC)) { $cat_id = $cat_row["CategoryID"]; $cat_name = $cat_row["CategoryName"]; $photo = $cat_row["Picture"]; $cat_tr .= "<tr><td align=middle valign=top>". $cat_id. "</td>". "<td valign=top>". $cat_name. "</td>". "<td valign=top>". $photo. "</td>". "<td valign=top><img src='/img/article/northwind/". $photo. "'></td>". "</tr>"; } ?> <p> <table cellpadding=5 cellspacing=0 border=1> <tr> <th>CategoryID</th> <th>CatgoryName</th> <th>Image file name</th> <th>Photo</th> </tr> <?=$cat_tr?> </table>
Here ends this article series. If you think I have missed anything that is better to be included, please let me know.
Happy Coding!
|
Copyright © 2024 GeeksEngine.com. All Rights Reserved. This website is hosted by HostGator. No portion may be reproduced without my written permission. Software and hardware names mentioned on this site are registered trademarks of their respective companies. Should any right be infringed, it is totally unintentional. Drop me an email and I will promptly and gladly rectify it. |
Home | Feedback | Terms of Use | Privacy Policy |