The full output from the webserver (including headers) is:
The 3 byte content is two carriage returns and a space ("\n\n "). The URL displayed in the client window seems to be generated by the browser (@xav0980: you wouldn't happen to be using Firefox, would you?). If it weren't for the fact that IndexProject.php dumps binary data into the table, I'd wonder if the bmkimage field were empty for row where PartNo='APC403062CLE'. Try fetching rows as an associative array rather than the integer indexed array. While you're at it, you might as well specify in $SQLstring exactly what's being fetched:
If that still doesn't work, try var_dump($Row) rather than just printing $Row['bmkimage'] so you can see exactly what was returned by the query.
That dbconnect.php has been bugging me for a bit. It appears to rely too much on global variables and performs actions without performing their counterparts. This all makes the script harder to maintain. Also, separating definitions from actions will make it easier to reuse code. Try something like this, to start with:
This provides a flexible yet secure way of creating connections. It's used as follows:
Using exceptions, the bulk of that script could be refactored as a function (named "fetch_single()", perhaps) that runs a query and returns a single result, resulting in a very short script, but that's a matter for another time.
Speaking of "click here", I would have thought people understood the concept of hypertext by now, but considering how often you see it... Of course, considering that some people appear to need to be told to "click here", maybe they're on to something.
Code:
HTTP/1.1 200 OK
Date: Mon, 22 Jun 2009 04:38:17 GMT
Server: Apache/2.2.11 (Freewebhostingarea.com) mod_perl/2.0.4 Perl/v5.10.0
X-Powered-By: PHP/5.2.9
Content-Length: 3
Connection: close
Content-Type: image/jpeg
PHP:
<?php
//get_image.php
// sanitized ID
$image_id = preg_replace('/\W.*/', '', $_GET['image']);
$TableName = "automobileparts";
$SQLstring = "SELECT bmkimage FROM automobileparts WHERE PartNo = '$image_id'";
require_once("dbconnect.php");
header('Content-Type: image/jpeg');
$Row= $QueryResult->fetch_assoc();
echo $Row['bmkimage'];
$DBConnect->close();
?>
That dbconnect.php has been bugging me for a bit. It appears to rely too much on global variables and performs actions without performing their counterparts. This all makes the script harder to maintain. Also, separating definitions from actions will make it easier to reuse code. Try something like this, to start with:
PHP:
<?php
//dbconnect.php
function local_db_connect($driver='mysqli', $user='dfltUser', $pw='*****', $db='') {
return new $driver('localhost', $user, $pw, $db);
}
?>
PHP:
<?php
//get_image.php
require_once("dbconnect.php");
$dbConn = local_db_connect();
// note: be sure to change database name to a real one
$dbConn->select_db('some_database');
// sanitized ID; could also do: $image_id = preg_replace('/\W.*/', '', $_GET['image']);
$image_id = $dbConn->escape_string($_GET['image']);
$tableName = "automobileparts";
if ($dbConn) {
$SQLstring = "SELECT bmkimage FROM $tableName WHERE PartNo = '$image_id'";
if ($queryResult=$dbConn->query($SQLstring)
and $row=$queryResult->fetch_assoc())
{
header('Content-Type: image/jpeg');
echo $row['bmkimage'];
$dbConn->close();
} else {
// errordoc/404.php is the custom 404 error page.
include('errordoc/404.php');
}
} else {
header('Content-Type: text/plain');
echo "Couldn't connect to image database. Something is probably wrong with the server. Please try again later.";
}
?>
Go for it, rough as it is. If you think of any improvements for the wording, please let me know. In particular, "these guidelines" is too much like "click here". Maybe "Eric Raymond's guidelines"?Oh BTW, mission, can I quote your signature? I find it very to the point nowadays...
Speaking of "click here", I would have thought people understood the concept of hypertext by now, but considering how often you see it... Of course, considering that some people appear to need to be told to "click here", maybe they're on to something.