Jump to content

How to output BLOB images through PHP to HTML ?


anevins

Recommended Posts

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

Link to comment
Share on other sites

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.

 

 

 

 

Link to comment
Share on other sites

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 */
?>

Link to comment
Share on other sites

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>';
}	

Link to comment
Share on other sites

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'];

Link to comment
Share on other sites

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'];

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

 

Link to comment
Share on other sites

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;
?>

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.