Question

I'm trying to write an SQL statement that will generate an SQL script that will update a BLOB field with an IMAGE being selected from the database.

This is what I have:

select concat( 'UPDATE `IMAGE` SET THUMBNAIL = ', 
               QUOTE( THUMBNAIL ), 
               ' WHERE ID = ', ID, ';' ) as UPDATE_STATEMENT
  from IMAGE;

In the above, THUMBNAIL is a BLOB field containing raw image data. When I run the resulting script I get the following error:

ERROR at line 2: Unknown command '\\'.

I first tried this without the QUOTE() function, like so:

select concat( 'UPDATE `IMAGE` SET THUMBNAIL = \'', 
               THUMBNAIL, 
               '\' WHERE ID = ', ID, ';' ) as UPDATE_STATEMENT
  from IMAGE;

Running the resulting script produces this error:

ERROR at line 2: Unknown command '\0'.

What is the proper function to apply to this BLOB field in the select, so the UPDATE statements will work?

If context is required, I'm looking to migrate thumbnails generated on one server to another server for certain image IDs only. I would use mysqldump, but I don't want to clobber the entire table.

Any help is greatly appreciated!

Was it helpful?

Solution

You must work with hexadecimal format blob. It looks like this:

select concat( 'UPDATE `IMAGE` SET THUMBNAIL = ', 
               0xC9CBBBCCCEB9C8CABCCCCEB9C9CBBB...., 
               ' WHERE ID = ', ID, ';' ) as UPDATE_STATEMENT
  from IMAGE;

Or you might find it handy to use the Openrowset command

OTHER TIPS

 <?php if (isset($_POST['sub'])) { 
     $tfname=$_FILES['f1']['tmp_name'];
     $name=$_POST['hi']; 
     $cont=file_get_contents($tfname);
     $cont=addslashes($cont); 
     mysql_connect("localhost" ,"root" ,"");
     mysql_select_db("k"); 
     $sqlstt="update test_image set
         id='2',name='$name',image='$cont' where id='2' ";
     if(mysql_query($sqlstt)) 
         echo "updated"; 
     else 
         echo "not";         
 } ?>

 <form action="" method="post" enctype="multipart/form-data">
     Name:-<input type="text" name="hi" /> 
     select image:-<input type="file" name="f1" required value="select" /> 
     <input type="submit" name="sub" value="update" /> 
 </form>  

this is worked nice check it out

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top