make SQL query return in an organized way

Asked

Viewed 109 times

0

I have the following SQL table:

|`id` | `user_id` | `key` | `value` |
|------------------------------------
| 1   |    1      |  nome | João da silva, 50
| 2   |    1      |  docs | 552.282.820-58, 22.532.25, 1.230.253-15
| 3   |    1      |  cep  | 0852-364, 15685-225
| 4   |    2      |  nome | Lais dos Santos, 19
| 5   |    2      |  docs | 255.365.358-74, 24.582.659, 4.526.874-9
| 6   |    3      |  nome | Thais Paula dos Santos, 40, promotora
| 7   |    3      |  docs | 258.481+574-85, 14.582.478, 3.546.874-8
| 8   |    3      |  cep  | 05832-005, 04185-007
| 9   |    3      |  ende | João Pessoa, 1040, Vila Almeida

I’m trying to make a select but I’m not getting back the values for the key and value column, organized according to the table below:

------------------------------------------------------------------------------------------------------------------
|       nome            |               docs                      |          cep        |            ende
------------------------------------------------------------------------------------------------------------------
João da silva           | 552.282.820-58, 22.532.25, 1.230.253-15 | 0852-364, 15685-225 | 
Lais dos Santos         | 255.365.358-74, 24.582.659, 4.526.874-9 |                     |
Thais Paula dos Santos  | 258.481+574-85, 14.582.478, 3.546.874-8 | 05832-005, 04185-007|João Pessoa, 1040, Vila Almeida

my select this way:

$resultado = $pdo->select("SELECT * FROM bd");

foreach ($resultado as $res) {
    $msg .="                ";
    $msg .="                    ".$res['id']."";
    $msg .="                    ".$res['user_id']."";
    $msg .="                    ".$res['key']."";
    $msg .="                    ".$res['value']."";
    $msg .="                ";
}

echo $msg;

1 answer

0

Brother, you are trying to store and display the data resulting from the query in String form. I think the ideal would be for you to store and display in array form to be more organized both for you to manipulate the data afterwards and for you to view them in a more organized way.

I will post both ways for you. The way you asked in the question and in the form of Array.

String

$resultado = $pdo->query("SELECT * FROM bd");

$msg = ""; 
foreach ($resultado as $res) {

    $msg .= $res['id']." | ";
    $msg .= $res['user_id']." | ";
    $msg .= $res['key']." | ";
    $msg .= $res['value']." | ";
    $msg .= "<br>";     
}

echo $msg;

Array

$resultado = $pdo->query("SELECT id, user_id, key, value FROM bd");
$resultado = $resultado->fetchAll(PDO::FETCH_ASSOC);

$msg = array(); 

foreach ($resultado as $res) {

    $msg[] = $res;          
}

echo "<pre>";
    print_r($msg);
echo "</pre>";
  • You may not have explained it correctly, but Observer that the key column, it stores the names referring to the columns, and if you notice will notice that the user_id column, the name will repeat itself according to the client id, and the value column stores the data or values. to perhaps facilitate clarification, focus on the user_id, key and value column, and note the second tabelinha there of my question, to see how the result of select should come, it is not the formatting I want, but the header and the body.

  • Brother, you tried to run the code I posted ? What came out ? Logically you should change the name of the query fields and adapt some things as your need. My answer was just a way for you to perform the query and display the data.

  • got it... the problem is that I’m struggling precisely by bringing these results to php and display, I can’t make it work!

  • When you executed the two codes that I posted, what were the outputs that came out in your file ? Did you make an error or something ? Add more information.

  • the two ran well, the first was in the format of the table I sent, but the big x of the question for me is the same sort, the header has to be name | Docs | cep | Where , and columns the data of the value column, understood?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.