clarky08 Posted April 4, 2008 Share Posted April 4, 2008 Hi I'm relatively new to PHP and I was just wondering if someone could help me with a problem i'm having. I have the following code: <?php function myEscape($string) { $con = mysql_connect("localhost"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("test", $con); $var = get_magic_quotes_gpc() ? stripslashes($string) : $string; return mysql_real_escape_string($var); } $cid = myEscape($_GET['cid']); $cn = myEscape($_GET['cn']); $cm = myEscape($_GET['cm']); $sql = "SELECT * FROM `clubs` WHERE `ClubID`='$cid' AND `ClubName`='$cn' AND `ClubManager`='$cm'"; $result = mysql_query($sql) OR DIE (mysql_error()); if (mysql_num_rows($result) > 0) { while ($row = mysql_fetch_array($result)) { echo "<table border='1'> <tr> <th>Firstname</th> <th>Lastname</th> </tr>"; echo "<tr>"; echo "<td>" . $row['ClubName'] . "</td>"; echo "<td>" . $row['ClubManager'] . "</td>"; echo "</tr>"; } } else { echo "Houston? We have a problem."; } ?> What this is doing for me is getting information from a database using an id that has been set. But i want to be able to have an image for each club too. So my questions are: 1) How do you save an image in a database? 2) How do you access the image to display it? All help would be much appreciated. Thanks Colm. Link to comment https://forums.phpfreaks.com/topic/99529-php-mysql-images/ Share on other sites More sharing options...
Yesideez Posted April 4, 2008 Share Posted April 4, 2008 Personally I'd have the images in a folder and reference the image going by the ID number or maybe another field if some records share the same image - my method as I've not played with storing image data inside databases. Link to comment https://forums.phpfreaks.com/topic/99529-php-mysql-images/#findComment-509152 Share on other sites More sharing options...
clarky08 Posted April 4, 2008 Author Share Posted April 4, 2008 How would I do that?? What type should the field be? Link to comment https://forums.phpfreaks.com/topic/99529-php-mysql-images/#findComment-509180 Share on other sites More sharing options...
craygo Posted April 4, 2008 Share Posted April 4, 2008 Are you going to have one or multiple images per product, user or whatever you want to show?? Link to comment https://forums.phpfreaks.com/topic/99529-php-mysql-images/#findComment-509182 Share on other sites More sharing options...
clarky08 Posted April 4, 2008 Author Share Posted April 4, 2008 Just the one for each club Link to comment https://forums.phpfreaks.com/topic/99529-php-mysql-images/#findComment-509186 Share on other sites More sharing options...
Yesideez Posted April 4, 2008 Share Posted April 4, 2008 If each club has its own unique identifier (ie. clubid in the database) then you could call the picture up with something like this: <img src="clublogos/clud<?=$clubid?>.jpg'> Link to comment https://forums.phpfreaks.com/topic/99529-php-mysql-images/#findComment-509190 Share on other sites More sharing options...
clarky08 Posted April 4, 2008 Author Share Posted April 4, 2008 oh right! so i just save the name in the table and then it should work from there? And do I have the images folder in the www directory? Link to comment https://forums.phpfreaks.com/topic/99529-php-mysql-images/#findComment-509193 Share on other sites More sharing options...
craygo Posted April 4, 2008 Share Posted April 4, 2008 you could also store all your images in one place as yesideez said lets say all the images are in clublogos you can add a field to table for the image name then call the image when you query the club row <img src="clublogos/<?=$row['imagename']?>" /> Pretty much like yesideez example but you would have to worry about naming the pictures in the folder. Although his way would keep from duplicate file names. So each has it's ups and downs. Ray Link to comment https://forums.phpfreaks.com/topic/99529-php-mysql-images/#findComment-509194 Share on other sites More sharing options...
Yesideez Posted April 4, 2008 Share Posted April 4, 2008 Just as an extra note I forgot to add in my post, you'd name the images in the "clublogos" folder something like this: club1.jpg club2.jpg club3.jpg and so-on. (I made a typo in my example and used "clud" instead of "club") The only downside to this is that you are restricted to using one type of file, in this case JPEG. Using craygo's method you can use whatever image format you want as you're specifying the entire filename. Link to comment https://forums.phpfreaks.com/topic/99529-php-mysql-images/#findComment-509197 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.