Re: [PHP] displaying image from MySQL DB using HTML/PHP
2007. 03. 14, szerda keltezéssel 19.30-kor Børge Holen ezt írta: On Tuesday 13 March 2007 22:09, Tijnema ! wrote: On 3/13/07, Bruce Gilbert [EMAIL PROTECTED] wrote: On 3/13/07, Tijnema ! [EMAIL PROTECTED] wrote: So you just need to set the content-type and output add this to the bottom of the script: header(Content-Type: .$encodeddata); echo $title; If i understand you right. Tijnema Thanks, I changed the code around some and now have: [php] ?php //check for validity of user $db_name=bruceg_mailinglist; $table_name =image_holder; $connection = @mysql_connect(db_address, uasername, password) or die (mysql_error()); $db = @mysql_select_db($db_name, $connection) or die (mysql_error()); $img = $_REQUEST[img]; $result = @mysql_query(SELECT * FROM image_holder WHERE id= . $img . ); if (!$result) { echo(Error performing query: . mysql_error() . ); exit(); } while ( $row = @mysql_fetch_array($result) ) { $imgid = $row[id]; header(Content-Type: .$encodeddata); echo $title;} ? [/php] and in the HTML centerimg src=image.php?id=1 width=200 border=1 alt=/center but I am getting a MySQL error Error performing query: 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 '' at line 1 -- ::Bruce:: You changed your html code, you have id=1, and in your PHP code you are requesting img, so change centerimg src=image.php?id=1 width=200 border=1 alt=/center to centerimg src=image.php?img=1 width=200 border=1 alt=/center But i must also say, it is NOT safe to input data from ?img= directly into your database, someone could do a SQL injection right away with this code!. He's not using image.php to insert. Earlier he mentioned using phpmyadmin to insert the image, that was the way I used too. First learn to display an image, this way its easier to know if any upload script you make up later is working correctly. evil code can be injected into any sort of sql statement. so it is not depending on whether he's using the php for insert or not. greets Zoltán Németh Then about this piece of code while ( $row = @mysql_fetch_array($result) ) { $imgid = $row[id]; header(Content-Type: .$encodeddata); echo $title; } I hope for you that there's only one item with this id, if not, there would come an error again, so a while loop is not needed, and second, now you don't define $encodeddata and $title anymore, try this piece of code instead of the one above: $row = @mysql_fetch_array($result); header(Content-Type: .row['mimetype']); echo $row['filecontents']; ps. Reply to the full PHP list, not just me... -- --- Børge Kennel Arivene http://www.arivene.net --- -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] displaying image from MySQL DB using HTML/PHP
On Tuesday 13 March 2007 22:09, Tijnema ! wrote: On 3/13/07, Bruce Gilbert [EMAIL PROTECTED] wrote: On 3/13/07, Tijnema ! [EMAIL PROTECTED] wrote: So you just need to set the content-type and output add this to the bottom of the script: header(Content-Type: .$encodeddata); echo $title; If i understand you right. Tijnema Thanks, I changed the code around some and now have: [php] ?php //check for validity of user $db_name=bruceg_mailinglist; $table_name =image_holder; $connection = @mysql_connect(db_address, uasername, password) or die (mysql_error()); $db = @mysql_select_db($db_name, $connection) or die (mysql_error()); $img = $_REQUEST[img]; $result = @mysql_query(SELECT * FROM image_holder WHERE id= . $img . ); if (!$result) { echo(Error performing query: . mysql_error() . ); exit(); } while ( $row = @mysql_fetch_array($result) ) { $imgid = $row[id]; header(Content-Type: .$encodeddata); echo $title;} ? [/php] and in the HTML centerimg src=image.php?id=1 width=200 border=1 alt=/center but I am getting a MySQL error Error performing query: 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 '' at line 1 -- ::Bruce:: You changed your html code, you have id=1, and in your PHP code you are requesting img, so change centerimg src=image.php?id=1 width=200 border=1 alt=/center to centerimg src=image.php?img=1 width=200 border=1 alt=/center But i must also say, it is NOT safe to input data from ?img= directly into your database, someone could do a SQL injection right away with this code!. He's not using image.php to insert. Earlier he mentioned using phpmyadmin to insert the image, that was the way I used too. First learn to display an image, this way its easier to know if any upload script you make up later is working correctly. Then about this piece of code while ( $row = @mysql_fetch_array($result) ) { $imgid = $row[id]; header(Content-Type: .$encodeddata); echo $title; } I hope for you that there's only one item with this id, if not, there would come an error again, so a while loop is not needed, and second, now you don't define $encodeddata and $title anymore, try this piece of code instead of the one above: $row = @mysql_fetch_array($result); header(Content-Type: .row['mimetype']); echo $row['filecontents']; ps. Reply to the full PHP list, not just me... -- --- Børge Kennel Arivene http://www.arivene.net --- -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] displaying image from MySQL DB using HTML/PHP
Bruce Gilbert wrote: I am having some difficulty getting an image to display on a php that I have added to MySQL DB. Here is what I have tried so far in the MySQL DB I have a table called image_holder and the fields are id,mimename,filecontents...filecontents field is set to a type of blob using PHPMyAdmin and I have uploaded the image in MySQL. In the PHP code I have: [php] ?php $dbhost = 'hostaddress'; $dbuser = 'username'; $dbpass = 'password; $dbcnx = @mysql_connect($dbhost,$dbuser,$dbpass); if (!$dbcnx) { echo( connection to database server failed!); exit(); } if (! @mysql_select_db(image_holder) ) { echo( Image Database Not Available! ); exit(); } DON'T USE REQUEST only use it from where you expect it to be, in this case, $_GET $img = $_REQUEST[img]; I see you calling a table called images not image_holder as stated previously. $result = @mysql_query(SELECT * FROM images WHERE id= . $img . ); if (!$result) { echo(Error performing query: . mysql_error() . ); exit(); } you don't need a while loop here, get rid of it. while ( $row = @mysql_fetch_array($result) ) { $imgid = $row[id]; $encodeddata = $row[mimetype]; $title = $row['filecontents']; } here is the end, but I see no echoing headers or of the data you need to spit out headers for the given type of image and then echo the data and then exit. $result = @mysql_query(SELECT * FROM images WHERE id= . $img . ); if (!$result) { echo(Error performing query: . mysql_error() . ); exit(); } if ( ( $row = @mysql_fetch_array($result) ) === FALSE ) { header(Content-Type: {$row['mimetype']}); echo $row['filecontents']; exit(); } ? and in the HTML code centerimg src=image.php?img=1 width=200 border=1 alt=/center I am probably way off base, so need some help! -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] displaying image from MySQL DB using HTML/PHP
On Tue, March 13, 2007 9:07 pm, Jim Lucas wrote: DON'T USE REQUEST only use it from where you expect it to be, in this case, $_GET Unless you actually WANT your web application to be flexible and allow other users to have links or POST forms to interface with it... It's not like you can trust the data of POST or GET any more or less than the other, really. They both come from the big bad internet world outside, and are equally suspect. I'm just suggesting that not using REQUEST is not an absolute. In this particular scenario, it's hard to imagine why you'd want a JPEG as a response to a POST, of course, so definitely use $_GET here. But if you have a web app that works equally well for other pages to link to, or to send POST data to, using REQUEST is quite nice, imho. -- Some people have a gift link here. Know what I want? I want you to buy a CD from some starving artist. http://cdbaby.com/browse/from/lynch Yeah, I get a buck. So? -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Displaying image from MySql DB
Hi Guys, I have some images stored in my MySql database that I'd like to display on the bottom of my HTML page. However, whenever I attempt to display the images using the follow code: $getPhoto = mysql_fetch_object($result); $Type = $getPhoto-type; Header(Content-type: $Type); $Body = $getPhoto-body; echo $Body; flush(); I get an error that states: Cannot add header information - headers already sent by Now I understand why I'm getting the error. What I'd like to know is how to get around it? Basically what I want is the image in the DB to be displayed like any regular image would be on an HTML page, i.e. img src=imageGoesHere height=x width=x border=x. However, I still need to pass the HTML headers. O From Now 'Till Then, \-Reginald Alex Mullin /\ 212-894-1690 ** This email and any files transmitted with it are confidential and intended solely for the use of the individual or entity to whom they are addressed. If you have received this email in error please notify the postmaster at [EMAIL PROTECTED] www.sothebys.com ** -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php