如何使用php mysql中的pdo(可能是fetch)将数据从一台服务器连接到另一台服务器(插入-创建表)?
谁能告诉我如何修复下面的代码?这样我就可以从host1(数据库x,表1)中提取数据,在host2(数据库dx,表2)中创建表 我错过了什么?有人告诉我使用prepare然后fetch数组然后插入到表2中,有人能告诉我怎么做吗 这是我的密码如何使用php mysql中的pdo(可能是fetch)将数据从一台服务器连接到另一台服务器(插入-创建表)?,php,mysql,pdo,database-connection,Php,Mysql,Pdo,Database Connection,谁能告诉我如何修复下面的代码?这样我就可以从host1(数据库x,表1)中提取数据,在host2(数据库dx,表2)中创建表 我错过了什么?有人告诉我使用prepare然后fetch数组然后插入到表2中,有人能告诉我怎么做吗 这是我的密码 <?php echo 'Database Connection <br>'; $hostname = "host1"; $hostname1 = "host2"; $username = "myname"; $password = "myp
<?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();
}
?>
我注意到您的“dbup”拼写方式不一致
在:
你用完了
在:
你用完了
也许这只是一个简单的印刷错误?什么不起作用?有错误信息吗?
$dbup = new PDO...
$dhup->query("CREATE TABLE...