PDA

View Full Version : PHP r/w MySQL Database?



ckevin
March 27th, 2001, 11:19
I have 2 different forums which are using PHP/MySQL, and I think one (GOOD) is better than the other (BAD), so I wish to use a PHP to call the database of the BAD and write it to the GOOD, in fact, their MySQL tables have different format but more or less the same functions/values, e.g username, password, AIM, ICQ, homepage, email, so is there a way to find a PHP file so that I can import the data to the GOOD forum?

Epgs
March 27th, 2001, 19:49
boy that was confusing

Niaad
March 27th, 2001, 20:28
lol, agreed...from what I can see, he seems to want to take the data from one database and put it into another one--but the field names are different.

ckevin
March 28th, 2001, 06:17
sorry, but here is what i want:

A php scripts that can read MySQL tables and write the data to a new (database) table in different column format.

E.g.

Database A. Table Name: A1
===========================

Column . Function
1 . Username
2 . Password
3 . ICQ no.

Database B. Table Name: B1
===========================
Write to Table 2

Column . Function
1 . ICQ no.
2 . Username
3 . Password

I hope it's more clear now... :) so can you show me are there any php scripts can do this?

Niaad
March 28th, 2001, 18:26
Here's a start/idea of mine. I have never written a script that deals with two databases because I only have one database with my hosting service.

So, I do not know if this will work by connecting to two databases, but I suppose it is worth a shot.



// Connect to Database A
$db = mysql_connect("localhost", "A", "[password]");
mysql_select_db("A",$dbA);

// Connect to Database B
$db = mysql_connect("localhost", "A", "[password]");
mysql_select_db("B",$dbB);

// Get all records from table A1, database A
$result = mysql_query("SELECT * FROM A1",$dbA);

if ($myrow = mysql_fetch_array($result)) {

do {

// Insert all records into table B1, database B

$result = mysql_query("INSERT INTO B1 (icq,username,pw) VALUES ('$myrow["icq"]','$myrow["username"]','$myrow["pw"]')",$dbB);

} while ($myrow = mysql_fetch_array($result));

} else {
echo "No more!";
}


Like I said, I have no idea if this works...as I can't test it.

If anybody else here spots a mistake, or can alter that code in some way to make it work (that is, if it doesn't), I'm definitely open for suggestions! :)

ckevin
March 28th, 2001, 19:04
Niaad, Thanks your help and I wish to know this script would only copy Database A. to Database B but not 'Cut and Paste' right? so I don't worried to lost any data if it failed?

Also, should I first estabish the Table B2 Column and Functions then use PHP to import the data?

Thanks a lot! :)

Cheap Bastard
March 28th, 2001, 20:11
... why not edit the good script so that the columns and functions would be lined up right?

That should be a lot easier...

Niaad
March 28th, 2001, 23:21
First off, no data loss would occur because you are simply reading the data from one database, and then putting it into the other.

And, I don't think it really matters that the columns aren't lined up. Since you are taking data from the first database and just making it into variables (i.e. $myrow["field"], you can use them freely--which means as long as those variables line up with the field they are being inserted into... i.e. INSERT INTO B1 (B1_field1, B1_field2, B1_field3) VALUES '$myrow["A1_field1"]','$myrow["A1_field2"]','$myrow["A1_field3"]'.

And ckevin, I don't really understand what you mean by "Also, should I first estabish the Table B2 Column and Functions then use PHP to import the data?"

ckevin
March 29th, 2001, 05:17
for editing the good script, well.. i think it's not good to me cause i need to update the MySQL tables every time once a new upgrade package available. ;p

"Also, should I first estabish the Table B2 Column and Functions then use PHP to import the data?"

What I mean is I have to create the new MySQL database 'B' and table 'B2' before I use this PHP, right? or the PHP can create the database and table for me...

thanks very much!

Niaad
March 29th, 2001, 20:20
You could just change the script to write to the database and table you want it to.

Otherwise, yes you do--because if you don't, you'll get a "Not a valid MySQL resource" error when you try to open a database that doesn't exist :)

Like I said, I do not know if this script will even work...good luck with it, you might want to look into working with two databases at once--again, I have no experience with it, considering my hosting service only gives me one database.