1

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?

3
  • Did you check this question? Commented Oct 15, 2014 at 21:00
  • It doesn't look like the author found an acceptable answer there. The table names are dynamic and will change. The table names in that one look static. Commented Oct 15, 2014 at 21:10
  • They select all table names in the DB. Nothing static there. Commented Oct 15, 2014 at 21:13

1 Answer 1

1

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);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.