anevins Posted March 18, 2011 Share Posted March 18, 2011 I'm trying to display my BLOB images in PHP and I've looked around in Google searches but everyone's examples seem to work for their specific code. My table is called 'identification'. Here's my SQL: == Table structure for table identification |------ |Field|Type|Null|Default |------ |//**id**//|int(11)|No| |image|blob|No| |image_width|char(220)|No| |image_height|char(220)|No| |image_type|enum('jpeg', 'pjpeg', 'png', 'gif')|No| |stickID|int(11)|Yes|NULL Quote Link to comment https://forums.phpfreaks.com/topic/231027-how-to-output-blob-images-through-php-to-html/ Share on other sites More sharing options...
litebearer Posted March 18, 2011 Share Posted March 18, 2011 what coding have you tried? what errors have you encountered? Quote Link to comment https://forums.phpfreaks.com/topic/231027-how-to-output-blob-images-through-php-to-html/#findComment-1189259 Share on other sites More sharing options...
anevins Posted March 18, 2011 Author Share Posted March 18, 2011 Well I've tried sticking in things like: print "<a href=\"$_SERVER[php_SELF]?id=$row[0]\">"; print "image $row[0] ($row[1] bytes)</a><br />\n"; And that outputs notices about undefined offets 0 or 1, and doesn't display the image. I've also tried: $image = $row['image']; header("Content-type: image/png"); // or whatever print $image; But I get this error: The image “http://localhost/xampp/dsa/wp3.php?terms=h&Search=Search” cannot be displayed, because it contains errors. Quote Link to comment https://forums.phpfreaks.com/topic/231027-how-to-output-blob-images-through-php-to-html/#findComment-1189272 Share on other sites More sharing options...
anevins Posted March 19, 2011 Author Share Posted March 19, 2011 bump Quote Link to comment https://forums.phpfreaks.com/topic/231027-how-to-output-blob-images-through-php-to-html/#findComment-1189463 Share on other sites More sharing options...
litebearer Posted March 19, 2011 Share Posted March 19, 2011 You are close (adjust to fit your table name etc)... Because displaying a BLOB image requires the use of headers AND that usually conflicts with being able to have other 'simultaneous' output to the browser, we do a little 'magic, like pulling a rabbit out of a hat ... 1. create a file called ShowBlob.php with the following content (this is where we put the rabbit in the hat ahead of time): <?PHP /* connect to db */ include('db.php'); $id = $_ REQUEST ['id']; $query = "SELECT img_name, img_type, img_size, img_data FROM img_tbl WHERE id = ‘$id’"; result = mysql_query($query) or die(mysql_error()); list($name, $type, $size, $content) = mysql_fetch_array($result); header(”Content-length: $size”); header(”Content-type: $type”); echo $content; ?> 2. In the page where you want to display the image do the following: <?PHP /* connect to db */ include('db.php'); $query = "SELECT id FROM tbl_images WHERE some_condition"; $result = mysql_query($query); $row = mysql_fetch_array($result); $id = $row[0]; ?> <img src="ShowBlob.php?id=<?PHP echo $id;?>" alt=""> <?PHP /* do more stuff */ ?> Quote Link to comment https://forums.phpfreaks.com/topic/231027-how-to-output-blob-images-through-php-to-html/#findComment-1189473 Share on other sites More sharing options...
anevins Posted March 19, 2011 Author Share Posted March 19, 2011 Okay I've created that showblob.php file but on the displaying image file, I am getting a syntax error: unexpected < on line: echo '<img src="showblob.php?id="'.<?php echo $id;?>.'" alt="" />'; Quote Link to comment https://forums.phpfreaks.com/topic/231027-how-to-output-blob-images-through-php-to-html/#findComment-1189494 Share on other sites More sharing options...
anevins Posted March 19, 2011 Author Share Posted March 19, 2011 I'm now using this code for the image: echo '<img src="showblob.php?id="'.$id.'" alt="" />'; But I get a notice, undefined index on $id = $row[0]; Quote Link to comment https://forums.phpfreaks.com/topic/231027-how-to-output-blob-images-through-php-to-html/#findComment-1189496 Share on other sites More sharing options...
anevins Posted March 19, 2011 Author Share Posted March 19, 2011 I'm trying to allow these BLOB images to come up through a search, so this is my code: if (isset($_GET['terms']) && ($_GET['terms'] != 'Search...') ) { $terms = $_GET['terms']; $dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME) or die *('Error connecting to MySQL server'); // Query the database. $query = "SELECT * FROM stick, location, identification WHERE make LIKE '%$terms%' AND stick.sid = location.lid AND identification.stickID = stick.sid "; // Fetch the results. $result=mysqli_query($dbc,$query); // Print the results: $num_rows = mysqli_num_rows($result); // If the results of the query match to 1 or more rows, display the products by their title, price and image. if ($num_rows > 0){ if($result=mysqli_query($dbc,$query)){ $output = Array(); while($row=mysqli_fetch_assoc($result)) { //'<br /> Street: ' .$row['street'].' $output[] = '<ul>'; $output[] = '<li> Type: '.$row['make'] .'<br />Size: '.$row['size'].$row['type'].'<br />Colour: '.$row['colour'] . '<br /> Street Missing in: ' .$row['street'].'<br />Town missing in: '.$row['town'] .'<br />City missing in: '.$row['city'].'</li>'; $output[] = '</ul>'; $id = $row[0]; echo '<img src="showblob.php?id="'.$id.'" alt="" />'; } } echo join('',$output); } // If the results don't match the titles of the table, output this message. else { echo "<h3>Sorry,</h3>"; echo "<p>your search: "" .$terms. "" returned zero results</p>"; } } else { // Tell them to use the search form. echo '<p class="error">Please use the search form at the top of the window to search this site.</p>'; } Quote Link to comment https://forums.phpfreaks.com/topic/231027-how-to-output-blob-images-through-php-to-html/#findComment-1189498 Share on other sites More sharing options...
litebearer Posted March 19, 2011 Share Posted March 19, 2011 Try changing this... $id = $row[0]; echo '<img src="showblob.php?id="'.$id.'" alt="" />'; to this... $id = $row['id']; ?> <img src="showblob.php?id=<? echo $id; ?>" alt="" /> <?PHP Quote Link to comment https://forums.phpfreaks.com/topic/231027-how-to-output-blob-images-through-php-to-html/#findComment-1189503 Share on other sites More sharing options...
anevins Posted March 19, 2011 Author Share Posted March 19, 2011 Thanks, that removes the notices but no images seem to be coming through. I've moved the image tag from if ($num_rows > 0){ if($result=mysqli_query($dbc,$query)){ $output = Array(); while($row=mysqli_fetch_assoc($result)) { $output[] = '<ul>'; $output[] = '<li> Type: '.$row['make'] .'<br />Size: '.$row['size'].$row['type'].'<br />Colour: '.$row['colour'] . '<br /> Street Missing in: ' .$row['street'].'<br />Town missing in: '.$row['town'] .'<br />City missing in: '.$row['city'].'</li>'; $output[] = '</ul>'; $id = $row['id']; ?> <img src="showblob.php?id=<? echo $id; ?>" alt="" /> <?php } } echo join('',$output); } to if ($num_rows > 0){ if($result=mysqli_query($dbc,$query)){ $output = Array(); while($row=mysqli_fetch_assoc($result)) { $output[] = '<ul>'; $output[] = '<li> Type: '.$row['make'] .'<br />Size: '.$row['size'].$row['type'].'<br />Colour: '.$row['colour'] . '<br /> Street Missing in: ' .$row['street'].'<br />Town missing in: '.$row['town'] .'<br />City missing in: '.$row['city'].'</li>'; $output[] = '</ul>'; $id = $row['id']; } } echo join('',$output); ?> <img src="showblob.php?id=<? echo $id; ?>" alt="" /> <?php } And in the view source of that page, I see this: <img src="showblob.php?id=6" alt="" /> The showblob.php?id=6 is clickable and once clicked, I get this message still in view source: <b>Parse error</b>: syntax error, unexpected T_STRING in <b>G:\xampp\htdocs\xampp\dsa\showblob.php</b> on line <b>6</b><br /> Line 6 showblob.php: $id = $_ REQUEST ['id']; Quote Link to comment https://forums.phpfreaks.com/topic/231027-how-to-output-blob-images-through-php-to-html/#findComment-1189507 Share on other sites More sharing options...
litebearer Posted March 19, 2011 Share Posted March 19, 2011 In showblob.php try add this right after the opening php tag... $id = $_GET['id']; echo $id; exit(); Quote Link to comment https://forums.phpfreaks.com/topic/231027-how-to-output-blob-images-through-php-to-html/#findComment-1189513 Share on other sites More sharing options...
anevins Posted March 19, 2011 Author Share Posted March 19, 2011 Okay so the new showblog.php code is: <?php $id = $_GET['id']; echo $id; exit(); /* connect to db */ include('connectvars.php'); $dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME); $id = $_ REQUEST ['id']; $query = "SELECT image FROM identification WHERE id = ‘$id’"; mysqli_query($dbc, $query); list($name, $type, $size, $content) = mysql_fetch_array($result); header(”Content-length: $size”); header(”Content-type: $type”); echo $content; ?> When trying to view this file by itself, the error given is: Parse error: syntax error, unexpected T_STRING in G:\xampp\htdocs\xampp\dsa\showblob.php on line 9 line 9: $id = $_ REQUEST ['id']; Quote Link to comment https://forums.phpfreaks.com/topic/231027-how-to-output-blob-images-through-php-to-html/#findComment-1189521 Share on other sites More sharing options...
litebearer Posted March 19, 2011 Share Posted March 19, 2011 remove that line Quote Link to comment https://forums.phpfreaks.com/topic/231027-how-to-output-blob-images-through-php-to-html/#findComment-1189530 Share on other sites More sharing options...
anevins Posted March 19, 2011 Author Share Posted March 19, 2011 Line removed. New error: Parse error: syntax error, unexpected ':' in G:\xampp\htdocs\xampp\dsa\showblob.php on line 14 Line 14: header(”Content-length: $size”); Quote Link to comment https://forums.phpfreaks.com/topic/231027-how-to-output-blob-images-through-php-to-html/#findComment-1189532 Share on other sites More sharing options...
UQ13A Posted March 19, 2011 Share Posted March 19, 2011 Replace: header(”Content-length: $size”); With header("Content-length: $size"); Quote Link to comment https://forums.phpfreaks.com/topic/231027-how-to-output-blob-images-through-php-to-html/#findComment-1189545 Share on other sites More sharing options...
anevins Posted March 19, 2011 Author Share Posted March 19, 2011 Ok, now I get: Notice: Undefined index: id in G:\xampp\htdocs\xampp\dsa\showblob.php on line 2 Line 2:$id = $_GET['id']; Quote Link to comment https://forums.phpfreaks.com/topic/231027-how-to-output-blob-images-through-php-to-html/#findComment-1189579 Share on other sites More sharing options...
litebearer Posted March 19, 2011 Share Posted March 19, 2011 Busy day - need to be out for a while; however, will be back later to help solve this (think I know what problem is but need to clarify it in my brain LOL) Quote Link to comment https://forums.phpfreaks.com/topic/231027-how-to-output-blob-images-through-php-to-html/#findComment-1189605 Share on other sites More sharing options...
anevins Posted March 19, 2011 Author Share Posted March 19, 2011 No worries, take your time Quote Link to comment https://forums.phpfreaks.com/topic/231027-how-to-output-blob-images-through-php-to-html/#findComment-1189615 Share on other sites More sharing options...
PFMaBiSmAd Posted March 19, 2011 Share Posted March 19, 2011 Ok, now I get: Notice: Undefined index: id in G:\xampp\htdocs\xampp\dsa\showblob.php on line 2 Line 2:$id = $_GET['id']; Is that when you are using a URL like showblob.php?id=x and are you doing any URL rewriting that might not be passing the ?id=x on the end of the actual URL? Quote Link to comment https://forums.phpfreaks.com/topic/231027-how-to-output-blob-images-through-php-to-html/#findComment-1189620 Share on other sites More sharing options...
anevins Posted March 19, 2011 Author Share Posted March 19, 2011 I think the previous person just wanted to see what value id had, so I've removed it. Here's the new blob php file code: <?php /* connect to db */ include('connectvars.php'); $dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME); $query = "SELECT image FROM identification WHERE id = ‘$id’"; mysqli_query($dbc, $query); list($name, $type, $size, $content) = mysql_fetch_array($result); header("Content-length: $size"); header("Content-type: $type"); echo $content; ?> That works; as in there's no errors. But I can't get the image displayed in the HTML. Quote Link to comment https://forums.phpfreaks.com/topic/231027-how-to-output-blob-images-through-php-to-html/#findComment-1189628 Share on other sites More sharing options...
anevins Posted March 19, 2011 Author Share Posted March 19, 2011 Undefined function ibase_blob_echo ? I found this function on: http://php.net/manual/en/function.ibase-blob-echo.php But I can't use it, any ideas why its not recognising it? Quote Link to comment https://forums.phpfreaks.com/topic/231027-how-to-output-blob-images-through-php-to-html/#findComment-1189644 Share on other sites More sharing options...
anevins Posted March 19, 2011 Author Share Posted March 19, 2011 Okay the new showblob.php warnings are: <br /> <b>Notice</b>: Undefined variable: id’ in <b>G:\xampp\htdocs\xampp\dsa\showblob.php</b> on line <b>7</b><br /> <br /> <b>Warning</b>: mysql_fetch_array() expects parameter 1 to be resource, boolean given in <b>G:\xampp\htdocs\xampp\dsa\showblob.php</b> on line <b>11</b><br /> And the code is: <?php /* connect to db */ include('connectvars.php'); $dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME); $query = "SELECT image FROM identification WHERE id = ‘$id’"; $result = mysqli_query($dbc, $query); list($name, $type, $size, $content) = mysql_fetch_array($result); header("Content-length: $size"); header("Content-type: $type"); echo $content; ?> Quote Link to comment https://forums.phpfreaks.com/topic/231027-how-to-output-blob-images-through-php-to-html/#findComment-1189669 Share on other sites More sharing options...
anevins Posted March 20, 2011 Author Share Posted March 20, 2011 bump Quote Link to comment https://forums.phpfreaks.com/topic/231027-how-to-output-blob-images-through-php-to-html/#findComment-1189865 Share on other sites More sharing options...
PFMaBiSmAd Posted March 20, 2011 Share Posted March 20, 2011 Your current error is both because your query is failing due to an error of some kind AND you are attempting to use a mysql fetch statement with a mysqli query. To get php/mysql to tell you why your query is failing, (please) use some error checking and error reporting logic in your code - $result = mysqli_query($dbc, $query) or die(mysqli_error($dbc)); If you are using mysqli for your connection, you must use mysqli for all the operations using that connection. You cannot mix mysql and mysqli statements on one connection. Quote Link to comment https://forums.phpfreaks.com/topic/231027-how-to-output-blob-images-through-php-to-html/#findComment-1189897 Share on other sites More sharing options...
kenrbnsn Posted March 20, 2011 Share Posted March 20, 2011 The quote symbols in this statement are wrong: <?php $query = "SELECT image FROM identification WHERE id = ‘$id’"; ?> You should be using single quotes: <?php $query = "SELECT image FROM identification WHERE id = '$id'"; ?> Ken Quote Link to comment https://forums.phpfreaks.com/topic/231027-how-to-output-blob-images-through-php-to-html/#findComment-1189907 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.