B-knotty Posted April 21, 2007 Share Posted April 21, 2007 So I've managed to upload my images into a database, but I have no idea how to get them out. here's the source for my gallery upload http://rafb.net/p/B4dI3R78.html I just took that off a site and then tried to incorporate it into my own locally hosted site. I can't work out how to get the images out of the database on another script. Can someone please help? Quote Link to comment Share on other sites More sharing options...
keeB Posted April 21, 2007 Share Posted April 21, 2007 http://yoursite/somepage.php?gim==1 Quote Link to comment Share on other sites More sharing options...
B-knotty Posted April 21, 2007 Author Share Posted April 21, 2007 I'm really sorry but I still don't understand, don't I need to query the database to get them out? I'm still pretty new it this so sorry if this is awkward. Quote Link to comment Share on other sites More sharing options...
keeB Posted April 21, 2007 Share Posted April 21, 2007 If you took the code directly from that site, it's right here: <?php if ($_REQUEST[gim] == 1) { header("Content-type: image/jpeg"); print $bytes; exit (); } ?> The 2 relevant parts are: 1. setting the header to the correct content-type, and 2. printing the $bytes, which correlates to the field in your database where the image is stored. That being said, I think uploading images to a database is wrong, and that you should store the files on your file system, with the full or relative paths in your database. Quote Link to comment Share on other sites More sharing options...
B-knotty Posted April 21, 2007 Author Share Posted April 21, 2007 Thanks so much for the help so far, I've managed to get it outputting the image now. However I've got two problems I need to sort of with the script then I'll be happy with it. First I need it to loop and display all of the pictures in the database and not just one for this I assumed I'd need some sort of loops to do this. <?php while ($row = mysql_fetch_assoc($result)) { ?> <img src=?gim=1 width=144><br> <b> <?= $title ?> <?php } ?> but it doesn't seem to like it. Second I need to change the log in to the database so it points to a different file with the log in details, this is for obvious security reasons. Normally I just but and include at the top of the script like this: <?php include('includes/db.php'); //Include the database script that holds information used to access that databse ?> that points to the location of the file with the log in details that looks like this. <?php $db_link = mysql_connect('localhost', 'Knotty' , 'letmein'); if (!db_link) { echo 'error connnecting to database.'; exit; } if (!mysql_select_db('rudeboy',$db_link)) { echo 'error connnecting to database.'; exit; } ?> but when I remove the log in part from the top of that script it can't log into the database, what am I doing wrong? again thanks so much for the help. Quote Link to comment Share on other sites More sharing options...
B-knotty Posted April 22, 2007 Author Share Posted April 22, 2007 Can nobody help? Quote Link to comment Share on other sites More sharing options...
boo_lolly Posted April 22, 2007 Share Posted April 22, 2007 i think you're using the wrong terminology. you don't upload images to a database, you upload them to a harddrive. what gets stored in a database is the name of the image or file. you can then dynamically call upon these files by accessing your database, pulling all the image names out of your database, then populating the browser with the images by putting the path to the images themselves before the image name. something like this: <?php /*connect to database*/ $db_conn = mysql_connect('xxx', 'xxx', 'xxx'); if(!$db_conn){ die('Could not connect to database.'); } /*select database*/ mysql_select_db('your_database', $db_conn) OR die('Could not find database.'); $sql = " SELECT image_names FROM your_table "; $query = mysql_query($sql); while($row = mysql_fetch_array($query)){ echo "<img src=\"/path/to/images/{$row['image_names']}\"><br />\n"; } ?> does that help? to explain what keeB was talking about, imagine if you have the same image table in your database, and it looks something like: +-------------------------------------+ | your_table | +-----+-------------------------------+ | id | image_names | +-----+-------------------------------+ | 1 | family.jpg | +-----+-------------------------------+ | 2 | dad.gif | +-----+-------------------------------+ | 3 | sister.jpg | +-----+-------------------------------+ | 4 | friends.jpg | +-----+-------------------------------+ you can use the $_GET method to dynamically display the images to your browser. so let's say your address bar has: www.yoursite.com/?img=3 you can populate image 3 by doing something like this: <?php /*connect to database*/ $db_conn = mysql_connect('xxx', 'xxx', 'xxx'); if(!$db_conn){ die('Could not connect to database.'); /*select database*/ mysql_select_db('your_database', $db_conn) OR die('Could not find database.'); if(isset($_GET['img'])){ $id = $_GET['img']; }else{ $id = 1; } $sql = " SELECT image_names FROM your_table WHERE id = '{$id}' "; $query = mysql_query($sql); while($row = mysql_fetch_array($query)){ echo "<img src=\"/path/to/images/{$row['image_names']}\"><br />\n"; } ?> does that make sense? Quote Link to comment Share on other sites More sharing options...
B-knotty Posted April 22, 2007 Author Share Posted April 22, 2007 The script that I was using uploads the image to the database though CREATE TABLE gallery ( autoid int unsigned not null auto_increment primary key, title varchar(100), imgdata longblob ); I used the to create the table, when ever I do a SELECT * FROM gallery in the MySQL console it starts to spit out loads of %&7838247£^% so I assumed thats how it stored the image in the database. I'm also a bit confused other the path to the image part as well. Thanks for you're help so far, i'm still quite new at this an I need to get this problem sorted tonight. Quote Link to comment Share on other sites More sharing options...
boo_lolly Posted April 22, 2007 Share Posted April 22, 2007 no worries bro, we can get you squared away. first, images are not stored in a database. only ASCII is stored in a database. how are you uploading images, how your you printing images to your browser? can we see some code and your upload form? Quote Link to comment Share on other sites More sharing options...
B-knotty Posted April 22, 2007 Author Share Posted April 22, 2007 Okay, that makes a lot more sense now here is the code for the gallery upload <?php // Connect to database $errmsg = ""; if (! @mysql_connect("localhost","Knotty","letmein")) { $errmsg = "Cannot connect to database"; } @mysql_select_db("rudeboy"); if ($_REQUEST[completed] == 1) { // Need to add - check for large upload. Otherwise the code // will just duplicate old file ;-) // ALSO - note that latest.img must be public write and in a // live appliaction should be in another (safe!) directory. move_uploaded_file($_FILES['imagefile']['tmp_name'],"latest.img"); $instr = fopen("latest.img","rb"); $image = addslashes(fread($instr,filesize("latest.img"))); if (strlen($instr) < 149000) { mysql_query ("insert into pix (title, imgdata) values (\"". $_REQUEST[whatsit]. "\", \"". $image. "\")"); } else { $errmsg = "Too large!"; } } // Find out about latest image $gotten = @mysql_query("select * from pix order by pid desc limit 1"); if ($row = @mysql_fetch_assoc($gotten)) { $title = htmlspecialchars($row[title]); $bytes = $row[imgdata]; } else { $errmsg = "There is no image in the database yet"; $title = "no database image available"; } // If this is the image request, send out the image if ($_REQUEST[gim] == 1) { header("Content-type: image/jpeg"); print $bytes; exit (); } ?> This part of it works fine, although I need to change the way it connects to the database, I don't like the idea of having login details on each page, as I mentioned earlier ideally I want to have an include at the top of the script that points to location of the login details, this would also mean that I don't have to change all the script if I changed any of the login information. So the database connection part at the top need to be changed to include('includes/db.php'); which points to this script with all the login details. <?php $db_link = mysql_connect('localhost', 'Knotty' , 'letmein'); if (!db_link) { echo 'error connnecting to database.'; exit; } if (!mysql_select_db('rudeboy',$db_link)) { echo 'error connnecting to database.'; exit; } ?> After the image details have been stored in the database, I would like to be able to display all the images on another page by simply querying the database and then looping through all of it. something a bit like this. <?php $query = 'SELECT * FROM pix' $result = mysql_query($query, $db_link); while ($row = mysql_fetch_assoc($result)) { ?> <img src=?gim=1 width=144> <?= $title ?> <?php } ?> but I've had no luck solving either of these two problems Thanks for the help! Quote Link to comment Share on other sites More sharing options...
boo_lolly Posted April 23, 2007 Share Posted April 23, 2007 where did you get this code from? Quote Link to comment Share on other sites More sharing options...
HaLo2FrEeEk Posted April 23, 2007 Share Posted April 23, 2007 Boo_lolly, you know you're wrong, don't you. Images can be stored as a blob in a mysql database. Quote Link to comment Share on other sites More sharing options...
boo_lolly Posted April 23, 2007 Share Posted April 23, 2007 Boo_lolly, you know you're wrong, don't you. Images can be stored as a blob in a mysql database. after doing some research when the word `longblob` looked unfamiliar, i found that i was wrong. a blob is basically a string of ascii characters that represent information about the image like pixel-location and color/contrast. to be honest, i should have assumed this was possible between the way a computer handles data and how robust mysql is. so, i'd imagine that some special procedure is involved in storing an image as a blob in a database, and retrieving/converting it back into an image. or maybe there isn't since an image is stored on a computer in that format in the first place. sorry to mislead, knotty. if anything, i've shown you a different way of storing images into a database =P. i'm going to do more research on what you're going for and get back to this thread. Quote Link to comment Share on other sites More sharing options...
B-knotty Posted April 23, 2007 Author Share Posted April 23, 2007 hah it's okay, learn something new everyday huh? Still no one can help with the connection to the database at least? Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.