繁体   English   中英

如何在 php-mysql 中使用 pdo 从一台服务器连接(拉取数据)到(插入 - 创建表)另一台服务器,可能是获取?

[英]how to connect ( pull data ) from one server to ( insert into - create table ) another server using pdo in php-mysql , possibly fetch ?

谁能告诉我如何修复下面的代码? 这样我就可以从 host1(数据库 x,表 1)中提取数据以在 host2(数据库 dx,表 2)中创建表,

我在这里错过了什么? 有人告诉我使用 prepare 然后获取数组然后插入 table2,任何人都可以告诉我,怎么做?

这是我的代码

<?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...

你用过 d'B'up。

在:

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

你用过d'H'up。

也许这是一个简单的印刷错误案例?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM