简体   繁体   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 ?

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.

相关问题 执行OPENQUERY来从MySql Server提取数据,并使用PDO从php脚本内部将其引导到SQL表? - Executing OPENQUERY to pull data from MySql Server and inner joing it to SQL table from php script using PDO? 从一个表中选择多个列并将数据插入到 PHP-MySQL 中不同数据库中的另一个表中 - Select multiple columns from a table and insert data into another table in a different database in PHP-MySQL 如何从一个表中获取数据并插入到php中的另一个表中 - how to fetch data from one table and insert into another table in php 使用json从PHP-MySql服务器到Android获取图像 - Get images from PHP-MySql server to Android using json 根据 PHP-MySQL 中另一个表的字段值在一个表上插入行 - Insert rows on one table based on the values of a field of another table in PHP-MySQL 使用PDO PHP将数据从一个表移动到另一个表 - Move Data from one table to another table using PDO php PHP-MYSQL脚本无法连接到服务器上的数据库 - PHP-MYSQL script failing to connect to the database on the server 如何获取上次mysql取数据的值? PHP-MySQL - How to get the value of last mysql fetch data? PHP-MySQL PHP MySQL将数据从一个表插入到另一个表 - PHP MySQL insert data from one table to another table 如何在PHP和MySQL中将几条记录从一台服务器上的表复制到另一台服务器上具有相同结构的表? - How to copy few records from a table on one server to the table with same structure on another server in PHP and MySQL?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM