0

Hi I need to store images (user's profile picture) to mySQL DB via BLOBs, now I understand that the better way is to store images to server's file system and to save only url for them to DB,but not me the boss :) , so I'l store image to DB ! I have the php code that working nice for java script for android (using converting to binary data with base64) , I need to use it now for iPhone:this is php I need to use to insert image:

    <?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;

?>

And this for get image:

<?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'];
}
?>

I'm trying to do something like this to store 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];

And I see in mySQL that it add new BLOB, but without my image ([BLOB-0B]). Any ideas where I lose my image data?

2 Answers 2

1

I found answer to my question: I only need to change :$base = $_REQUEST['image']; to $base = $_POST['image'];

Sign up to request clarification or add additional context in comments.

Comments

0

Use the following for PHP/Mysql

When you fetch the result from DB(image file), store the content into a file.. something like

file_put_contents('User_photo.png', $image['image'] );

If you are able to view the file(as image), then Your PHP code is fine...

Select query has to be only for image column (select image from photo)

1 Comment

my php code fine, because it working for android (java) I need to know what wrong with my objective-c code :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.