E
E
Evgeny Ivanovich2018-02-26 18:14:50
PHP
Evgeny Ivanovich, 2018-02-26 18:14:50

How to get PHP array of data from db?

Suppose there is a table with the following values:
id name age
1 John 21
2 Jack 16
3 James 21
4 Jerry 22
Submit a MySQL query

$con->query("SELECT name FROM tbl_name WHERE age='21'");

which, in theory, should return 2 values ​​from 2 rows. How can the return value be converted to an array with these values? That is, for this array to work like this:
echo $arr[0];//John
echo $arr[1];//James

Answer the question

In order to leave comments, you need to log in

1 answer(s)
J
jasonOk, 2018-02-26
@eugenius1997

$query = $con->query("SELECT name FROM tbl_name WHERE age='21'");
$arr = $query->fetchAll();

echo $arr[0]['name']; //John
echo $arr[1]['name']; //James

// OR

$query = $con->query("SELECT name FROM tbl_name WHERE age='21'");
while ($row = $query->fetch()) {
echo $row['name'];
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question