Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading