Tuesday, November 2, 2010

Merge two SQL databases using PHP

1.Q: One database with 100 members and another database with 500 members, append 100 members to the database with 500 members.


Create 2 db connection scripts
connect.php and dbconnect.php
Create 1 script called
create-new.php
Below is the connect.php

<?php
// this is connect.php
// connect to the 100 member database

$host = "localhost"; // Database server
$user = "Database username"; // Database username
$pass = "Database password"; // Database password
$db = "Database name"; // Database name
$db1=mysql_connect("$host","$user","$pass");
mysql_select_db("$db");
$ok = mysql_select_db("$db");
if (!ok)
{
die("<br>" . mysql_errno().":".mysql_errno()."<br>");
}
?>

Below is the dbconnect.php

<?php
// this is dbconnect.php
// connect to the 500 member database
$host = "localhost"; // Database server
$user = "Database username"; // Database username
$pass = "Database password"; // Database password
$db = "Database name"; // Database name
$db1=mysql_connect("$host","$user","$pass");
mysql_select_db("$db");
$ok = mysql_select_db("$db");
if (!ok)
{
die("<br>" . mysql_errno().":".mysql_errno()."<br>");
}
?>



Below is the create-new.php

<?php
// 100 members database
include("connect.php);
// for this demo we'll use customer as the table name
// replace the field names with the correct fields from your database table

$sql = "SELECT * FROM customer";
$sql1=mysql_query($sql) or die("Couldn't select customer!");
while($row = mysql_fetch_array($sql1))
{
$ID = stripslashes($row['ID']); // replace the field names only
$MembersID = stripslashes($row['MembersID']); // replace the field names only
$fname = stripslashes($row['fname']); // replace the field names only
$lname = stripslashes($row['lname']); // replace the field names only
$email = stripslashes($row['email']); // replace the field names only
// 500 members database
include("dbconnect.php);
$insertmember="insert into customer (ID, MembersID, fname, lname, email) values ('$ID', '$MembersID', '$fname', '$lname', '$email')";
//registering member in database
$insertmember2=mysql_query($insertmember) or die("<br><br>Could not insert customer");
// do not uncomment code below.
//$ID = mysql_insert_id();
echo "Insert $fname $lname Completed Successfully";
}// Closes While statement
?>

No comments:

Post a Comment