This question already has an answer here:
I have a question, first look my code:
$testsql = $db->prepare('SELECT * FROM forum_sujets');
$testsql->execute();
$test = $testsql->fetch();
print_r($test);
My DB:
Ok, so I want to select my information in array, and i wan't this array look like:
array(
'id' => '1', array(
'nom' => 'test',
'titre' => 'test',
'auteur' => 'test',
),
'id' => '2', array(
'nom' => 'test2',
'titre' => 'test2',
'auteur' => 'test2',
),
);
Ok, and after my array look like this, I want to echo, for exemple, the name of the ID "2", I don't known if i'm clearful. I hope you can help me, see u :)
</div>
An example, your screenshot is not so good so I suppose you have colums titre, nom et auteur dans votre table:
$testsql = $db->prepare('SELECT id, titre, nom, auteur FROM forum_sujets');
$testsql->execute();
$data = [];
while ($test = $testsql->fetch()) {
$id = $test['id'];
$data[$id] = [
'nom' => $test['nom'],
'auteur' => $test['auteur'],
'title' => $test['title'],
];
}
print_r($data);
The output will be like :
array(
1 => array(
'nom' => 'test',
'titre' => 'test',
'auteur' => 'test',
),
2 => array(
'nom' => 'test2',
'titre' => 'test2',
'auteur' => 'test2',
),
);
As you cannot have two same key in one array