[英]how to connect ( pull data ) from one server to ( insert into - create table ) another server using pdo in php-mysql , possibly fetch ?
Can anyone please tell me how could I fix this code below?谁能告诉我如何修复下面的代码? so that I can pull data from host1 (database x, table 1 ) to create table in host2 ( database dx, table2 ),
这样我就可以从 host1(数据库 x,表 1)中提取数据以在 host2(数据库 dx,表 2)中创建表,
What am I missing here?我在这里错过了什么? someone tell me to use prepare then fetch array then insert into table2, can any one show me, how to?
有人告诉我使用 prepare 然后获取数组然后插入 table2,任何人都可以告诉我,怎么做?
here's my code这是我的代码
<?php
echo 'Database Connection <br>';
$hostname = "host1";
$hostname1 = "host2";
$username = "myname";
$password = "mypassword";
try {
$dbh = new PDO("mysql:host=$hostname;dbname=x", $username, $password);
echo "Connected to database x<br>"; // check for connection
$dbup = new PDO("mysql:host=$hostname1;dbname=dx", $username, $password);
echo "Connected to database dx<br>"; // check for connection
/*** The SQL SELECT statement ***/
$sql = $dbh->query("select name, choices from table1")or die(print_r($dbh->errorInfo(), true));
$sql1 = $dhup->query("CREATE TABLE api(
`name` VARCHAR(40) NOT NULL,
`choices` VARCHAR(255) NOT NULL )")or die(print_r($dbh->errorInfo(), true));
foreach ($sql as $row)
{
$dbup->exec("insert into table2('name','choices') values('" . $row['name'] . "','" . $row['choices'] . "')") or die(print_r($dbup->errorInfo(), true));
}
// /*** close the database connection ***/
$dbh = null;
$dbup = null;
}
catch(PDOException $e)
{
print 'Exception : '.$e->getMessage();
}
?>
Thanks谢谢
I noticed you're not consistently spelling "dbup" the same way.我注意到您并没有始终以相同的方式拼写“dbup”。
In:在:
$dbup = new PDO...
You used d'B'up.你用过 d'B'up。
In:在:
$dhup->query("CREATE TABLE...
You used d'H'up.你用过d'H'up。
Maybe this is a simple case of a typographical error?也许这是一个简单的印刷错误案例?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.