benji87 Posted November 8, 2006 Share Posted November 8, 2006 Hi all im trying to display a news story with an uploaded image. Im new to working with images stored in a database so im not quite sure the best way to script it in different ways at the moment. But what im trying to do is get the result then refresh the headers and print out some html with the results variables embedded into the html as you can see in the script below:[code]<?phprequire_once('includes/db.php');$query=("SELECT * FROM ssrfc_test ORDER BY id DESC LIMIT 0, 1");$getdata = mysql_query($query) or die(mysql_error());if($row = mysql_fetch_assoc($getdata)){$imgtitle = htmlspecialchars($row["imgtitle"]);$imgdata = $row["imgdata"];$id = $row["id"];$title = $row["title"];$message = $row["message"];}else{ $errmsg = "MySql Error!";}header("Content-type: image/jpeg");print '<html><head><title>Untitled Document</title><meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"></head><body><table width="100%" border="1" cellspacing="2" cellpadding="0" bordercolor="#000000"> <tr> <td width="2%">$id</td> <td width="2%">$title</td> <td width="2%">$message</td> <td width="2%">$imgtitle</td> <td width="92%">$imgdata</td> </tr></table></body></html>';exit();?>[/code]All it does is just print the name of the variable. Ive tried a hundred different ways i can think of including echo() and printf() someone please help me out. Thanks Link to comment https://forums.phpfreaks.com/topic/26560-print-with-variables/ Share on other sites More sharing options...
trq Posted November 8, 2006 Share Posted November 8, 2006 Variables are only parsed when contained within double quotes.[code=php:0]print "<html><head><title>Untitled Document</title><meta http-equiv=\"Content-Type\" content=\"text/html; charset=iso-8859-1\"></head><body><table width=\"100%\" border=\"1\" cellspacing=\"2\" cellpadding=\"0\" bordercolor=\"#000000\"> <tr> <td width=\"2%\">$id</td> <td width=\"2%\">$title</td> <td width=\"2%\">$message</td> <td width=\"2%\">$imgtitle</td> <td width=\"92%\">$imgdata</td> </tr></table></body></html>";[/code] Link to comment https://forums.phpfreaks.com/topic/26560-print-with-variables/#findComment-121492 Share on other sites More sharing options...
Round Posted November 8, 2006 Share Posted November 8, 2006 Try:-[code]<html><head><title>Untitled Document</title><meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"></head><body><?phprequire_once('includes/db.php');$query=("SELECT * FROM ssrfc_test ORDER BY id DESC LIMIT 0, 1");$getdata = mysql_query($query) or die(mysql_error());$num = mysql_numrows ($getdata); if ($num !=0) {while ( $row = mysql_fetch_array( $getdata ) ){ $list ="<table width=\"100%\" border=\"1\" cellspacing=\"2\" cellpadding=\"0\" bordercolor=\"#000000\">"; $list .="<tr>"; $list .="<td width=\"2%\">".$row["id"]."</td>"; $list .="<td width=\"2%\">".$row["title"]."</td>"; $list .="<td width=\"2%\">".$row["message"]."</td>"; $list .="<td width=\"2%\">".$row["imgtitle"]."</td>"; $list .="<td width=\"92%\">".$row["imgdata"]."</td>"; $list .="</tr>";}$list .="</table>";echo ($list)}else { echo ("MySql Error"); exit();}?></body></html>[/code]Or something close to thatNot sure how to fit in the special characters tho Link to comment https://forums.phpfreaks.com/topic/26560-print-with-variables/#findComment-121494 Share on other sites More sharing options...
trq Posted November 8, 2006 Share Posted November 8, 2006 No... dont use a while() loop!!! You only have one record, no loop is required. Link to comment https://forums.phpfreaks.com/topic/26560-print-with-variables/#findComment-121502 Share on other sites More sharing options...
benji87 Posted November 8, 2006 Author Share Posted November 8, 2006 What thorpe told me to do does work but now it is just showing the image as binary characters and not the image itself where as it does if you just do print $imgdata; Link to comment https://forums.phpfreaks.com/topic/26560-print-with-variables/#findComment-121506 Share on other sites More sharing options...
Round Posted November 8, 2006 Share Posted November 8, 2006 if you shouldn't use while loops for single results, whats best in my instance?[code]<?php #create query $sql = "select * from stu where stu_id=\"$stu_id\" "; #exe query $rs = mysql_query( $sql, $conn ) or die( "Could not execute Query"); while ( $row = mysql_fetch_array( $rs ) ) { $list = "<table border=\"0\" align=\"center\" cellpadding=\"4\" width=\"100%\">"; $list .= "<tr>"; $list .= "<th class=table width=\"25%\">ID:</th>"; $list .= "<td class=tblleft>".$row["stu_id"]."</td>"; $list .= "</tr>"; $list .= "<tr>"; $list .= "<th class=table width=\"25%\">Name:</th>"; $list .= "<td class=tblleft>".$row["forename"]." ".$row["surname"]."</td>"; $list .= "</tr>"; $list .= "<tr>"; $list .= "<th class=table width=\"25%\">D.O.B:</th>"; $list .= "<td class=tblleft>".$row["birth_dt"]."</td>"; $list .= "</tr>"; } $list .= "</table>"; #display details echo( $list ); } else { echo ("No details); exit();} ?>[/code]It will only ever find 1 match but it sits within the HTML body (using tenplates) Link to comment https://forums.phpfreaks.com/topic/26560-print-with-variables/#findComment-121515 Share on other sites More sharing options...
benji87 Posted November 8, 2006 Author Share Posted November 8, 2006 Ive also tried what you gave me round and it did work and only returned one result but it did exactly as the other scripts do just return binary instead of an image. I really dont understand as it works fine when you just print $imgdata and no html Link to comment https://forums.phpfreaks.com/topic/26560-print-with-variables/#findComment-121519 Share on other sites More sharing options...
trq Posted November 8, 2006 Share Posted November 8, 2006 Your telling the browser your sending it text....<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">As for your question Round. Just remove the loop. eg;[code=php:0]$row = mysql_fetch_array( $rs );$list = "<table border=\"0\" align=\"center\" cellpadding=\"4\" width=\"100%\">";$list .= "<tr>";$list .= "<th class=table width=\"25%\">ID:</th>";$list .= "<td class=tblleft>".$row["stu_id"]."</td>";$list .= "</tr>";$list .= "<tr>";$list .= "<th class=table width=\"25%\">Name:</th>";$list .= "<td class=tblleft>".$row["forename"]." ".$row["surname"]."</td>";$list .= "</tr>"; $list .= "<tr>";$list .= "<th class=table width=\"25%\">D.O.B:</th>";$list .= "<td class=tblleft>".$row["birth_dt"]."</td>";$list .= "</tr>";$list .= "</table>";echo $list;[/code] Link to comment https://forums.phpfreaks.com/topic/26560-print-with-variables/#findComment-121572 Share on other sites More sharing options...
benji87 Posted November 8, 2006 Author Share Posted November 8, 2006 Nope thats not the solution still the same problem :o( please somebody Link to comment https://forums.phpfreaks.com/topic/26560-print-with-variables/#findComment-121576 Share on other sites More sharing options...
Round Posted November 8, 2006 Share Posted November 8, 2006 ok I,ve removed the loop and it display the table but no data within ???CheersSorry ignore me, my fault I missed a ;.Doh!Cheers thorpe Link to comment https://forums.phpfreaks.com/topic/26560-print-with-variables/#findComment-121581 Share on other sites More sharing options...
trq Posted November 8, 2006 Share Posted November 8, 2006 [quote]Nope thats not the solution still the same problem Shocked( please somebody[/quote]You need to tell the browser your sending an image via a header call (this your allrerady doing). Unfortunately, this also meens you'll need to put the code to pull your image in a seperate script and place a link to this script within <img> tags.At the moment your telling the broswer your sending an image, then your telling it your sending text, and then you actually do send text.yet another reason not to store images in a database! Link to comment https://forums.phpfreaks.com/topic/26560-print-with-variables/#findComment-121616 Share on other sites More sharing options...
benji87 Posted November 8, 2006 Author Share Posted November 8, 2006 I know this is far from the best way of storing images but i could never work out how to when uploading images to a directory at the same time upload its path to a sql table! Is that possible? Link to comment https://forums.phpfreaks.com/topic/26560-print-with-variables/#findComment-121628 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.