0

I want to get the data from my mysql db , but one column is saved data as an array ... How can i get the values separately . Please advice...

This is how Data saved in db for the dropdown values

{"value":["Authority ","Boards"],"order":["1","2"]}

mysql query

SELECT a.select FROM sltdb_cddir_fields a WHERE a.categories_id=81

What i want is to get Authrity and Boards as two data values instead of this array ...

Please advice

 $searchg=$_GET["term"];
// $query=mysql_query("SELECT * FROM sltdb_cddir_content where title like '%".$searchg."%'AND categories_id=81 order by title ASC ");
$query=mysql_query ("SELECT a.select FROM sltdb_cddir_fields a WHERE  a.select like '%".$searchg."%'  a.categories_value_id=19 ");
$json=array();
    while($display=mysql_fetch_array($query)){
         $json[]=array(
                    'value'=> $display["title"],
                    'label'=>$display["title"]
                        );
    }

 echo json_encode($json);
?>
4
  • after you get data use unserialize to convert into array Feb 25, 2014 at 12:54
  • @Karthick Kumar Ganesh : i wrote a query to get data but its output the same as which saved in array . you ask me to unserialize the query results is it ?
    – fdz
    Feb 25, 2014 at 12:55
  • why you are converting this to json array ? Feb 25, 2014 at 12:59
  • @Karthick Kumar Ganesh : im using this for my jquery autocomplete and this json array is passed as source url
    – fdz
    Feb 25, 2014 at 13:06

4 Answers 4

1

The data format stored is in JSON format. Hence, you could obtain the data and parse them using the function json_decode(); For instance:

    $data = '{"value":["Authority ","Boards"],"order":["1","2"]}';
    $objJSON = json_decode($data);
    $values = $objJSON->value;  //contains "Authority ","Boards"
    $orders = $objJSON->order; //contains "1","2"
0

you can use implode to change array to string like this

while($display=mysql_fetch_array($query)){
         $json[]=array(
                    'value'=> implode(",", $display["title"]),
                    'label'=>implode(",", $display["title"])
                        );
    }

or even you can use serialize() instead of implode

1
0

try this

Use the FIND_IN_SET function:

SELECT t.*
  FROM YOUR_TABLE t
 WHERE FIND_IN_SET(3, t.ids) > 0
0

Mysqli offers several result types, mysqli_fetch_all() for instance returns an associative array. If you want the data formatted in a non standard way, chances are you'll need to loop through it and build your data structures yourself.

$json = array();

while($row = mysqli_fetch_assoc($result)) {
    $json[$row['order']] = $row['value'];
}

return json_encode($json);

In JavaScript:

$.each(json, function(key, value) {
    html += '<option value="' + key + '">' + value + '</option>';
});
4
  • i want this to on my json encode code, i have update my question as well , how to use this for that code . please advice
    – fdz
    Feb 25, 2014 at 12:58
  • You should make an associative array, json_encode will convert that to a javascript object, then you can use $.each() from jquery to iterate through your object.
    – Peter
    Feb 25, 2014 at 13:01
  • im using this for jquery autocomplete jQuery(function(){ jQuery("#searchp").autocomplete({ , how can i put $.each, pls advice
    – fdz
    Feb 25, 2014 at 13:08
  • you please check this kindly stackoverflow.com/questions/22163466/…
    – fdz
    Mar 4, 2014 at 18:24

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.