douchu4048 2019-02-15 10:32
浏览 44

PHP试图将select中的结果插入另一个db.table [duplicate]

This question already has an answer here:

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:

<?php
//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);
?>
</div>
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥30 win from 窗口最大最小化,控件放大缩小,闪烁问题
    • ¥20 易康econgnition精度验证
    • ¥15 msix packaging tool打包问题
    • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
    • ¥15 python的qt5界面
    • ¥15 无线电能传输系统MATLAB仿真问题
    • ¥50 如何用脚本实现输入法的热键设置
    • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
    • ¥30 深度学习,前后端连接
    • ¥15 孟德尔随机化结果不一致