Display image in MYSQL

Discussion in 'Scripts, 3rd Party Apps, and Programming' started by acellec, Jun 17, 2009.

  1. misson

    misson Community Paragon Community Support

    Messages:
    2,572
    Likes Received:
    72
    Trophy Points:
    48
    The full output from the webserver (including headers) is:
    Code:
    HTTP/1.1 200 OK
    Date: Mon, 22 Jun 2009 04:38:17 GMT
    Server: Apache/2.2.11 (Freewebhostingarea.com) mod_perl/2.0.4 Perl/v5.10.0
    X-Powered-By: PHP/5.2.9
    Content-Length: 3
    Connection: close
    Content-Type: image/jpeg
    
    
    
     
    The 3 byte content is two carriage returns and a space ("\n\n "). The URL displayed in the client window seems to be generated by the browser (@xav0980: you wouldn't happen to be using Firefox, would you?). If it weren't for the fact that IndexProject.php dumps binary data into the table, I'd wonder if the bmkimage field were empty for row where PartNo='APC403062CLE'. Try fetching rows as an associative array rather than the integer indexed array. While you're at it, you might as well specify in $SQLstring exactly what's being fetched:
    PHP:
    <?php
    //get_image.php

    // sanitized ID
    $image_id preg_replace('/\W.*/'''$_GET['image']);

    $TableName "automobileparts";
    $SQLstring "SELECT bmkimage FROM automobileparts WHERE PartNo = '$image_id'";

    require_once(
    "dbconnect.php");

    header('Content-Type: image/jpeg');
    $Row$QueryResult->fetch_assoc();
    echo 
    $Row['bmkimage']; 

    $DBConnect->close();
    ?>
    If that still doesn't work, try var_dump($Row) rather than just printing $Row['bmkimage'] so you can see exactly what was returned by the query.


    That dbconnect.php has been bugging me for a bit. It appears to rely too much on global variables and performs actions without performing their counterparts. This all makes the script harder to maintain. Also, separating definitions from actions will make it easier to reuse code. Try something like this, to start with:
    PHP:
    <?php
    //dbconnect.php
    function local_db_connect($driver='mysqli'$user='dfltUser'$pw='*****'$db='') {
        return new 
    $driver('localhost'$user$pw$db);
    }
    ?>
    This provides a flexible yet secure way of creating connections. It's used as follows:
    PHP:
    <?php
    //get_image.php
    require_once("dbconnect.php");

    $dbConn local_db_connect();
    // note: be sure to change database name to a real one
    $dbConn->select_db('some_database'); 
    // sanitized ID; could also do: $image_id = preg_replace('/\W.*/', '', $_GET['image']);
    $image_id $dbConn->escape_string($_GET['image']);
    $tableName "automobileparts";

    if (
    $dbConn) {
        
    $SQLstring "SELECT bmkimage FROM $tableName WHERE PartNo = '$image_id'";
        if (
    $queryResult=$dbConn->query($SQLstring
            and 
    $row=$queryResult->fetch_assoc()) 
        {
            
    header('Content-Type: image/jpeg');
            echo 
    $row['bmkimage']; 
            
    $dbConn->close();
        } else {
            
    // errordoc/404.php is the custom 404 error page.
            
    include('errordoc/404.php');
        }
    } else {
        
    header('Content-Type: text/plain');
        echo 
    "Couldn't connect to image database. Something is probably wrong with the server. Please try again later.";
    }
    ?>
    Using exceptions, the bulk of that script could be refactored as a function (named "fetch_single()", perhaps) that runs a query and returns a single result, resulting in a very short script, but that's a matter for another time.


    Go for it, rough as it is. If you think of any improvements for the wording, please let me know. In particular, "these guidelines" is too much like "click here". Maybe "Eric Raymond's guidelines"?

    Speaking of "click here", I would have thought people understood the concept of hypertext by now, but considering how often you see it... Of course, considering that some people appear to need to be told to "click here", maybe they're on to something.
     
  2. acellec

    acellec New Member

    Messages:
    29
    Likes Received:
    0
    Trophy Points:
    0
    hello i tried...but still doesnt display..


    do i need to put the code above in my index.php or in my get_image.php?



    I tried the above code using my localhost... i use echo $Row['bmkimage'];

    i have an error message :

    Warning: Cannot modify header information - headers already sent by (output started at D:\xampp\htdocs\xampp\Project5\dbconnect.php:10) in D:\xampp\htdocs\xampp\Project5\get_image.php on line 12
    ÿØÿà�JFIF��H�H��ÿá%@Exif��MM�*������������������b�������j(�������1���� ���r2�������~‡i�������’���¼���H������H���


    i tried var_dump($Row)..echo var_dump($Row);


    and have this error message:

    Warning: Cannot modify header information - headers already sent by (output started at D:\xampp\htdocs\xampp\Project5\dbconnect.php:10) in D:\xampp\htdocs\xampp\Project5\get_image.php on line 12
    array(1) { ["bmkimage"]=> string(242987) "ÿØÿà�JFIF��H�H��ÿá%@Exif��MM�*������������������b�������j(�������1���� ���r2�������~‡i�������’���¼���H������H���GIMP 2.4.2��2009:04:01 14:00:59�� ����ÿÿ�� ������ì �

    when i uploaded my php in myfree webhost
    it looks like this one

    http://acemysql.freetzi.com/get_image.php?image=APC403062CLE

    by the way i am using firefox
    Edit:

    this one i already tried it displays garbage characters...no images
    Warning: Cannot modify header information - headers already sent by (output started at D:\xampp\htdocs\xampp\Project5\dbconnect.php:6) in D:\xampp\htdocs\xampp\Project5\get_image.php on line 17
    NULL
    Edit:
    tried to put the header like this

    PHP:
    <?php

    header
    ('Content-Type: image/jpeg');
    $image_id preg_replace('/\W.*/'''$_GET['image']);
    no warning error but the image doesnt display and it displays

    [​IMG]
     
    Last edited: Jun 22, 2009
  3. acellec

    acellec New Member

    Messages:
    29
    Likes Received:
    0
    Trophy Points:
    0
    tried to put the header like this

    PHP:
    <?php

    header
    ('Content-Type: image/jpeg');
    $image_id preg_replace('/\W.*/'''$_GET['image']);
    no warning error but the image doesnt display and it displays

    http://localhost/xampp/Project4/get_image.php?image=APC403062CLE

    my APC403062CLE row is not empty attached is my dbase
     

    Attached Files:

    Last edited: Jun 22, 2009
  4. acellec

    acellec New Member

    Messages:
    29
    Likes Received:
    0
    Trophy Points:
    0
    i tried a code like this just to check

    PHP:
    <?php
    $username 
    "root";
    $password "";
    $host "localhost";
    $database "automobile_parts";

    @
    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());



    $query mysql_query("SELECT bmkimage FROM automobileparts WHERE PartNo='APC402029BLE'");
    $row mysql_fetch_array($query);
    $logo $row['bmkimage'];

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

    ?> 
    it displays only one picture...i dont know if it doesnt catch the value from $_GET['image'] which is in my Index.php
     
  5. galaxyAbstractor

    galaxyAbstractor Community Advocate Community Support

    Messages:
    5,508
    Likes Received:
    35
    Trophy Points:
    48
    uh, I would save the image as a file, then put the path in the database, but I don't know if that is the correct way, as my webdesign teacher disliked it...

    Say, you have an image called image.png.
    You put it in /images/, and save the path "http://forums.x10hosting.com/images/image.png"

    in the database as text.

    Then when you do <img src="<?php echo $row['image'] ?>" />, it would turn out <img src="http://forums.x10hosting.com/images/image.png" />.


    Ignore the forum url, somehow it get's added automatically
     
    Last edited: Jun 22, 2009
  6. acellec

    acellec New Member

    Messages:
    29
    Likes Received:
    0
    Trophy Points:
    0
    ok i did what you say i have this
    echo "<td align='right'><img src='{$Row[6]}' /></td>";

    but it doesnt display the picture on the text
    http://acemysql.freetzi.com/images/img1.jpg
     
  7. misson

    misson Community Paragon Community Support

    Messages:
    2,572
    Likes Received:
    72
    Trophy Points:
    48
    After reading over your various posts, it looks like something is going on in dbconnect.php. Add better error handling to your code and learn exactly what it is you're doing. The cookbook approach fails early an often (as you're experiencing).

    Read the first line more carefully. That's the output of the webserver when returning the image. Do you understand HTTP headers?


    Do you understand how header() works?

    The script was sending the binary image data, so no need for var_dump.



    Take a closer look. This script searches for a fixed PartNo, which is why it always returns the same picture.
     
  8. xav0989

    xav0989 Community Public Relation Community Support

    Messages:
    4,467
    Likes Received:
    95
    Trophy Points:
    0
    Man with all the problems that this script has gotten into, I think it would be best to start from sratch again, and follow a tutorial on saving images in MySQL. This one, posted by misson I believe is really good.
     

Share This Page