Steve Kim
Steve Kim

Reputation: 5601

mysql BLOB manipulate in php

In mysql, I have a db column with BLOB data type.

The blob data looks something like this:

{\"Name\":\"Mike Smith\",\"Number\":\"1\"}

or

[{\"Name\":\"Mike Smith\"},{\"Name\":\"Jon Doe\",\"Spaces\":\"1\"},{\"Name\":\"My Space(s)\"},{\"Name\":\"Rear\"}]

I am trying to detect the content in php and saving the string as a variable as such:

{\"Name\":\"Mike Smith\",\"Number\":\"1\"} 
BECOMES -> Mike Smith(1)

And...

[{\"Name\":\"Mike Smith\"},{\"Name\":\"Jon Doe\",\"Number\":\"1\"},{\"Name\":\"My Space(s)\"},{\"Name\":\"Rear\"}]   
BECOMES --> Mike Smith, Jon Doe(1), My Space(s), Rear

I am not familiar with BLOB data type and wanted to get some help from the community. How would I manipulate the BLOB data to above?

Any suggestions will be much appreciated.

Upvotes: 0

Views: 101

Answers (1)

Bhavin Thummar
Bhavin Thummar

Reputation: 1293

<?php

$data = '[{\"Name\":\"Mike Smith\"},{\"Name\":\"Jon Doe\",\"Number\":\"1\"},
{\"Name\":\"My Space(s)\"},{\"Name\":\"Rear\"}]';

$data =  json_decode(stripslashes($data))  ;

 $string = '';
 for ($i=0; $i <sizeof($data) ; $i++) { 

 $d = $data[$i];

 if(isset($d->Number)){
    $number = '('.$d->Number.')';
 }
 else{
    $number = '';
 }

 $string .= ($d->Name).$number.',  ';

}


echo $string;

Upvotes: 1

Related Questions