problem with array

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
babush
Forum Newbie
Posts: 3
Joined: Wed Feb 25, 2009 7:50 am

problem with array

Post by babush »

Hi,

The code below is from a tutorial at phpriot.com (yes it suks to be a newbie)
<?php
.
.
$image_id = $_GET['image_id'];

$query = mysql_query("SELECT image, image_type FROM testblob WHERE image_id='".$image_id."'");
$row = mysql_fetch_assoc($query);
$image = $row['image'];
if(sizeof($row) == 2)
{
header("Content-type: ". $row['image_type']);
echo $image;
}
print_r($image);
?>

The db connection is ok, i can upload the images but i was not able to display them using the code above. If the "header("Content-type: ". $row['image_type']);" is commented print_r and echo of $image gives Resource id#5.

thanks for your help.
~BlitZ
Forum Newbie
Posts: 22
Joined: Mon Feb 23, 2009 5:36 pm
Location: United Kingdom

Re: problem with array

Post by ~BlitZ »

Could you please post up more of the script please?
Thank you.
babush
Forum Newbie
Posts: 3
Joined: Wed Feb 25, 2009 7:50 am

Re: problem with array

Post by babush »

<?php

$username = "***";
$password = "****";
$host = "localhost";
$database = "test";

@mysql_connect($host, $username, $password) or die("Can not connect to database: ".mysql_error());

@mysql_select_db($database) or die("Can not select the database: ".mysql_error());

$id = $_GET['image_id'];

if(!isset($id) || empty($id)){
die("Please select your image!");
}else{

$sql = "select * from testblob1 where image_id = 2";
$result = mysql_query($sql);
$row = mysql_fetch_assoc($result);
$image = $row['image'];


//header('Content-type: image/jpg');

echo $image;

}

echo "<br />";
print_r($row);
?>

the output on Firefox is below

Resource id #8
Array ( [image_id] => 2 [image_type] => image/jpeg [image] => Resource id #8 [image_size] => width="1280" height="1024 [image_ctgy] => [image_name] => Nehmesis_LHR_TOP.jpg )

thank you
babush
Forum Newbie
Posts: 3
Joined: Wed Feb 25, 2009 7:50 am

Re: problem with array

Post by babush »

i think there is something wrong with the upload form i use below because the blob data in mysql does not seem to be correct.

<?php require_once("includes/connection.php"); ?>
<html>
<head><title>File Upload To Database</title></head>
<body>
<h2>Please Choose a File and click Submit</h2>
<form enctype="multipart/form-data" action="<?php echo htmlentities($_SERVER['PHP_SELF']);?>" method="post">
<input type="hidden" name="MAX_FILE_SIZE" value="99999999" />
<div><input name="userfile" type="file" /></div>
<div><input type="submit" value="Submit" /></div>
</form>
<?php

function upload(){
/*** check if a file was uploaded ***/
if(is_uploaded_file($_FILES['userfile']['tmp_name']) && getimagesize($_FILES['userfile']['tmp_name']) != false)
{
/*** get the image info. ***/
$size = getimagesize($_FILES['userfile']['tmp_name']);
/*** assign our variables ***/
$type = $size['mime'];
$imgfp = fopen($_FILES['userfile']['tmp_name'], 'rb');
$size = $size[3];
$name = $_FILES['userfile']['name'];
$maxsize = 99999999;

/*** check the file is less than the maximum file size ***/
if($_FILES['userfile']['size'] < $maxsize )
{
/*** our sql query ***/
$query = ("INSERT INTO testblob (image_type ,image, image_size, image_name) VALUES ('$type' ,'$imgfp', '$size', '$name')");

/*** execute the query ***/
$results = mysql_query($query);
}
else
{
/*** throw an exception is image is not of type ***/
throw new Exception("File Size Error");
}
}
else
{
// if the file is not less than the maximum allowed, print an error
throw new Exception("Unsupported Image Format!");
}
}
?>

<?php

/*** check if a file was submitted ***/
if(!isset($_FILES['userfile']))
{
echo '<p>Please select a file</p>';
}
else
{
try {
upload();
/*** give praise and thanks to the php gods ***/
echo '<p>Thank you for submitting</p>';
}
catch(Exception $e)
{
echo '<h4>'.$e->getMessage().'</h4>';
}
}
?>
<a href="file.php?image_id=1">Pictures</a>
</body>
</html>
Post Reply