我的问题很简单,我认为人们经常需要这样做。然而,经过数小时的在线搜索,没有什么特别的启发性出现。我知道这里有一个关于使用PDO进行相同操作的线程,但是我想坚持使用mysqli在整个站点上保持一致。
因此,我需要将一个或几行从一个数据库移到另一个数据库(而不是从一个表移到单个数据库中的另一个表)。
到目前为止,我所拥有的代码肯定会引起人们的嘲笑,因为事实证明该任务远远超出了我的能力范围。无论如何,这里去了:
首先,我有两个module.exports = {
//... your config
module: {
rules: [
{
test: /\.scss$/,
use: [
{
loader: 'style-loader',
options: {
insertAt: 'top', //Insert style tag at top of <head>
singleton: true, //this is for wrap all your style in just one style tag
}
},
"css-loader",
"sass-loader"
],
},
]
},
//... rest of your config
};
...尽管目前还没有给我带来麻烦,但是不确定是否鼓励这样做。
mysqli_connect
这是三明治中的代码:
$connect_MAIN = mysqli_connect($servername_MAIN, $username_MAIN, $password_MAIN, $dbname_MAIN);
$connect_TEMP = mysqli_connect($servername_TEMP, $username_TEMP, $password_TEMP, $dbname_TEMP);
所以,我知道这有点混乱……正如我所说,我不确定是否可以在从DB1返回值的///Prepare this in advance because the 'IN' values are taken from a $_GET
$sql_select = "SELECT * FROM Concert WHERE id IN (1, 2, 3, 4)";
///Connect to source DB
$result = $connect_TEMP->query($sql_select);
if ($result->num_rows != 0) {
///Connect to destination DB
$stmt = $connect_MAIN->prepare('INSERT INTO Concert (venue_id, date, ensemble_id, info, title, repertoire, time) VALUES (?, ?, ?, ?, ?, ?, ?)');
$venue_id = $date = $ensemble_id = $info = $title = $repertoire = $time = null;
$stmt->bind_param("isissss", $venue_id, $date, $ensemble_id, $info, $title, $repertoire, $time);
while($row = $result->fetch_assoc()) {
$venue_id = $row["venue_id"];
$date = $row["date"];
$ensemble_id = $row["ensemble_id"];
$info = $row["info"];
$title = $row["title"];
$repertoire = $row["repertoire"];
$time = $row["time"];
if ($stmt->execute() === TRUE) {
if ($show_once == 1) {
echo "Info successfully submitted.";
$show_once = 0;
}
} else {
echo "Hmm, something went wrong..." . $connect_MAIN->error;
}
}
}
$stmt->close();
mysqli_close($connect_TEMP);
}
循环中间连接到DB2。随意嘲笑,但前提是您有一些有用的建议!
答案 0 :(得分:3)
使用参数化查询而不是串联字符串。
///Prepare this in advance because the 'IN' values are taken from a $_GET
$sql_select = "SELECT * FROM Concert WHERE id IN (1, 2, 3, 4)";
///Connect to source DB
$result = $connect_TEMP->query($sql_select);
if ($result->num_rows != 0) {
///Connect to destination DB
$stmt = $connect_MAIN->prepare('INSERT INTO Concert (venue, date, info, time) VALUES (?, ?, ?, ?)');
$venue = $date = $info = $time = null;
$stmt->bind_param("ssss", $venue, $date, $info, $time);
while($row = $result->fetch_assoc()) {
$venue = $row["venue"];
$date = $row["date"];
$info = $row["info"];
$time = $row["time"];
if ($stmt->execute()) {
///This is just to stop message from appearing multiple times.
if ($show_once == 1) {
echo "Info successfully submitted.";
$show_once = 0;
}
} else {
echo "Hmm, something went wrong..." . $stmt->error;
}
}
$stmt->close();
mysqli_close($connect_TEMP);
}
答案 1 :(得分:2)
是的,您可以在脚本中的任何时候建立第二个数据库连接。没有什么可以阻止它,也没有理由阻止它。 (由于不同的原因)它比您想像的要普遍得多。但是,考虑到实际连接到数据库所带来的开销,我建议您不要在每次迭代时都建立连接(这是从性能角度考虑的最佳做法)
我会做什么:
- 首先,连接到源数据库,获取所需的数据
在这里无需多说,您已经做了,看起来还可以。像检查一样,在继续进行之前检查至少有一个结果。如果结果集为空,则不要再继续(退出)
- 如果结果集中至少有一行,请建立与目标数据库的连接
在开始循环之前进行此操作 ,以避免建立多个连接而造成的性能损失。如果要查看成百上千行或更多行,您会真正注意到差异。建立连接后,继续
- 浏览源数据并准备插入语句
您可以在这里选择。 a)遍历整个结果集并准备一个包含多行的单个插入,您将在循环结束时一次全部插入,或者b)在每次迭代中创建单个插入(单行),运行插入,然后继续进行下一个迭代。
有两种策略都支持和反对的理由。选择一个更适合您的选择。
用法律上的术语来说,您的代码已经准备好了(在开始循环之前只需连接到目标数据库一次)。使用带绑定的参数化查询,而不是编写完整的插入字符串,可能会受益匪浅,但除此之外,您几乎可以做到这一点。
答案 2 :(得分:1)
如果您正在运行自己的MySql服务器,则可以使用Federated引擎以更简单的方式执行此操作。
联合表的行为类似于普通表,但是它将数据存储在远程数据库中。使用此引擎,您可以执行以下操作:
INSERT INTO remoteTable (values) SELECT * FROM localTable