BLOB是一种MySQL数据类型,称为二进制大对象。正如它的名字它是用来存储类似MYSQL二进制和VARBINARY类型的、大量的字符串数据。
MySQL BLOB分类
LONGBLOB ((2 ^ 32)1)
CREATE TABLE IF NOT EXISTS `output_images` ( `imageId` tinyint(3) NOT NULL AUTO_INCREMENT, `imageType` varchar(25) NOT NULL DEFAULT '', `imageData` mediumblob NOT NULL, PRIMARY KEY (`imageId`) )
<?php if(count($_FILES) > 0) { if(is_uploaded_file($_FILES['userImage']['tmp_name'])) { mysql_connect("localhost", "root", ""); mysql_select_db ("phppot_examples"); $imgData =addslashes(file_get_contents($_FILES['userImage']['tmp_name'])); $imageProperties = getimageSize($_FILES['userImage']['tmp_name']); $sql = "INSERT INTO output_images(imageType ,imageData) VALUES('{$imageProperties['mime']}', '{$imgData}')"; $current_id = mysql_query($sql) or die("<b>Error:</b> Problem on Image Insert<br/>" . mysql_error()); if(isset($current_id)) { header("Location: listImages.php"); }}} ?> <HTML> <HEAD> <TITLE>Upload Image to MySQL BLOB</TITLE> <link href="imageStyles.css" rel="stylesheet" type="text/css" /> </HEAD> <BODY> </p> </BODY> </HTML>
<?php $conn = mysql_connect("localhost", "root", ""); mysql_select_db("phppot_examples") or die(mysql_error()); if(isset($_GET['image_id'])) { $sql = "SELECT imageType,imageData FROM output_images WHERE imageId=" . $_GET['image_id']; $result = mysql_query("$sql") or die("<b>Error:</b> Problem on Retrieving Image BLOB<br/>" . mysql_error()); $row = mysql_fetch_array($result); header("Content-type: " . $row["imageType"]); echo $row["imageData"]; } mysql_close($conn); ?>
" />
List BLOB Images ログイン後にコピー