I am trying to display a binary blob from a mysql table. The blob was saved as a OLE Object. My database was originally a mssql7 database that I imported into a mysql database.

It is stored in a field called 'medium_photo'

How can I display the image? You will notice I attempted to display it in my code below...to no avail..

Thank you for any help you can give....

My current code is as follows....

mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");

// Below will display all listings///
//$query="SELECT * FROM listingsDB WHERE Title like '%'";

// Below is an example on how to setup multiple searches for different fields//

$query="SELECT * FROM residential WHERE town like '%$town_listing%'";

//$query="SELECT * FROM listingsDB WHERE Title like '%$title%' AND user_ID = '$user'";

//$query="SELECT * FROM listingsDB WHERE Title like '%$search%'";

$result=mysql_query($query) or die (mysql_error());
$num=mysql_numrows($result);
?>

<?
if(mysql_numrows($result) == '0') {
echo "Sorry, no results were found...";
} else {
// display results here
}
?>

<?
while ($row = mysql_fetch_array($result)) // this will grab the results from query
{
?>
<table border="1" width="100%">
<tr>
<td width="25%"><b><? echo $row['town']; ?></b></td>
<td width="25%">Listing created on:<? echo $row['user_ID']; ?></td>
<td width="25%"><? echo $row['year_built']; ?></td>
</tr>
<tr>
<td width="25%"><? echo $row['medium_photo']; ?></td>
<td width="25%"><? echo $row['lister_office_name']; ?></td>
<td width="25%">&nbsp;</td>
</tr>
<tr>
<td width="25%">&nbsp;</td>
<td width="25%">&nbsp;</td>
<td width="25%">&nbsp;</td>
</tr>
</table>

<?
}
?>
<?

    Hi,

    OLE object... I don't know how these objects are stored but you may try this:

    $row['medium_photo'] -> <img src="medium_photo.php?photo_id=<?= $row['id'] ?>">

    $row['id']: primary key of your town table

    medium_photo.php:
    Get $row['medium_photo'] from $_POST ['photo_id']
    header ("Content-type: image/jpeg");
    echo ($row['medium_photo']);

    It will work if your OLE object is stored as a JPEG... What the OLE object look like ?

    JM

      Where should

      $row['medium_photo'] -> <img src="medium_photo.php?photo_id=<?= $row['id'] ?>">

      $row['id']: primary key of your town table

      Be placed?

      No matter where I place it I get the following error

      Parse error: parse error, expecting T_STRING' orT_VARIABLE' or '{'' or'$'' in /home/httpd/virtual/century21highview.com/htdocs/century21/test.php on line 43

        The files are JPG's 320x208 stored as BLOB's

          I am able to get the first part of the code to display broken images.

          medium_photo.php?photo_id=430729

          I do not think I have medium_photo.php set correctly. Can you give me a little more detail on how to setup this file?

          Thank you for the help.

            Hmmmm....I am still having issues with this. I am pulling out my hair...what little I have left! :p

            So someone look at this code and offer me a suggestion on how to get my binary blob images to show?

            I would appreciate it!

            test.php (Main Script)

            <?
            mysql_connect(localhost,$username,$password);
            @mysql_select_db($database) or die( "Unable to select database");

            // Below will display all listings///
            //$query="SELECT * FROM listingsDB WHERE Title like '%'";

            // Below is an example on how to setup multiple searches for different fields//

            $query="SELECT * FROM residential WHERE town like '%$town_listing%'";

            //$query="SELECT * FROM listingsDB WHERE Title like '%$title%' AND user_ID = '$user'";

            //$query="SELECT * FROM listingsDB WHERE Title like '%$search%'";

            $result=mysql_query($query) or die (mysql_error());
            $num=mysql_numrows($result);
            ?>

            <?
            if(mysql_numrows($result) == '0') {
            echo "Sorry, no results were found...";
            } else {
            // display results here
            }
            ?>

            <?
            while ($row = mysql_fetch_array($result)) // this will grab the results from query
            {
            ?>
            <table border="1" width="100%">
            <tr>
            <td width="25%"><b><? echo $row['town']; ?></b></td>
            <td width="25%">Listing created on:<? echo $row['user_ID']; ?></td>
            <td width="25%"><? echo $row['year_built']; ?></td>
            </tr>
            <tr>
            <td width="25%">

            <img src="medium_photo.php?photo_id=<?= $row['Id'] ?>">

            <? echo $row['medium_photo'];?>
            </td>
            <td width="25%"><? echo $row['lister_agent_name']; ?></td>
            <td width="25%">&nbsp;</td>
            </tr>
            <tr>
            <td width="25%">&nbsp;</td>
            <td width="25%">&nbsp;</td>
            <td width="25%">&nbsp;</td>
            </tr>
            </table>
            <?
            }
            ?>

            medium_photo.php (photo processing script)

            <?
            mysql_connect(localhost,$username,$password);
            @mysql_select_db($database) or die( "Unable to select database");

            $query="SELECT * FROM residential WHERE town like '%$town_listing%'";

            $result=mysql_query($query) or die (mysql_error());
            $num=mysql_numrows($result);
            ?>

            <?
            if(mysql_numrows($result) == '0') {
            echo "Sorry, no results were found...";
            } else {
            // display results here
            }
            ?>

            <?
            while ($row = mysql_fetch_array($result)) // this will grab the results from query
            {
            ?>

            <?
            Get $row['medium_photo'] from $_POST ['photo_id']
            header ("Content-type: image/jpeg");
            echo ($row['medium_photo']);
            ?>
            <?
            }
            ?>
            <?

              What is a "broken image".?

              medium_photo.php must display absolutely nothing before the content header is displayed. Thus, there can be no spaces, space lines, nada before the first line of php code. Otherwise you get a text content header generated and the image will not work. Ditto for the lines following the php code... you add garbage to the image. In your example, you close the php code "?>" insert a blank line, then start php again "<?" before you issue the content header.

              I use output buffering in my image output routine which solved all the problems for me.

              getthumb.php

              <?php
              ob_start(); // turn output buffering on
              // open the database and retrieve the record
              (!($dbh=mysql_connect ("localhost",
              "USERNAME",
              "USERPASS")));
              (!mysql_select_db("DBNAME",$dbh));
              $query = "SELECT thumbnail FROM thumbs
              WHERE thumbid = $thumb";
              (!($result = @ mysql_query ($query, $dbh)));
              $data = @mysql_fetch_array($result);
              $image = $data["thumbnail"];
              header("Content-Type: image/jpeg");
              echo $image;
              ob_end_flush();
              ?>

                I was looking more at your code, and I don't understand why the medium_photo.php code is doing anything more than retrieving a single row which contains the image data.

                I've posted my getthumb.php code which retrieves a single image in the previous message. It gets called by this line

                echo "<img src=\"getthumb.php?thumb={$row["thumbid"]}\">";

                So, you can see there is a one on one relationship between the thumb requested and the thumb returned. So, I'd guess I'd want to know why you get a whole raft of rows all over again using %town_listings%, when all you want is a single row.

                  Hi,

                  <img src="medium_photo.php?photo_id=<?= $row['Id'] ?>">

                  <? echo $row['medium_photo'];?>

                  Remove your echo. Echo is output, so you output your image like you output text... The img tag allows you to insert an image using a PHP script.

                  I was looking more at your code, and I don't understand why the medium_photo.php code is doing anything more than retrieving a single row which contains the image data.

                  BeastRider is right. Why don't you just get the image ? That's why I asked you to pass a photo_id to medium_photo. That script is meant to display 1 image using the unique id.

                  <?php
                  ob_start(); // turn output buffering on
                  // open the database and retrieve the record
                  (!($dbh=mysql_connect ("localhost",
                  "USERNAME",
                  "USERPASS")));
                  (!mysql_select_db("DBNAME",$dbh));
                  $query = "SELECT thumbnail FROM thumbs
                  WHERE thumbid = $thumb";
                  (!($result = @ mysql_query ($query, $dbh)));
                  $data = @mysql_fetch_array($result);
                  $image = $data["thumbnail"];
                  header("Content-Type: image/jpeg");
                  echo $image;
                  ob_end_flush();
                  ?>

                  This is a good example but you don't need the ob_ functions. medium_photo.php is a new PHP script, it's not part of the other script. The idea is to query the DB, create a MIME header (image/jpeg) and output $image using echo. The difference with your echo resides in the header. It indicates that the following data are part of an image.

                  JM

                    I want to thank everyone for helping. I still am having an issue and hope maybe you can help me solve it.

                    Here is what I am doing.....

                    I have one table called residetial with a field called 'medium_photo'. This field is where I am storing OLE binary blob jpegs. I also have a field called 'Id' which displays an id number for each listing.

                    I am trying to display the images and having problems. My php code shows a broken images and points to

                    getthumb.php?thumb=430729 (thumb= changes based on image so this tells me that it is seeing the Id).

                    Here is the code I am an using

                    picture.php
                    ///////////////
                    <?php
                    include("template/generic/top.html");
                    ?>

                    <?php
                    include("test.html");
                    ?>

                    <?
                    $username="XXXX";
                    $password="xxxx";
                    $database="xxxx";

                    mysql_connect(localhost,$username,$password);
                    @mysql_select_db($database) or die( "Unable to select database");

                    // Below will display all listings///
                    //$query="SELECT * FROM listingsDB WHERE Title like '%'";

                    // Below is an example on how to setup multiple searches for different fields//

                    $query="SELECT * FROM residential WHERE town like '%$town_listing%'";

                    //$query="SELECT * FROM listingsDB WHERE Title like '%$title%' AND user_ID = '$user'";

                    //$query="SELECT * FROM listingsDB WHERE Title like '%$search%'";

                    $result=mysql_query($query) or die (mysql_error());
                    $num=mysql_numrows($result);
                    ?>

                    <?
                    if(mysql_numrows($result) == '0') {
                    echo "Sorry, no results were found...";
                    } else {
                    // display results here
                    }
                    ?>

                    <?
                    while ($row = mysql_fetch_array($result)) // this will grab the results from query
                    {
                    ?>
                    <table border="1" width="100%">
                    <tr>
                    <td width="25%"><b><? echo $row['town']; ?></b></td>
                    <td width="25%">Listing created on:<? echo $row['user_ID']; ?></td>
                    <td width="25%"><? echo $row['year_built']; ?></td>
                    </tr>
                    <tr>
                    <td width="25%">
                    <?
                    echo "<img src=\"getthumb.php?thumb={$row["Id"]}\">";
                    ?>
                    </td>
                    <td width="25%"><? echo $row['lister_agent_name']; ?></td>
                    <td width="25%">&nbsp;</td>
                    </tr>
                    <tr>
                    <td width="25%">&nbsp;</td>
                    <td width="25%">&nbsp;</td>
                    <td width="25%">&nbsp;</td>
                    </tr>
                    </table>
                    <?
                    }
                    ?>
                    <?

                    getthumb.php
                    //////////////////

                    <?php
                    ob_start(); // turn output buffering on
                    // open the database and retrieve the record
                    (!($dbh=mysql_connect ("localhost", "XXX", "XXX")));
                    (!mysql_select_db("XXX",$dbh));
                    $query = "SELECT medium_photo FROM residential WHERE Id = $thumb";
                    (!($result = @ mysql_query ($query, $dbh)));
                    $data = @mysql_fetch_array($result);
                    $image = $data["medium_photo"];
                    header("Content-Type: image/jpeg");
                    echo $image;
                    ob_end_flush();
                    ?>

                    Where am I making my mistake?

                      What is a "broken image"? Are you getting a red 'X' or what? Do you have any spaces, line feeds, blank lines... anything that is outside of the actual php code markers in the getthumbs.php routine?

                      A couple of other things to look for. Are you sure that getthumbs is actually returning a record? You can verify that you are successfully retrieving an image record by commenting out the content header line, and putting in a few lines of debug echoes. Then, execute the script outside of the <img src> line. You should see whatever you echo... like the thumb id, other information in the record... and you can echo the image as well... you'll get a giant mess of characters on screen.

                        Originally posted by sgtpepper
                        Yes, a red X

                        The red 'x' indicates that the content header being received by the <img src> line isn't of the image variety, that is, a default header is being generated before you get around to issuing your own content header. I played with this for quite a while before I got it working. Again, if getthumb.php outputs anything before the content header, you are fried.

                        Since the code you have posted is virtually identical to what I use, email me and I will ship you the script in a file.

                          An OLE/image probably has its own header, that's why it can't be displayed correctly. The solution would be to export the OLE objects from your DB to files. I am sure there's a script somewhere to do it.

                          BeastRider, about your script. Why do you use ! and () for the mysql_ function calls ? "(!mysql_select_db("DBNAME",$dbh)); ". I'm just curious 🙂

                          JM

                            sgtpepper,

                            Silly question but how do you manage your DB with MS SQL ? I never tried it and I have no idea how to manage a DB with it. Is MS Access the front end to manage it ?

                            JM

                              Originally posted by goa103
                              BeastRider, about your script. Why do you use ! and () for the mysql_ function calls ? "(!mysql_select_db("DBNAME",$dbh)); ". I'm just curious 🙂JM

                              It is a screwup! I copied the lines from a script where I could output an error message that I could read, ie.,

                              if (!mysql_select_db("DBNAME",$dbh))
                                die("Cannot open the database");
                              

                              There is not much point in doing this in the routine to retrieve the image out of the table since the output is going to an <IMG SRC> statement. An error message or a bad connect, open, or query all give the big red X, ie., I get no useful information without having to do additional debugging anyway.

                              Thanks for pointing that out. Me fix.

                                11 days later

                                Well, I feel alittle embaressed to be telling you all this...but...

                                Apparently, most of the issues have been because somehow, the code I am using to transfer the database from the ODBC to my mysql database, placed the blobs in HEX format.

                                Obviously, this is causing me issues 🙂

                                Can anyone recommend a way I can convert HEX to BINARY?

                                  Originally posted by sgtpepper
                                  Well, I feel alittle embaressed to be telling you all this...but...

                                  Apparently, most of the issues have been because somehow, the code I am using to transfer the database from the ODBC to my mysql database, placed the blobs in HEX format.

                                  Obviously, this is causing me issues 🙂

                                  Can anyone recommend a way I can convert HEX to BINARY?

                                  Wellllllll.... I think it might be time for a bit of a lesson in character representation. There is actually no such thing as blobs in hex format, but you can display blobs formatted as hex, or binary, or octal, or decimal. It all depends upon how you want to view the basic binary data.

                                  Let us start simple. One byte is 8 bits. The possible range of values is 00000000 to 11111111. Thus 00001000 is a valid byte.

                                  Now, you can use this binary data in a variety of ways. First, you can treat the byte as a numeric integer value. Then

                                  00000001 = 1
                                  00000010 = 2
                                  00000011 = 3
                                  00000100 = 4 all the way up to
                                  11111111 = 255

                                  You can also look at the bit patterns as representing ASCII or ANSI characters.

                                  A few selections from with the ASCII world (which is only the lower 128 possible values in a byte)

                                  00001101 (decimal 13) - carriage return
                                  00111101 (decimal 61) - =
                                  01011010 (decimal 90) - capital Z

                                  The ANSI character set includes the first 128 characters and symbols found in the ASCII character set plus the remaining 128 possible values in a byte. Thus,

                                  10110001 (decimal 177) - the (+/-) symbol.

                                  When you look at a typical text dump of a blob in a mysql record, it is displayed in ANSI. That is why you see characters, special symbols, etc. BUt, you could just as easily display it in decimal values as shown above. So, the following ANSI and decimal displays of a blob field are identical. Note: I have put in slashes so that you can see the individual decimal values...the slashes amy or may not be present in a decimal dump

                                  JAC!45/

                                  074/065/067/033/052/053/047

                                  However, in addition to decimal, you can also display the value of a byte in hexadecimal format. So,

                                  00001000 - dec 8, hex 8
                                  00001001 - dec 9, hex 9
                                  00001010 - dec 10, hex A
                                  00001011 - dec 11, hex B
                                  00001100 - dec 12, hex C
                                  00001101 - dec 13, hex D
                                  00001110 - dec 14, hex E
                                  00001111 - dec 15, hex F
                                  00010000 - dec 16, hex 10

                                  So, a byte in hex format runs from 00 to FF, where FF is equivalent to decimal 255.

                                  Then the contents of the mini blob field above look like this. Note: I have put in slashes so that you can see the individual decimal and values...the slashes amy or may not be present in a dump

                                  JAC!45/

                                  074/065/067/033/052/053/047

                                  4A/41/43/21/34/35/2F

                                  So, there are two points to be made here. First, no matter how you represent the binary data in a dump of a blob field, the underlying pattern of the bits remains the same. And thus, it doesn't mean anything to say that your blobs are in hex format. As long as the binary patterns in the source and destination databases are the same, then you got a good copy.

                                  What you need to do is take an ANSI or hex dump of the blob field and determine if you have the correct header field for the image that you are storing. For example, you should always see 'JFIF' in the first few characters of a jpeg image.

                                  Go to www.wotsit.com for a detailed description of many file formats.

                                  Final note. If you are attempting to export a text blob field from one database and then import it into a mysql database, you must be extremely careful that the end of field, end of line, field delimiters, and escape characters are identical in both the program creating the text for export and the importing program or nothing will work right.

                                  I finally gave up trying to use the command line facilities of mysql (mysqldump) to dump a database that would be read by phpMyAdmin... it would never work. Installing phpMyAdmin on my own machine made it a piece of cake.

                                  BeastRider

                                  aka

                                  066101097115116082105100101115

                                  aka

                                  42656173745269646572

                                    hmmmm, now I am wondering if I imported all the data correctly.

                                    The blob doesnt seem like it is a jpeg based on not seeing JFIF

                                    Here is a sample of the first line in my blob

                                    ffd8ffe000104a46494600010201015401540000ffed0b7450686f746f73686f7020332e30003842494d03ed000000000010015400000001000101540000000100013842494d040d000000000004000000783842494d03f300000000000800000000000000003842494d040a00000000000100003842494d271000000000000a000100000000000000023842494d03f5000000000048002f66660001006c66660006000000000001002f6666000100a1999a0006000000000001003200000001005a00000006000000000001003500000001002d000000060000000000013842494d03f80000000000700000ffffffffffffffffffffffffffffffffffffffffffff03e800000000ffffffffffffffffffffffffffffffffffffffffffff03e800000000ffffffffffffffffffffffffffffffffffffffffffff03e800000000ffffffffffffffffffffffffffffffffffffffffffff03e800003842494d0408000000000010000000010000024000000240000000003842494d0414000000000004000000013842494d040c0000000009e30000000100000070000000490000015000005fd0000009c700180001ffd8ffe000104a46494600010201004800480000ffee000e41646f626500648000000001ffdb0084000c08080809080c09090c110b0a0b11150f0c0c0f1518131315131318110c0c0c0c0c0c110c0c0c0c0c

                                    So, this may sound silly, but is there a way I can take my blob data and verify that it is intact? Can I somehow cut and paste and view the image somehow?

                                      Originally posted by sgtpepper
                                      So, this may sound silly, but is there a way I can take my blob data and verify that it is intact? Can I somehow cut and paste and view the image somehow?

                                      The only way to verify since it won't display is to study the header.

                                      Go to www.wotsit.org. Download the jpeg file definition (and other graphic formats if you wish). Every graphic format has a header which enables a graphics program to decode an image.

                                      Look at the dump of your blob. If it doesn't have the header information, then it is invalid. For example, the first two bytes of a jpeg is the header length. The next five bytes will be JFIF followed by hex 0. If you don't have this, you don't have a valid jpeg format (might be another format, but from your dump it doesn't look like it).

                                      No graphic program can decode and image file or blob without header information. Consider the simplest of operations. A blob is a stream of bytes. Without header information, how do you know how to organize those bytes into x rows of y pixels each?