Sunday, 13 May 2018

mysql - PHP trying to insert results from a select into another db.table

I have two databases and I'm trying to perfom a SELECTin db1.table1 and input the results from this select INTO db2.table2.



Basic things like connection to databases and SELECT are tested, I just need help to configure the correct way to put the SELECT resultS in array and INPUT it INTO db2.table2.



A very important thing, I named the SELECT columns and the db2.table2 columns with exact same name, so I can use the IMPLODE command from PHP.



You can see my code bellow:




//data to connect in both databases
require("connect_otrs_2.php");
require("connect_local_db.php");


//files with select queries
require("select_worklog.php");
require("select_backlog.php");


//connections to databases
$connect=mysqli_connect($db_host,$db_user,$db_password,$db_name);
$connect2=mysqli_connect($db_localhost,$db_localuser,$db_localpassword,$db_localname);

//set utf8
mysqli_set_charset($connect, "utf8");
mysqli_set_charset($connect2, "utf8");

//the variable $qworklog contains the select from db1.table1, that I want insert into db2.table2
$worklog = mysqli_query($connect,$qworklog);



//this is my best try to insert values from $worklog select into the second database
while ($list = mysqli_fetch_assoc($worklog)){
$prep = array();
foreach($list as $k => $v) {
$prep[':'.$k] = $v;
}
$sth = $connect2->prepare("INSERT INTO worklog ( " . implode(', ',array_keys($list)) . ") VALUES (" . implode(', ',array_keys($prep)) . ")");
$res = $sth->execute($prep);


}

//close connection
mysqli_close($connect);
mysqli_close($connect2);
?>

No comments:

Post a Comment

casting - Why wasn't Tobey Maguire in The Amazing Spider-Man? - Movies & TV

In the Spider-Man franchise, Tobey Maguire is an outstanding performer as a Spider-Man and also reprised his role in the sequels Spider-Man...