Reputation: 1289
I have this, which works perfectly:
include '../sql.php';
$d = $_POST['forum'] or die('no forum name was given');
$p = new PDO('mysql:host=localhost; dbname=mydb; charset=utf8', $user, $pw);
$s = $p -> query('SELECT * FROM `_' . $d . '`');
echo json_encode($s -> fetchAll(PDO::FETCH_ASSOC));
In the above example, $d is a string (the name of table I want to get all the results from) and I use it to get all the rows of that table and put it into an array (as an object) so that I can retrieve it client-side.
That code produces something like this:
[{"id":"1", "name":"bob"}, {"id":"2", "name":"frank"}]
What I'd like to do is make $d an array of table names. Then retrieve all rows from all of those tables like a loop or simply select all of those tables at the same time and return the results.
The result should look like this:
[
[{"id":"1", "name":"bob"}, {"id":"2", "name":"frank"}], //table 1
[{"id":"1", "color":"red"}, {"id":"2", "color":"blue"}], //table 2
[{"id":"1", "style":"conservative"}, {"id":"2", "style":"gothic"}], //table 3
]
How do I do this?
Upvotes: 1
Views: 711
Reputation: 3852
try something like:
include '../sql.php';
$d = $_POST['tables'] or die('no forum name was given');
$p = new PDO('mysql:host=localhost; dbname=mydb; charset=utf8', $user, $pw);
$ret = [];
foreach ($d as $t){
$s = $p->query('SELECT * FROM `_' . $t . '`');
$ret[$t] = $s -> fetchAll(PDO::FETCH_ASSOC);
}
echo json_encode($ret);
Upvotes: 1