简体   繁体   中英

Using PHP to take data from one SQL database and inserting it into another

I am needing help inserting data from one MySQL database into another. I am taking data based on the last 90 days from a MySQL database that updates every night. I am having tousle trying to get the data from one table into another. Here is what I have so far.

<?php
$con= mysqli_connect("localhost","jeff","birdie15","kcoj");
    if ($con->connect_errno) {
        echo ("<br/> Failed to connect to MySQL:(".
        $con->connect_errno .").".$con->connect_error);
    }else{
        echo ("Connected to database".KCOJ);
        echo ("<br/> ".$con->host_info);
    }

    $d= date ($format, strtotime ('-90' .$date));

    $code = mysqli_query($con,"INSERT INTO last90Days * SELECT * FROM drugCountsByCharge WHERE countyCode ='9999' AND date <= '$d'");

?>

I have 36 columns that have to be queried and then inserted and numerous amounts of rows. Any and all help would be greatly appreciated.

To get a better performance, try to to ever use grouped SQL statements, and to avoid to get too much result and load all your free memory into mysql, get just a few rows by interaction.

You can first count the total of rows that you have into your source database and split into "blocks" of 100.000 rows for example, and into this 100.000 registers you can create a insert statement like this: INSERT INTO your_table(field1,field2,field3,field4,....) VALUES (value1,value2,value3,value4,....) ,(value1,value2,value3,value4,....) ,(value1,value2,value3,value4,....) ,(value1,value2,value3,value4,....) ,(value1,value2,value3,value4,....) ,(value1,value2,value3,value4,....) ...;

So...first you have to know the total rows that you have: SELECT COUNT(1) AS TOTAL FROM your_table;

After that you can estipulate a number of rows for each iteration:

for($i=0;$i<=$number_of_iteractions;$i++) {
    $limit_start = $i * $rows_per_process; # to know where to start getting data
    $limit_end = $rows_per_process;

    $sql = "SELECT colunsx... FROM your_table LIMIT $limit_start,$limit_end";
    $query = mysql_query($sql);

    $data = array();
    while($row = mysql_fetch_array($query)) {
        # build your VALUES statement here
    }
}

?>

After build your grouped sql statement you can commit the transaction.

Just adapt it for your needs as I don't tested anything.

As I understand, it's not a job to copy from one DB to another but from one table to another within the same DB.

You're almost there, alter your "SELECT ... INSERT" a bit:

 $code = mysqli_query($con,"INSERT INTO last90Days SELECT * FROM drugCountsByCharge WHERE countyCode ='9999' AND date >= '$d'");

As calculated, $d is 90 days back from now, so the clause must be >= $d

And, this only works, if table layout is the same in both tables- literally the same!

Otherwise you might use a statement like this:

   INSERT INTO table_b (field_b_a, field_b_b, field_b_c) 
             SELECT field_a_a, field_a_b, field_a_c from table_a;

(that's an example only)

Hope this would be helpful, I guess

$sql =mysql_query(USE DB_1);
$sql1 =mysql_query(Your select query)
while($res = mysql_fetch_array($sql1)){
  $sql3 = mysql_query(USE DB_2);
  $sql4 = mysql_query(Your Insert Query);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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