Jump to content

Copying DB row that has a BLOB element


jumbo

Recommended Posts

Hi

 

im trying to copy a row from table1 unto table2. The row contains 2 text fields and a blob object. The two tables have the same exact structure. My code goes like:

 


$query1= mysql_query("SELECT * FROM table1 WHERE ID='$id' ");
$num1 = mysql_num_rows($query1);
        
if($num1 == 0)
{
$error.="Wrong ID";
die();
}
                
$result1 = mysql_fetch_array($query1);
                
$query2= "INSERT INTO table2 VALUES ("'{$result1[iD]}','{$result1[text]}','{$result1[pic]}')";  

 

 

So the BLOB element is a picture and is the last value in the row. The code above returns a MySQL error: "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1"

 

What is wrong in my code? do i need to treat the BLOB element differently than the other text fields?

 

Thank you

Link to comment
https://forums.phpfreaks.com/topic/201867-copying-db-row-that-has-a-blob-element/
Share on other sites

I was thinking about doing this in 1 MySQL query. Then you avoid transfering the binary code from your image with Php to the new query. It is much faster. You might want to try this query instead:

 

<?php

$Q = sprintf(
"INSERT INTO table2
SELECT ID, text, pic 
FROM table1
WHERE ID = '%s'",
mysql_real_escape_string($id)
);

$R = mysql_query($Q) or die(mysql_error());

?>

 

Please let me know if it worked  ;)

 

 

And btw to solve the error in your code, replace this line:

 

$query2= "INSERT INTO table2 VALUES ("'{$result1[iD]}','{$result1[text]}','{$result1[pic]}')"; 

 

With this line:

 

$query2= "INSERT INTO table2 VALUES ('{$result1[iD]}','{$result1[text]}','{$result1[pic]}')"; 

 

I was thinking about doing this in 1 MySQL query. Then you avoid transfering the binary code from your image with Php to the new query. It is much faster. You might want to try this query instead:

 

 

You're right!! doing it in 1 MySQL query makes it work! I thought about the PHP having to handle binary data, but i couldnt make sure it got distorted that way!

 

Thanx a lot for your help!

Admins can mark this thread as [solved]

 

PS: the extra quote was a typo, im sorry

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.