D
D
del9937882016-12-06 02:55:42
PHP
del993788, 2016-12-06 02:55:42

How to assign responses from the database to a variable?

Hello. I make a request to the database:

$id = "53";

const SQL_ALL_FOR_LOG = '
SELECT * FROM main WHERE id = :id
';

$pdo = new PDO($dsn, $user, $password);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
 
$stmt = $pdo->prepare(SQL_ALL_FOR_LOG); 
$res = $stmt->execute([':id' => $id]);
$row = json_encode($stmt->fetchAll(PDO::FETCH_OBJ));

echo "$row";

And I get this response:
[{"id":"53","namenomer":"lux","datestart":"2016-12-22","dateend":"2016-12-24"}]

How to assign a variable to each value in a table in php?
Like $a = "namenomer",
And if you do echo "$a" , then get the answer: lux

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry, 2016-12-06
@del993788

Goodnight.
Use json_decode
You can do it a little differently

$row = $stmt->fetchAll(PDO::FETCH_OBJ);
foreach($row as $value){
   // остальной код
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question