fdz
fdz

Reputation: 73

How to get data from mysql db which saved as an array

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

Upvotes: 0

Views: 137

Answers (4)

Peter
Peter

Reputation: 145

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>';
});

Upvotes: 0

ceciliajoanna
ceciliajoanna

Reputation: 51

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"

Upvotes: 1

krish
krish

Reputation: 219

try this

Use the FIND_IN_SET function:

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

Upvotes: 0

krishna
krishna

Reputation: 4089

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

Upvotes: 0

Related Questions