Jump to content

Retrieving Image Location in MySQL


sashi34u

Recommended Posts

Dear all,

 

I have been trying to retrieve the location of a image from database and display the image in the browser using PHP. I have written a sort of code for the purpose. All I am getting in my browser after using the code is a long set of ASCII characters with the following warning

*Warning*: Cannot modify header information - headers already sent by (output started at C:\wamp\www\mysqli.php:65) in *C:\wamp\www\mysqli.php* on line *237

*

I am herewith attaching my code. If anyone can  point out the error or give some kind of advice that would be really great. The location of the image is stored in a table by the name IMAGE under the column name Location and here in the code, the location is retrieved under the varibale name $Location.

 

I have already looked through the Header errors sticky. But I cannot think of anyway in which I can change my code as of now to remove the warning. I have posted this question in the db forum, but it did not clear my doubts. Any help would be greatly appreciated

 

thanks,

Sashi

 

<html>
<body>
<form action="mysqli.php" method="post">
<br>
<div align="center">
Building Name:<select name="name">
<option value=""> Select a Building</option>
<option value="Agnew Hall">Agnew Hall</option>
     <option value="Rector Field House">Rector Field House</option>
     <option value="Richard B. Talbot Educational Resources Center">Richard B. Talbot Educational Resources Center</option>
     <option value="Robeson Hall">Robeson Hall</option>
     <option value="Sandy Hall">Sandy Hall</option>
     <option value="Saunders hall">Saunders hall</option>
     <option value="Seitz Hall">Seitz Hall</option>
     <option value="Shanks Hall">Shanks Hall</option>
     <option value="Shultz Hall">Shultz Hall</option>
     <option value="Skelton Conference Center">Skelton Conference Center</option>
     <option value="Williams Hall">Williams Hall</option>
     <option value="Women's Softball Field">Women's Softball Field</option>
     <option value="Wright House">Wright House</option>
</select>
</div>
                   <input type="submit" />
</form>
<img src="mysqli.php?">

<?php
// Connects to your Database
$host="*******";
$user="*****";
$password="******";
$dbname="*******";
$cxn=mysqli_connect($host, $user, $password, $dbname) ;
if (!$cxn=mysqli_connect($host, $user, $password, $dbname))
   {
       $error=mysqli_error($cxn);
       echo "$error";
       die();
   }
else
   {
       echo "Connection established successfully";
   }
//Define the variables for Day and Month
$Today=date("l");
$Month=date("F");
$build=$_POST["name"];
$low_speed=2.5;
$high_speed=4;
$hour=date("G");
$minute=date("i");
if ($minute>=00 && $minute<=14)
   {
       $minute=00;
   }
elseif ($minute>=15 && $minute<=29)
   {
       $minute=15;
   }
elseif ($minute>=30 && $minute<=44)
   {
       $minute=30;
   }
else
   {
       $minute=45;
   }
$times="$hour:$minute";
$sql="SELECT buildingname, parking_lot_name, empty_spaces, distance, round(distance/($low_speed*60),1) AS low_time, round(distance/($high_speed*60),1) AS high_time, Location FROM buildings, buildings_lots, parkinglots, occupancy2, Image where (buildings.buildingcode=occupancy2.building AND  buildings.buildingcode=buildings_lots.building_code AND parkinglots.parking_lot_code=buildings_lots.parking_lot_code AND parkinglots.parking_lot_code=occupancy2.parking_lot AND Buildings.BuildingCode=Image.BuildingCode) AND buildingname='$build' AND month='$Month' AND day='$Today' AND Time='$times'";
$data = mysqli_query($cxn,$sql);
if (!$data=mysqli_query($cxn,$sql))
   {
       $error=mysqli_error($cxn);
       echo "$error";
       die();
   }
else
   {
       echo "<br>";
       echo "Query sent successfully";
   }
echo "<br>";
echo "<h1> PARKING LOT INFORMATION <h1>";
echo "<table border='1' cellspacing='5' cellpadding='2'>";
echo "<tr>\n
       <th>Building</th>\n
       <th>Parking Lot</th>\n
       <th>Estimated Number of Empty Spaces</th>\n
       <th>Distance (Feet)</th>\n
       <th>Estimated walking time to the building</th>\n
   </tr>\n";
while ($row=mysqli_fetch_array($data))
   {
       extract($row);
$building = $row[0];
     $parking_lot = $row[1];
     $Number_of_Empty_Spaces = $row[2];
     $Distance = $row[3];
     $time_l = $row[4];
     $time_h=$row[5];
     $location=$row[6];   echo "<tr>\n
             <td>$building</td>\n
             <td>$parking_lot</td>\n               <td>$Number_of_Empty_Spaces</td>\n
             <td>$Distance</td>\n
             <td>$time_h - $time_l mins</td>\n
<td>$location</td>\n
             </tr>\n";
   }
   echo "</table>\n";
$err=1;
if ($img = file_get_contents($location, FILE_BINARY))
{
if ($img = imagecreatefromstring($img)) $err = 0;
}
if ($err)
{
header('Content-Type: text/html');
echo '<html><body><p style="font-size:9px">Error getting image...</p></body></html>';
}
else
{
header('Content-Type: image/jpeg');
imagejpeg($img);
imagedestroy($img);
}
?>
</body>
</html>

 

Link to comment
Share on other sites

You can't have HTML and an image output by the same page. It just doesn't make sense. The browser has no idea what it's supposed to display. Two options:

 

1.) Write the code that creates the image in one file (lets say image.php ) and make that the source for an img tag:

 

<img src="path/to/image.php" />

 

2.) Are you actually doing any manipulation with this image? Or are you just displaying an image as-is? If so, you really shouldn't be loading it into an image resource and displaying it. Just put the location of the image as the source of the tag.

Link to comment
Share on other sites

I am just trying to display the pic as it is without any modifications. I am retrieving the  location of the image into a variable and then using the tha varibale wherever it is necessary.

 

The main problem with what you have suggested is(of creating 2 files), I am designing this application as a user input application and I will need the user input to run the query and get the image location (As well as a table of results). So I will be definitely needing a HTML form to input the data.

 

Any suggestions on how it can be done?

 

Thanks,

Sashi

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.