在PHP中,可以使用PDO类来读取和写入BLOB数据。以下是一个简单的示例:
// 连接数据库$dsn = 'mysql:host=localhost;dbname=mydatabase';$username = 'root';$password = 'password';try { $db = new PDO($dsn, $username, $password);} catch (PDOException $e) { die('连接数据库失败:' . $e->getMessage());}// 读取BLOB数据$stmt = $db->prepare('SELECT image FROM images WHERE id = :id');$stmt->bindParam(':id', $id, PDO::PARAM_INT);$stmt->execute();$row = $stmt->fetch(PDO::FETCH_ASSOC);if ($row) { $image_data = $row['image']; // 将BLOB数据写入文件 file_put_contents('image.jpg', $image_data);}// 写入BLOB数据$image_data = file_get_contents('image.jpg');$stmt = $db->prepare('INSERT INTO images (image) VALUES (:image)');$stmt->bindParam(':image', $image_data, PDO::PARAM_LOB);$stmt->execute();在上面的示例中,我们首先连接到数据库,然后从表中读取BLOB数据并将其写入文件。然后我们将文件中的数据读取出来,并将其作为BLOB数据插入到另一个表中。请注意,在写入BLOB数据时,我们使用了PDO::PARAM_LOB来绑定参数。


