Reputation: 135
I am trying to select data from one table and insert it into an existing table using PHP.
While i understand that i could just collect the results into an array and then re-inject them into the correct table this seems inefficient. I must be able to just copy the requested query into a table of my choosing.
Any guidence would be much appreciated. Below is how i would put data into an array.
<?php
header("Cache-Control: no-cache");
date_default_timezone_set('Europe/London');
// open DB connection
require_once("DbConnect.php");
// fetch playlist
$result = $db->query(
"SELECT `artist`, `title`, `label`, `albumyear`, `date_played`, `duration`, `picture` "
."FROM historylist ORDER BY `date_played` DESC LIMIT 5 ");
// put into array
while ($row=$result->fetch_object()) $list[] = $row;
?>
Upvotes: 0
Views: 67
Reputation: 5427
As @tkausl says, if you are working with the same database, you can insert the result into the desired table with the same query.
<?php
header("Cache-Control: no-cache");
date_default_timezone_set('Europe/London');
// open DB connection
require_once("DbConnect.php");
// fetch playlist
$result = $db->query(
"INSERT INTO my_table(`artist`, `title`, `label`, `albumyear`, `date_played`, `duration`, `picture`)" .
"SELECT `artist`, `title`, `label`, `albumyear`, `date_played`, `duration`, `picture` " .
"FROM historylist ORDER BY `date_played` DESC LIMIT 5 ");
Upvotes: 1