MySql Query result only first column

My query that i use is

SELECT item1, item2, item3, item4 FROM table  WHERE id ='99'

should result 1 2 3 4 5 6 7 8, but i got only 1 , can i get them all in 1 column like Items ?

+-------+--------+----------+---------+---------+
| id    | item1  |   item2  |  item3  |  item4  |   
+-------+--------+----------+---------+---------+
|  99   |    1   |     2    |    3    |    4    | 
|  99   |    5   |     6    |    7    |    8    | 
|  92   |    1   |     2    |    3    |    4    | 
|  92   |    1   |     2    |    3    |    4    | 
+-------+--------+----------+---------+---------+

Function

function getInv($id)
    {
        global $database;
        $stmt = $dbh->user("SELECT CONCAT(item1,', ',item2,', ',item3,', ',item4) Items
        FROM table  WHERE id= ? ORDER BY Items");
        $stmt->bindParam(1, $id, PDO::PARAM_INT);
        $stmt->execute();
        $result = $stmt->fetchAll(PDO::FETCH_COLUMN);
        
        if($result)
        return convert_number($result[0]);
        else {
        return '---';
    }

View Page

<?php print getInv($user['id']); ?>

>Solution :

To produce your expected result you could use:

select group_concat(CONCAT_WS(' ',item1, item2, item3, item4) SEPARATOR ' ')  as my_column
FROM my_table  WHERE id =99
group by id;

Result:

my_column
1 2 3 4 5 6 7 8

Demo

Leave a Reply