2012-04-05 32 views
0

任何人都可以请告诉我如何修复下面的代码?以便我可以从host1(数据库x,表1)中提取数据以在host2(数据库dx,表2)中创建表,如何从一台服务器连接(拉数据)到(插入到 - 创建表)在php-mysql中使用pdo的另一台服务器,可能取?

我在这里丢失了什么?有人告诉我使用准备然后取数组然后插入到表2中,可以任何一个显示我,如何?

这里是我的代码

<?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(); 
    } 

?> 

感谢

+0

什么不起作用?任何错误消息? – barsju 2012-04-05 23:05:52

回答

0

我注意到你没有始终如一地拼写 “dbup” 以同样的方式。

在:

$dbup = new PDO... 

您使用D'B'up。

在:

$dhup->query("CREATE TABLE... 

您使用D'H'up。

也许这是一个打字错误的简单情况?

相关问题