...

View Full Version : mySQL image loading help



tecmeister
08-13-2008, 02:16 AM
Hi everyone,

I'm having trouble loading the image that i have stored in mySQL.
I'm getting a header error and loads of symbols:



Warning: Cannot modify header information - headers already sent by (output started at /home/sites/onestopremovalsshop.co.uk/public_html/shop.php:9) in /home/sites/onestopremovalsshop.co.uk/public_html/shop.php on line 174


This is the script that i have used to send the image to mySQL if it helps:



<?
$dbhost = "localhost";
$dbname = "********";
$dbuser = "********";
$dbpass = "********";

$connect = mysql_connect ($dbhost, $dbuser, $dbpass) or die("CAN NOT CONNECT:".mysql_error());
mysql_select_db ($dbname);

$name = $_POST['name'];
$des = $_POST['des'];
$no = $_POST['no'];
$price = $_POST['price'];

if (isset($_FILES['image']) && $_FILES['image']['size'] > 0) {

// Temporary file name stored on the server
$tmpName = $_FILES['image']['tmp_name'];

// Read the file
$fp = fopen($tmpName, 'r');
$data = fread($fp, filesize($tmpName));
$data = addslashes($data);
fclose($fp);


// Create the query and insert
// into our database.
$query = "INSERT INTO image(image, name, des, no, price) VALUES ('$data','$name', '$des', '$no', '$price')";
$results = mysql_query($query, $connect);

// Print results
print "Thank you, your image and data has been uploaded.";

}
else {
print "No image selected/uploaded";
}

mysql_close($connect);
?>


This is the code that i am using to view the image and data. I have know problem viewing the data just image.



<?php
$host="localhost";
$username="web180-shop-1";
$password="ryan2008";
$db_name="web180-shop-1";
$tbl_name="image";

mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

$sql="SELECT * FROM $tbl_name ORDER BY id DESC";
$result=mysql_query($sql);
?>
<table width="90%" border="0" align="center" cellpadding="3" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<td width="4%" align="center" bgcolor="#0000FF"><strong class="style4">#</strong></td>
<td width="12%" align="center" bgcolor="#0000FF"><strong class="style4">Name</strong></td>
<td width="8%" align="center" bgcolor="#0000FF"><strong class="style4">Price</strong></td>
<td width="7%" align="center" bgcolor="#0000FF"><strong class="style4">No of</strong></td>
<td width="69%" align="center" bgcolor="#0000FF"><span class="style4"><strong>Discription</strong></span></td>
</tr>

<?php
while($rows=mysql_fetch_array($result)){ // Start looping table row
?>
<tr>
<td bgcolor="#FFFFFF"><? echo $rows['id']; ?></td>
<td bgcolor="#FFFFFF"><? echo $rows['name']; ?><BR></td>
<td align="center" bgcolor="#FFFFFF"> <? echo $rows['price']; ?></td>
<td align="center" bgcolor="#FFFFFF"><? echo $rows['no']; ?></td>
<td align="center" bgcolor="#FFFFFF"><? echo $rows['des']; ?></td>
<? header('Content-type: image/jpg'); ?>
<td align="center" bgcolor="#FFFFFF"><? echo $rows['image]; ?></td>
</tr>

<?php
// Exit looping and close connection
}
mysql_close();
?>
</table>


Thanks for the help that you may be able to give me.

shyam
08-13-2008, 07:53 AM
<?php
$host="localhost";
$username="web180-shop-1";
$password="ryan2008";
$db_name="web180-shop-1";
$tbl_name="image";

mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

$sql="SELECT * FROM $tbl_name ORDER BY id DESC";
$result=mysql_query($sql);
?>
<table width="90&#37;" border="0" align="center" cellpadding="3" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<td width="4%" align="center" bgcolor="#0000FF"><strong class="style4">#</strong></td>
<td width="12%" align="center" bgcolor="#0000FF"><strong class="style4">Name</strong></td>
<td width="8%" align="center" bgcolor="#0000FF"><strong class="style4">Price</strong></td>
<td width="7%" align="center" bgcolor="#0000FF"><strong class="style4">No of</strong></td>
<td width="69%" align="center" bgcolor="#0000FF"><span class="style4"><strong>Discription</strong></span></td>
</tr>

<?php
while($rows=mysql_fetch_array($result)){ // Start looping table row
?>
<tr>
<td bgcolor="#FFFFFF"><? echo $rows['id']; ?></td>
<td bgcolor="#FFFFFF"><? echo $rows['name']; ?><BR></td>
<td align="center" bgcolor="#FFFFFF">&#163; <? echo $rows['price']; ?></td>
<td align="center" bgcolor="#FFFFFF"><? echo $rows['no']; ?></td>
<td align="center" bgcolor="#FFFFFF"><? echo $rows['des']; ?></td>
<? header('Content-type: image/jpg'); ?>
<td align="center" bgcolor="#FFFFFF"><? echo $rows['image]; ?></td>
</tr>

<?php
// Exit looping and close connection
}
mysql_close();
?>
</table>



you can only send one content-type per request...so, you script is already sending text/html for the html content and hence you cannot change it in the middle :(

what you can do is have a separate php script that only serves up the image...sorta like this

img.php

<?
$host="...";
$username="...";
$password="...";
$db_name="...";
$tbl_name="image";

mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

$id = $_GET["id"];
// sanitize $id !!
$sql="SELECT image FROM $tbl_name WHERE id=" . $id;
$result=mysql_query($sql);
header('Content-type: image/jpg');
if ( $rows=mysql_fetch_array($result) ) {
echo $rows['image];
} else {
// serve up a default
}
?>

and in your script change it


<tr>
<td bgcolor="#FFFFFF"><? echo $rows['id']; ?></td>
<td bgcolor="#FFFFFF"><? echo $rows['name']; ?><BR></td>
<td align="center" bgcolor="#FFFFFF">&#163; <? echo $rows['price']; ?></td>
<td align="center" bgcolor="#FFFFFF"><? echo $rows['no']; ?></td>
<td align="center" bgcolor="#FFFFFF"><? echo $rows['des']; ?></td>
<td align="center" bgcolor="#FFFFFF"><img src="img.php?id=<? echo $rows['id']; ?>"></td>
</tr>

tecmeister
08-13-2008, 05:33 PM
I have already tried that and i seem to still get the same header error.
I have looked up the error and mosr people say tat it is white space.

I have check the whole thing through, even created a new php file and still get the same error.

Is there anyother way of loading the image from mySQL?

Thanks for your help tho much appricated.

tecmeister



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum