-1

I am trying to select data from my database and display it like a string. I know you would need to change it to an array and then to a string, but I don't know how. When I run the mysql query:

"SELECT 'plant (cm)' FROM PlantDATA"
I get the following:
0.10
0.25
0.33
I really would like to get it to a string like:
0.10, 0.25, 0.33
so I could feed it to my bootstrap chart.

EDIT: My database looks like this: {id, date, plant (cm)}

1 | 01-02-2019 | 0.10
2 | 03-02-2019 | 0.25
3 | 06-02-2019 | 0.33

2 Answers2

0

Just a suggestion .. If you want select a column named 'plant (cm)' then you should use backtics

 "SELECT `plant (cm)` FROM PlantDATA"

and if you want comma separated result you could use group_concat

"SELECT group_concat(`plant (cm)`) FROM PlantDATA"
ScaisEdge
  • 131,976
  • 10
  • 91
  • 107
  • 2
    If this is *Just a suggestion* - surely a comment would be better. If this solves the problem then it is just a duplicate of https://stackoverflow.com/questions/11321491/when-to-use-single-quotes-double-quotes-and-back-ticks-in-mysql – Nigel Ren Feb 06 '19 at 17:34
  • .. i don't know your data content so i can't eval your result .. update yuor question add a clear data sample and the expected result – ScaisEdge Feb 06 '19 at 17:37
  • Hi, thanks for the help, I used group_concat like you said (with some other code) and it's working! –  Feb 07 '19 at 19:52
  • Hi, thank you for your comment, but it only helped partially. I had to add more code to let it work, so you didn't actually solve my issue. I am still thankfull tho! –  Feb 10 '19 at 09:25
0

I kinda solved my own question by surfing on the internet. So here's my working code:

// Get the 'water (cm)' from database to chart
            $query="SELECT GROUP_CONCAT(`plant (cm)` SEPARATOR ', ') FROM PlantDATA ORDER BY date";
            $result = mysqli_query($conn, $query);
            foreach($result as $category) { }

            $res_arr = implode(',',$category);
            //print_r($res_arr);

I used the GROUP_CONCAT so I get an output like this: 0.1, 0.25, 0.33 (already from running the query). Then execute the query and with foreach save it in one, then implode it as others were saying (and the print_r($res_arr); at the end was just for debugging purposes.

In the chart area I used the print_r($res_arr); in php formation and it worked!

So Thanks everyone!