Re: Retrieving Image Location in PHP from MySQL

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



I am just storing the location of the image(as a varchar type), not the image itself. For example for a particular image, the location is stored as C:\wamp\bin\apache\apache2.2.8\htdocs\Bldgs_lots\Burruss.jpg

-sashi

Nathan Nobbe wrote:


On Sat, Mar 7, 2009 at 7:10 PM, Sashikanth Gurram <sashi34u@xxxxxx <mailto:sashi34u@xxxxxx>> wrote:

    Hello everyone,

    I am using a WAMP server for my coding purposes. My MySQL Version
    is 5.0.51b, PHP version is 5.2.6 and Apache version is 2.2.8

    I have created a database with one of the tables containing a
    location of the image. Using PHP I am trying to retrieve the
    location of the image (from the mysql db) and display it in a
    browser. I have, with help from php-db mailing list user's help,
    created a code to do the same. Now, the problem is, the code is
    extracting the location details of the image exactly and it is
    even getting the image details. But it is simply not dispalying
    the image. Instead of displaying the image, it is printing a hell
    lot of binary characters (For example: 2!
!22222222222222222222222222222222222222222222222222ÿÀ ^ " ÿÄ ÿĵ } !1A Qa "q 2 ‘¡ #B±Á RÑð$3br‚ %&'()*456789:CDEFGHIJSTUVWXYZcdefghijstuvwxyzƒ„…†‡ˆ‰Š’“”•–—˜™š¢£¤¥¦§¨©ª²³´µ¶·¸¹ºÂÃÄÅÆÇÈÉÊÒÓÔÕÖ×ØÙÚáâãäåæçèéêñòóôõö÷øùúÿÄ
                        ÿĵ              w      !1  AQ aq "2   B‘¡±Á
#3Rð brÑ $4á%ñ &'()*56789:CDEFGHIJSTUVWXYZcdefghijstuvwxyz‚ƒ„…†‡ˆ‰Š’“”•–—˜™š¢£¤)

    I have tried in two forums and mailing lists but none of them have
    completely answered my question. How do I get rid of these signs
    and display the original image in its place? And to display the
    image successfully, is it really necessary to create the image
    retrieval and displaying code in a seperate PHP file? Any PHP
    gurus, enthusiasts, newbies out there, Please help me out.

    I am posting the code below so that you can directly see what I
    have done till now. The name of the php file is mysqli.php. The
    location of the image is stored to the variable $location. The
    image retrieval code is present towards end of the file.

    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="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>
    <?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);
    echo '<img src="mysqli.php?&img=' . $location . '" border="1"
    height="150" width="200" alt="' . $build . '">';
    imagedestroy($img);
    }
    ?>
    </body>
    </html>


how are you storing the image in the database? i believe youll want to make it a BLOB of some type. also, depending on the application, many folks prefer to just place images directly on the filesystem, then reference their location on the filesystem in the database.

-nathan



--
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Sashikanth Gurram
Graduate Research Assistant
Department of Civil and Environmental Engineering
Virginia Tech
Blacksburg, VA 24060, USA


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux