INSERT INTO newDatabase.users (name, city, email, username, added_by) 
SELECT CONCAT(first_name, ' ', last_name), 'Asgard', CONCAT(first_name,'@gmail.com'), CONCAT(first_name,last_name), 'Damir' FROM oldDatabase.old_users;

    $handle  =  mysql_connect("localhost", "username", "password") or die(mysql_error());
     
      mysql_query("USE database1",$handle);
      $query = "SELECT * FROM table";
      $result = mysql_query($query);
     
      while ($data = mysql_fetch_object($result)){
          $variable1 = $data->column1;
          $variable2 = $data->column2;
     
          mysql_query("USE database2",$handle);
          $sql = "INSERT INTO table2 SET
                  col1 = '$variable1',
                  col2 = '$variable2'";
           if (!mysql_query($sql)) {
           echo '<p>Error adding data into database: ' . mysql_error() . '</p>';
          }
          mysql_query("USE database1",$handle);
     
      }

Blair Wadman's picture

By Blair Wadman, January 2009
Tags: 
PHP
MySQL
Comments
Submitted by Sadek (not verified) on Wed, 02/01/2012 - 19:24

thanks!!!

    reply 

Submitted by Thorwald (not verified) on Wed, 07/29/2015 - 08:14

Thanks man, not a lot of info to be found around this, so very useful!

    reply 

Submitted by ananthi (not verified) on Fri, 08/28/2015 - 14:11

Tank u so much.Its very cleared and simple method

    reply 

Add new comment
Your name
E-mail
The content of this field is kept private and will not be shown publicly.
Homepage
Comment *

More information about text formats

    Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd>
    No HTML tags allowed.
    Web page addresses and e-mail addresses turn into links automatically.
    Lines and paragraphs break automatically.

By submitting this form, you accept the Mollom privacy policy.

Drupal Newsletter | Drupal Tutorials | Marketing Automation | Services | Drupal Books

© Wadman Consulting Ltd 2006 - 2017. All rights reserved. Article archive

