0

I have a table named profile like this

S/N   Name   Age (VALUES) (1, Julius, 5 ) (2, Mark, 7) (3, Ismael, 9) 

I want to display a result that will fetch the name row and separate the values with comma, e.g.

$result = Julius, Mark, Isamel

I used

$resuk = mysqli_query($conn, "SELECT GROUP_CONCAT(Name) FROM profile;");
      $get_info = mysqli_fetch_assoc($resuk); 
 $values = explode(",", $get_info);
       echo $values;

This gave me below error

Warning: explode() expects parameter 2 to be string, array given in C:\xampp\htdocs\dowel\commasep.php on line 

I would appreciate if someone can assist. Thanks.

1 Answers1

0

mysqli_fetch_assoc() function fetches a result row as an associative array.

So your $get_info variable is basically a whole row, you need to explode the FIELD and NOT the whole ROW

You should be able to achieve this by assigning a column alias from your query result like so

mysqli_query($conn, "SELECT GROUP_CONCAT(Name) as AllNames FROM profile;");

And then you can access that column in the explode function like so

$values = explode(",", $get_info['AllNames']);
slashroot
  • 773
  • 1
  • 4
  • 13
  • 1
    Thanks a lot @slashroot. Your response shed more light on my issues and i was able to finally resolve it even without the explode. $resuk = mysqli_query($conn, "SELECT GROUP_CONCAT(Name) as AllNames FROM profile;"); $get_info = mysqli_fetch_assoc($resuk); $result = $get_info['AllNames']; echo $result ; $result = Julius, Mark, Isamel Thanks a million – Omotayo Iyiola Oct 10 '21 at 13:10