Shanmuga Ramesh Shanmuga Ramesh - 5 months ago 10
PHP Question

image cannot be displayed because its conain error in php

$result = mysql_query("select profilepic from `profile` where id = '$pid' ");

if ($result) {
if ($row = mysql_fetch_array($result)) {
$img = $row["profilepic"];
}
}

header("Content-type: image/jpeg");
echo $img;

Answer

You need to create another php script to return the image data(getImage.php). Change main file as

<body>
<img src="getImage.php?id=1" width="175" height="200" />
</body>

Then getImage.php is

$result = mysql_query("select profilepic from `profile` where id = '$pid' ");
if ($result) {
$row = mysql_fetch_assoc($result) ;
$img = $row["profilepic"];
header("Content-type: image/jpeg");
echo $row['img'];
}

You have to use mysql_fetch_assoc() if you want to get values by its column name


mysql_fetch_row

-This function will return a row where the values will come in the order as they are defined in the SQL query, and the keys will span from 0 to one less than the number of columns selected.

mysql_fetch_assoc

-This function will return a row as an associative array where the column names will be the keys storing corresponding value.

mysql_fetch_array

-This function will actually return an array with both the contents of mysql_fetch_row and mysql_fetch_assoc merged into one. It will both have numeric and string keys which will let you access your data in whatever way you'd find easiest.

It is recommended to use either _assoc or _row though.