www.webdeveloper.com
Results 1 to 5 of 5

Thread: Data from one database and insert into a new database

  1. #1
    Join Date
    Aug 2007
    Posts
    58

    Data from one database and insert into a new database

    Can someone show me a php script that will query a db then place the results into a different database.

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,326
    If the same database connection and the same database user can access both databases, then it can be done completely via SQL, probably a single query.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  3. #3
    Join Date
    Aug 2007
    Posts
    58
    Ok it does use the same db connection and user. Could you give an example of what the code should look like? What if there were a different connection and user, what would I do then?

  4. #4
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,326
    If the same connection/user, you should be able to do something like:
    PHP Code:
    $connx mysql_connect('localhost''dbuser''dppassword') or die(mysql_error());
    $sql "
    INSERT INTO database2.table_name (fld1, fld2, fld3)
    SELECT fld1, fld2, fld3 FROM database1.table_name
    "
    ;
    $result mysql_query($sql) or die(mysql_error()); 
    If separate connections/users are required, then you'd either have to query the first DB, save the results into an array, then use that array for inserting into the 2nd DB; or perhaps you could use the DBMS command line functions to create a SQL file, then load that file into the second DB.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  5. #5
    Join Date
    Aug 2007
    Posts
    58
    Ok thanks, that makes total sense.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles