How to transfer data from n of tabit in one variable?
Hello!
Database in which there are three tables - books,movies,music. Trying to transfer all the data from those tables array in variable $catalog
$db = new PDO($dsn, $username, $password);
$results = $db->query("SHOW TABLES FROM data");
$tables = $results->fetchALL(PDO::FETCH_ASSOC);
but then I'm not sure,because $table is an object...
foreach ($tables as $categories) {
$result = $db->query("SELECT * FROM ".$categories);
// trying to SELECT * FROM books / SLECT * FROM movies / SELECT * FROM music depending on the table
then
$catalog = $results->fetchAll(PDO::FETCH_ASSOC)); // and here I have,in theory,should be the figures from the tables
I would be grateful for any hint!
2 answers
A simple foreach to concatenate all.
Why do you need this vinigret? If it really wants to be perverted - make 3 requests and make array_merge
Find more questions by tags MySQLPHP PDOPHP
way train database you have,but I would not want to change the rest of the code ,which in turn depends on the variable $catalog. He looked about like this:
$catalog[101] = [
"title" => "Martin Eden",
"img" => "img/media/martin-eden.jpg",
"genre" => "Philosophical",
"format" => "Paperback",
"year" => 1909,
"category" => "Books",
"authors" => [
"Jack London",
]];
$catalog[302] = [
"title" => "10 000 day"
"img" => "img/media/tool.jpg",
"genre" => "Rock",
"format" => "Vinyl",
"year" => 2006,
"category" => "Music",
"artist" => "Tool"
]; and so on.... - Sherman87 commented on July 12th 19 at 17:25