[英]Select data from one table and inserts it into an existing table using PHP
I am trying to select data from one table and insert it into an existing table using PHP. 我正在尝试从一个表中选择数据,然后使用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;
?>
As @tkausl says, if you are working with the same database, you can insert the result into the desired table with the same query. 正如@tkausl所说,如果使用相同的数据库,则可以使用相同的查询将结果插入所需的表中。
<?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 ");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.