如何获取sql查询中涉及的表的所有名称?

For example:

I have this query:

SELECT first_name
FROM users
    INNER JOIN roles ON roles.id = users.id_roles
WHERE roles.name = 'admin';

I need an Array with the name of the tables used in the query,like that:

['users','roles'];

I think this would be tough using regex - unless you know your queries are all consistently following the same standard. Here's an option using EXPLAIN.

$tables = [];
$query = "EXPLAIN SELECT first_name FROM users INNER JOIN roles ON roles.id = users.id_rolesWHERE roles.name = 'admin'";
$q = mysqli_query($link, $query);
while($r = mysqli_fetch_assoc($q)) {
    $tables[] = $r['table'];
}
print_r(array_unique($tables));