Link to home
Start Free TrialLog in
Avatar of puneetdudeja
puneetdudejaFlag for India

asked on

BLOB field in a table returned as empty string on being fetched through php script.

I have a table in which I have a blob field in which i am storing an image data after base64 encoding it. When I fetch that record from php,  i get all fields correct except the BLOB field which is retrieved as empty string.

I can't figure it out, why it is happening ?
Avatar of Shinesh Premrajan
Shinesh Premrajan
Flag of India image

what is the database you are using.. ?
Avatar of puneetdudeja

ASKER

Mysql
can you please include your code and ill have a look for u mate.
Please see the code below::

When I fetch the record, I get both the fields "ImgId and MimeType" correct, but "Image" field as empty string.
$params = $this->_request->getParams();
		
		
		if(isset($params['submitted']))
		{
			$fp = fopen($_FILES['file1']['tmp_name'], "rb"); 
            $content = base64_encode(fread($fp, filesize($_FILES['file1']['tmp_name']))); 

			
			$bubble->insertBubble($content,$_FILES['file1']['type']);
			move_uploaded_file($_FILES['file1']['tmp_name'],'/home/puneet/public_html/u2me/public/abc.gif');
			echo 'inserted ... ';
		}
		else
		{
			$arr = $bubble->fetchBubble();
			echo '<pre>';print_r($arr);echo '</pre>';

		}



public function insertBubble($data,$mime)
	{
		$arr = array( 'Image' =>  $data,
            'MimeType' => $mime
            );
		$db = $this->getAdapter();
		
		$db->insert('bubble_messages',$arr);
		

	}
	
	public function fetchBubble()
	{	
		$sql = 'SELECT * FROM bubble_messages order by ImgId desc';
		$db = $this->getAdapter();
		$res = $db->query($sql);
		$result = $res->fetch();
		return $result;
	}


CREATE TABLE `bubble_messages` (
  `ImgId` int(11) NOT NULL auto_increment,
  `Image` longblob,
  `MimeType` varchar(100) default NULL,
  PRIMARY KEY  (`ImgId`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

Open in new window

try this see what u get..
<?php
$params = $this->_request->getParams();
                
                
                if(isset($params['submitted']))
                {
                        $fp = fopen($_FILES['file1']['tmp_name'], "rb"); 
            $content = base64_encode(fread($fp, filesize($_FILES['file1']['tmp_name']))); 

                        
                        $bubble->insertBubble($content,$_FILES['file1']['type']);
                        move_uploaded_file($_FILES['file1']['tmp_name'],'/home/puneet/public_html/u2me/public/abc.gif');
                        echo 'inserted ... ';
                }
                else
                {
                        $arr = $bubble->fetchBubble();
                        echo '<pre>';print_r($arr);echo '</pre>';

                }



 function insertBubble($data,$mime)
        {
                $arr = array( 'Image' =>  $data,
            'MimeType' => $mime
            );
                $db = $this->getAdapter();
                
                $db->insert('bubble_messages',$arr);
                

        }
        
        function fetchBubble()
        {       
                $sql = 'SELECT * FROM bubble_messages order by ImgId desc';
                $db = $this->getAdapter();
                $res = $db->query($sql);
                $result = $res->fetch();
                return $result;
        }

?>

Open in new window

also.. i believe u are using zend frame work.. im not to hot on this but.. i also think u need to change line 2.. to the following...

$params = $this->_request->getParam('params');
ok i think i got it.. this returns a blank page for me.. there will be an error with my previous code.. on line 2. that is because $params is being caled out of a function.. so try this and let me know how it goes.. i get a blank page for this when i run it.. so hopefully u will see some data..


<?php
function param($params)
{
$params = $this->_request->getParams(params);


                if(isset($params['submitted']))
                {
                        $fp = fopen($_FILES['file1']['tmp_name'], "rb");
            $content = base64_encode(fread($fp, filesize($_FILES['file1']['tmp_name'])));


                        $bubble->insertBubble($content,$_FILES['file1']['type']);
                        move_uploaded_file($_FILES['file1']['tmp_name'],'/home/puneet/public_html/u2me/public/abc.gif');
                        echo 'inserted ... ';
                }
                else
                {
                        $arr = $bubble->fetchBubble();
                        echo '<pre>';print_r($arr);echo '</pre>';

                }



 function insertBubble($data,$mime)
        {
                $arr = array( 'Image' =>  $data,
            'MimeType' => $mime
            );
                $db = $this->getAdapter();

                $db->insert('bubble_messages',$arr);


        }

        function fetchBubble()
        {
                $sql = 'SELECT * FROM bubble_messages order by ImgId desc';
                $db = $this->getAdapter();
                $res = $db->query($sql);
                $result = $res->fetch();
                return $result;
        }

?>

Open in new window

NeoAshura, u have not changed anything in the code, and perhaps i think the problem is not with the syntax of the code, there is some basic thing missing out of it. Maybe some setting in mysql server or something we don't know.
perhaps if u look more closely u will see that

<?php
function param($params)
{
$params = $this->_request->getParams(params);
 

is changed.

public function is now called function..

my advice is dont use Zend frame work. ur more likely to get more help/
you cannot call a class outside of a function which u did previously.
ASKER CERTIFIED SOLUTION
Avatar of puneetdudeja
puneetdudeja
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial