A
A
Asjacker2020-04-14 14:42:14
PHP
Asjacker, 2020-04-14 14:42:14

outputting data from mysql php to json?

Hello!
I am a beginner programmer. I ran into a problem. I need to output certain rows from database to json format in php. I.e. For example, I make a query to the database with WHERE user='andrey', there are several such lines and I need to display them all. So far, it has been possible to display only 1 of these all lines.
Help me please!)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
F
FanatPHP, 2020-04-14
@Asjacker

$user = 'andrey';
$stmt = $conn->prepare("SELECT * FROM table WHERE user=?");
$stmt->bind_param("s", $user);
$stmt->execute();
$result = $stmt->get_result();
$data = $result->fetch_all(MYSQLI_ASSOC);
echo json_encode($data);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question