M
M
Max Ba2019-03-05 11:21:16
PHP
Max Ba, 2019-03-05 11:21:16

How to retrieve data from database with keys?

Guys, how to replace this design?

$data = array();
foreach($stmt->fetchAll() as $val){
  $data[$val['id']] = $val;
}

Maybe there is some PDO mode so that the keys are immediately taken from the database, and not from 0.

Answer the question

In order to leave comments, you need to log in

6 answer(s)
F
FanatPHP, 2019-03-05
@phpcoder81

Of course, there is such a regime. And of course, he is completely unknown to those who taught pohapa from video courses.
PDO can return data in dozens of different formats, and in particular, index the selection by the first field specified in the request
. If we need a dictionary, then there is a mode for this that returns not nested arrays, but a one-dimensional array, which consists of key-value pairs

$data = $pdo->query("SELECT id, name FROM table")fetchAll(PDO::FETCH_KEY_PAIR);

A
Arman, 2019-03-05
@Arik

It seems that there is no ready one, but as an option, use array_column
something like this
but you need to keep in mind that this function behaves differently in php56 and php7

T
Talyan, 2019-03-05
@flapflapjack

You already have the keys. What would you like?

S
Sanostee, 2016-10-22
@AlexanderAm

Set Grid.Column and Grid.Row for answers[k]

Grid.SetColumn(answers[k], 0);  // Grid.Column = 0
Grid.SetRow(answers[k], 0);  // Grid.Row= 0

R
Roman Sokolov, 2016-10-22
@jimquery

Try setting the MinWidth and MinHeight values. Perhaps due to the lack of text, the size of the text field = 0.

R
Roman, 2016-10-23
@yarosroman

I understand that this is some kind of testing program? It's easier to ListView, GridView, set a template, bind an ObservableCollection, and add data to the collection.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question