SitePoint Sponsor

User Tag List

Results 1 to 4 of 4

Thread: PHP conversions

  1. #1
    SitePoint Guru
    Join Date
    Apr 2007
    Posts
    685
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    PHP conversions

    I'm looking to take data out of one database, and then slap it into another database on the same server. Is anyone familiar with the php that could do this?

    Example

    One database has a users table:

    Columns: First name Last name Birth date

    I need to take that and put it in another database with a table like:

    columns: first last birth

    Any ideas?

  2. #2
    SitePoint Zealot
    Join Date
    Jul 2006
    Location
    Dundee, Scotland
    Posts
    179
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Create 2 mysql connections 1 for the old database and 1 for the new.

    Select the data from the first as normal and loop through the results entering the details into the second database.

  3. #3
    SitePoint Evangelist ldivinag's Avatar
    Join Date
    Jan 2005
    Location
    N37 33* W122 3*
    Posts
    414
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    $conn_1 mysql_connect ("server1.com""username1""password1")

    mysql_db_select ($conn_1"db1");



    $conn_2 mysql_connect ("server2.com""username2""password2")

    mysql_db_select ($conn_2"db2");

    $sql1 "select first, last, birth from table1";
    $result1 mysql_query ($sql1$conn_1);

    while (
    $row mysql_fetch_row ($result_1)
    {
      
    $sql2 'insert into table2 (first, last, birth) values ("' .
                 
    $row[0] . '", "' $row[1] . '", ' $row[2] . ')';
      
    $result2 mysql_query ($sql2$conn_2);
    }

    mysql_close($conn_1);
    mysql_close($conn_2); 
    no error checks are implemented so put them in on your own.

    trick is that resource value when you issue a 'mysql_connect' call...
    leo d.

  4. #4
    SitePoint Wizard cranial-bore's Avatar
    Join Date
    Jan 2002
    Location
    Australia
    Posts
    2,634
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code SQL:
    INSERT INTO `new_table` (FIRST, LAST, birth) VALUES SELECT first_name, last_name, birth_date FROM `otherdb`.`other_table`

    Do this with a connection to the new db, with a user that also has access to the old DB. If you have PhpMyAdmin or direct access to MySQL you probably don't even need to involve PHP at all.


Bookmarks

Posting Permissions

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