0

I'm using CONCAT to combine multiple columns,

SELECT CONCAT(col1, ' - ', col2) AS result
FROM table 
WHERE col1 LIKE '%apple%' OR
      col2 LIKE '%apple%' 
ORDER BY col1 ASC

And then return it as json data.

while ($row = mysql_fetch_array($query)) {
    $data[] = $row['result'];
}
echo json_encode($data);

Question: How can I separate the data into its individual field when I read it in jQuery? Is it better (more efficient) to send it separately?

<script>
   $(function() {
    $( "#AllWord" ).autocomplete({
      source: 'search_apple.php'
    });
   });
</script>
tarako
  • 127
  • 3
  • 10
  • why would you concat the 2 columns if you want the seperate values? –  Sep 16 '16 at 09:04
  • it's for autocomplete form. showing the two fields instantly. then separate it for other purposes. I can do the other way round too. – tarako Sep 16 '16 at 09:16

1 Answers1

0

Best will be to send it individually, so the query without the concat.

Encoding:

while ($row = mysql_fetch_array($query)) {
    $data[] = $query; // No need to create another array, it is already correct
}
echo json_encode($data);

You can still concat (best place will be in the query itself), next to col1 and col2 individually if you need to concat.

And last remark:
Please don't make use of the mysql_* functions.
Please use mysqli_* or PDO.
More information here: Why shouldn't I use mysql_* functions in PHP?

Blaatpraat
  • 2,829
  • 11
  • 23