Loading Image from mysql database

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
miszt
Forum Newbie
Posts: 5
Joined: Fri Dec 14, 2007 7:14 am

Loading Image from mysql database

Post by miszt »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


I've been trying to extract images from a database to display in a page using this...

Code: Select all

<?php
mysql_connect('****','****','****') or die("Unable to connect to SQL server");
@mysql_select_db('fomicidae_species') or die("Unable to select database");
$result=mysql_query('SELECT image FROM species WHERE index=$PicNum') or die ("Can't perform Query");
$row=mysql_fetch_object($result);
Header("Content-type: image/jpeg");
echo $row->Image;
?>
but I keep getting Cant Perform Query, obviously theres somthing wrong with the SQL, but I cant work out what? As it all seems correct. Even using diffrent php code (without sql errors) i've not been able to get the image to display, so if anyone has a quick way of doing this, I'd b very gratefull!!

image = field
species = table
index = index (being taken from... echo "<IMG SRC=\"getimage.php?PicNum=$row->PicNum\">"; on the main page)

I want to take the corrosponding blob out of mysql, depending on the index/picnum that is sent to the page, then show it as a jpeg image

I made this using an example page, i've tried several but not been able to get it working

(mysql5/php5)


Thanks from a nooooob! :-)


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
Last edited by miszt on Fri Dec 14, 2007 7:36 am, edited 1 time in total.
User avatar
waradmin
Forum Contributor
Posts: 240
Joined: Fri Nov 04, 2005 2:57 pm

Post by waradmin »

Code: Select all

<?php
$DBHOST = "localhost";      //mysql host name
$DBUSER = "USERNAME";               //database username
$DBPASS = "PASS";               //database password
$DBNAME = "DB";               //database name
mysql_connect($DBHOST, $DBUSER, $DBPASS) or die(mysql_error());
mysql_select_db($DBNAME) or die(mysql_error());

$result = mysql_query("SELECT images FROM species WHERE index='$PicNum'") or die(mysql_error());
$row = mysql_fetch_array($result);

echo $row['images'];
mysql_close();
?>
miszt
Forum Newbie
Posts: 5
Joined: Fri Dec 14, 2007 7:14 am

Post by miszt »

cheers waradmin, the echo $row[] is somthing I tried before, but it kept giving me random stuff, still getting an sql error tho lol but i think i see what the problem is :)
miszt
Forum Newbie
Posts: 5
Joined: Fri Dec 14, 2007 7:14 am

Post by miszt »

arg, I cant find the error :(

$query = "SELECT Image FROM species WHERE index=2";

$result = mysql_query($query) or die(mysql_error());

keeps giving me


You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'index=2' at line 1
User avatar
Chalks
Forum Contributor
Posts: 447
Joined: Thu Jul 12, 2007 7:55 am
Location: Indiana

Post by Chalks »

miszt wrote:arg, I cant find the error :(

$query = "SELECT Image FROM species WHERE index=2";

$result = mysql_query($query) or die(mysql_error());

keeps giving me


You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'index=2' at line 1
"Image" is capitalized... shouldn't it be lowercase (looking at your first post)?

Edit: or maybe it should be

Code: Select all

$query = "SELECT image FROM species WHERE index='2'";
miszt
Forum Newbie
Posts: 5
Joined: Fri Dec 14, 2007 7:14 am

Post by miszt »

image is capitalized in my database, and ur not supposed to put single quotes around numbers in a query...guess this is an sql q? now rather than php
miszt
Forum Newbie
Posts: 5
Joined: Fri Dec 14, 2007 7:14 am

Post by miszt »

ah i've been very stupid! lol I shuldnt have a field called Index as its used for other things in SQL, changed and sorted :) thank you all!
Post Reply