嗨,我需要通过 BLOB 将图像(用户的个人资料图片)存储到 mySQL DB,现在我知道更好的方法是将图像存储到服务器的文件系统并仅将它们的 url 保存到 DB,但不是我老板 :),所以我将图像存储到数据库!我有适用于 android 的 java 脚本的 php 代码(使用 base64 转换为二进制数据),我现在需要将它用于 iPhone:这是我需要用来插入图像的 php:
<?php
$base = $_REQUEST['image'];
$filename = $_REQUEST['filename'];
//connect to the db
$user = ‘root’;
$pswd = '';
$db = ‘test’;
$conn = mysql_connect(‘localhost’, $user, $pswd);
mysql_select_db('test');
$query = "INSERT INTO `test`.`photos` (`id`, `image`) VALUES (NULL,'$base')";
mysql_query($query) or die(mysql_error());
echo "Image id is ".mysql_insert_id();
echo $base;
?>
这用于获取图像:
<?php
$id = $_REQUEST['id'];
$username = "root";
$password = "";
$host = "localhost";
$database = "test";
mysql_connect($host, $username, $password) or die("Can not connect to database: ".mysql_error());
mysql_select_db($database) or die("Can not select the database: ".mysql_error());
//select image
$query = "SELECT * FROM `photos` WHERE id =30 LIMIT 0 , 30";
$sql = mysql_query($query) or die(mysql_error());
while($image = mysql_fetch_array($sql)){
echo $image['image'];
}
?>
我正在尝试做这样的事情来存储图像:
//converting image to data end to binary data (base64)
NSData* data = UIImagePNGRepresentation(Image);
[Base64 initialize];
NSString *strEncoded = [Base64 encode:data];
//prepare request
NSString *strURL = @"My URL Here.";
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:
[NSURL URLWithString:strURL]];
[request setDelegate:self];
[request setData:strEncoded withFileName:@"image.png" andContentType:@"image/png" forKey:@"image"];
[request startAsynchronous];
我在 mySQL 中看到它添加了新的 BLOB,但没有我的图像 ([BLOB-0B])。我丢失图像数据的任何想法?