fredted40x Posted March 23, 2011 Share Posted March 23, 2011 Hi, I have managed to get the code working to store a .jpg file in the database under the longblob type. Now all i have left to do is to retrieve that image and display it. So far i have this: list.php while($r = mysql_fetch_array($sql)) { //for each record ... echo " <img src= getoutside.php?id='".$r[apartmentId]. " '> "; getoutside.php <?php header("Content-type: image/jpg"); // act as a jpg file to browser $nId = $_GET['id']; include 'dbase.php'; //connect to database $sqlo = "SELECT outside FROM apartment WHERE apartmentId = $nId"; $oResult = mysql_query($sqlo); $oRow = mysql_fetch_array($oResult); $sJpg = $oRow["outside"]; echo $sJpg; ?> The result from this is a box with a red cross in it. Can anyone find a problem with this code please? Thanks Link to comment https://forums.phpfreaks.com/topic/231476-retrieving-image-from-database-blob-just-getting-binary/ Share on other sites More sharing options...
aabid Posted March 23, 2011 Share Posted March 23, 2011 echo " <img src= getoutside.php?id='".$r['outside']. " '> "; where are the escape slashes, coz when zend engine sees second " in that line, it might take it as a end of echo statement. Link to comment https://forums.phpfreaks.com/topic/231476-retrieving-image-from-database-blob-just-getting-binary/#findComment-1191217 Share on other sites More sharing options...
fredted40x Posted March 23, 2011 Author Share Posted March 23, 2011 can you explain please? Dont think i've used them before. Heres the code to add the image if it helps. //get file information --step1 $fileName = $_FILES['userfile']['name']; $tmpName = $_FILES['userfile']['tmp_name']; $fileSize = $_FILES['userfile']['size']; //get file content -- step1 $fp = fopen($tmpName, 'r'); $content = fread($fp, $fileSize); $content = addslashes($content); fclose($fp); Link to comment https://forums.phpfreaks.com/topic/231476-retrieving-image-from-database-blob-just-getting-binary/#findComment-1191222 Share on other sites More sharing options...
aabid Posted March 23, 2011 Share Posted March 23, 2011 You have used double quotes inside double quotes in that line that i mentioned in the previous reply. These sort of statements confuse the engine on where to start and where to end. In other words when you echo anything and put first double quote then all the compiler(or whatever it is called ) has to search another double quote and prints out everything that is in between them. You should add \ before every double quote that is in between to let the compiler know that its just a literal. Link to comment https://forums.phpfreaks.com/topic/231476-retrieving-image-from-database-blob-just-getting-binary/#findComment-1191226 Share on other sites More sharing options...
fredted40x Posted March 23, 2011 Author Share Posted March 23, 2011 Ok, understand now thanks. Adding the / to make it echo "<img src=getoutside.php?id='\".$r['apartmentId']. \" '>"; gives me a unexpected T_ENCAPSED_AND_WHITESPACE error. without the \'s it now produces just a white image with an x in. Link to comment https://forums.phpfreaks.com/topic/231476-retrieving-image-from-database-blob-just-getting-binary/#findComment-1191237 Share on other sites More sharing options...
fredted40x Posted March 23, 2011 Author Share Posted March 23, 2011 Are you able to check the insert code for me please. Im thinking it may either be that or the getoutside.php file. <form action="admin.php" method="post" enctype="multipart/form-data" > ... <input type="hidden" name="MAX_FILE_SIZE" value="2000000"> <input name="userfile" type="file" id="userfile"> ... admin.php include 'dbase.php'; //connect to database //get file information --step1 $fileName = $_FILES['userfile']['name']; $tmpName = $_FILES['userfile']['tmp_name']; $fileSize = $_FILES['userfile']['size']; //get file content -- step1 $fp = fopen($tmpName, 'r'); $content = fread($fp, $fileSize); $content = addslashes($content); fclose($fp); $query = "INSERT INTO apartment VALUES(NULL,...,'$content')"; mysql_query($query) Link to comment https://forums.phpfreaks.com/topic/231476-retrieving-image-from-database-blob-just-getting-binary/#findComment-1191246 Share on other sites More sharing options...
aabid Posted March 23, 2011 Share Posted March 23, 2011 echo "<img src=getoutside.php?id='\".$r[apartmentId]. \" '>"; This will do the job as i have checked it Link to comment https://forums.phpfreaks.com/topic/231476-retrieving-image-from-database-blob-just-getting-binary/#findComment-1191295 Share on other sites More sharing options...
fredted40x Posted March 23, 2011 Author Share Posted March 23, 2011 Thanks again for replying. That line seems to work but without errors now but im still getting a white box with a cross in it. Its says the location for the image is http://localhost/webs/getoutside.php?id='".15. and if i remove the two ' it says the location is http://localhost/webs/getoutside.php?id=".15. Do you need the full stops? If i remove them they go from the url as well. It doesnt seem to be calling getoutside.php, from what i can gather it thinks that it is a image. edit: If i change src to href i get a white box with a different icon in it Link to comment https://forums.phpfreaks.com/topic/231476-retrieving-image-from-database-blob-just-getting-binary/#findComment-1191312 Share on other sites More sharing options...
fredted40x Posted March 23, 2011 Author Share Posted March 23, 2011 Solved! Found out i didnt need any " or '. echo "<img src=getoutside.php?id=$r[apartmentId]>"; worked . Thanks for helping p.s. cant find the soled button now Link to comment https://forums.phpfreaks.com/topic/231476-retrieving-image-from-database-blob-just-getting-binary/#findComment-1191314 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.