Jump to content

[SOLVED] error trying to display an image from a databse in a webpage


leest

Recommended Posts

I have a script that i have previously had working but i have swapped servers and changed some pages and now i can not seem to get this to work, so any ideas would be appreciated.

 

The line to pass the id is:

 

<img src="http://My website/image/download.php?id=

 echo $site_id 

 

the site id is generated from a database query which works ok

 

it then calls this code, i have preset the id and added a couple of echo's to see how far the code gets, but it only displays the first test and doesn't get to the echo id or test2.

 


echo "test";

if(isset($_GET['id']))
{
include 'config.php';

$id = 1;

echo "$id";

echo "$test2";
$query = "SELECT name, type, size, content " .
         "FROM upload WHERE id = '$id'";

$result = mysql_query($query) or die('Error, query failed');
list($name, $type, $size, $content) = mysql_fetch_array($result);

header("Content-length: $size");
header("Content-type: $type");
header("Content-Disposition: attachment; filename=$name");
echo $content;


exit;
}

 

Link to comment
Share on other sites

This looks like a PHP related problem if you don't get echo id or 'test2' line

 

It looks like your $_GET['id'] variable is not set. Try putting print_r($_GET) right before the IF statement. That will show you what $_GET vars are set and if the $_GET['id'] variable exists and is correct.

Link to comment
Share on other sites

Thanks for the reply, because i am calling the download page direct from the browser i manually set the variable as it is of course not being sent one and so i thought i would remove the if statement to see if that made any difference.

 

if(isset($_GET['id']))
{

now when i run the script i get no thing at all,

Link to comment
Share on other sites

ok i have stripped the code down and took away the query and bottom half of the code so just the variable and echo statements where left and the variable echoed, however as soon as i added the query part the variable stopped echoing.  but it doesn't makes sense

Link to comment
Share on other sites

That is strange!

 

Ok, so we have confirmed that the if statement is correct and the trouble is somewhere around the 'echo "$id"' or echo "$test2" lines?

 

Have you already defined the variable $test2 that you are echoing with echo "$test2"?

 

Do you have error_reporting set to E_ALL? http://www.phpcodinghelp.com/article.php?article=debug#basic_error_messages

 

Are you manually setting the '$id' variable to '1' for debugging purposes or were you wanting it to be $id = $_GET['id']?

 

Is there anything in 'config.php' that is messing up the script? Is 'config.php' included into your script more then once?

 

Have you checked the SQL query? Does it work correctly by itself?

Link to comment
Share on other sites

Ok,

 

Yes i have manually set id to 1 for testing purposes, test2 was an error it wasn't meant to be a variable just an echo to see how far the script got, so that was fixed ages ago. 

 

I have checked the config file and that works fine, what i am getting now is an error failed query that is generated by the query it's self and i don't know why.  I have included the database structure below.

 

 

 

  `id` int(11) NOT NULL auto_increment,

  `name` varchar(30) NOT NULL,

  `type` varchar(30) NOT NULL,

  `size` int(11) NOT NULL,

  `content` mediumblob NOT NULL,

  `site_id` varchar(10) NOT NULL,

  PRIMARY KEY  (`id`)

 

 

below is the script i am now running and the script that generates the error, query failed message.

 

include 'System/connections/db3.php';

$id    = 1;
$query = "SELECT name, type, size, content " .
         "FROM upload WHERE id = '$id'";

$result = mysql_query($query) or die('Error, query failed');
list($name, $type, $size, $content) = mysql_fetch_array($result);

header("Content-length: $size");
header("Content-type: $type");
header("Content-Disposition: attachment; filename=$name");
echo $content;


exit;

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.