insert blob not working (mysql_real_escape_string)

569 Views Asked by At

I am trying to copy image blobs from one table to another, and it is not working, since the mysql_real_escape_string () is no longer available. The special characters in the blob are blocking the insert and I can't figure out how to correct this. Your help would be appreciated. Thank you.

foreach ($dbh->query($query_images) as $images-1) {
  $ins = "INSERT INTO images-2 SET image_blob='".images-1['image_blob']."'";
  $dbh->exec($ins);
}
1

There are 1 best solutions below

2
Michael On

First you have:

 $ins = "INSERT INTO images-2 SET image_blob='".images-1['image_blob']."'";

when you likely want:

 $ins = "INSERT INTO images-2 SET image_blob='".$images-1['image_blob']."'";

but that could just be a typo in your post.

You can do this in pure MySQL and not have to iterate through each row in the images-1 table:

INSERT INTO images-2 (image_blob) VALUES (SELECT image_blob FROM images-1)

If it is some kinda of escaping problem I would suggest going with the PDO stuff in PHP:

$pdo = new PDO('mysql:host=hostname;dbname=db', $user, $pass);
foreach($dbh->query($query_images) as $images-1) {
    $sth = $pdo->prepare("INSERT INTO images-2 SET image_blob=?")
    $sth->execute([$images-1['image_blob']]);
}

I did not test any of this code but just put it together based on documentation and experience.