Jump to content

[SOLVED] Updating a 2 databases


cheechm

Recommended Posts

Hi,

I can't seem to make this script function. It runs however the database doesn't get updated:

 

<?php
// Lets open connections to MySQL database
// -------------------------------------

$con1 = mysql_connect("*********************");
$db = mysql_select_db("**DB**", ($con1))
or die ("Couldn't select database.");

$con2 = mysql_connect("*********************");
$db = mysql_select_db("**DB**", ($con2))
or die ("Couldn't select database.");

// Lets get the data from the first site
// -------------------------------------

$sql = "SELECT * FROM **TABLE**";
$query = mysql_query($sql, $con1);

// If no data exists then there is no point executing the query
// -------------------------------------

if (mysql_num_rows($query) == 0) {

echo "Nothing to copy. Exiting now.";

exit;

}

// Lets insert it into the other site
// -------------------------------------

for ($i=0;$i<($result = mysql_fetch_assoc($query));$i++)
{mysql_query("INSERT INTO probid_users (username, password, email, regdate) VALUES('username', 'password', 'email', 'joindate') ($con2)");
}


mysql_close($con1);
mysql_close($con2);

?>

 

How can I make it so that they update the required tables..?

 

Thanks in advanced.

Link to comment
https://forums.phpfreaks.com/topic/70943-solved-updating-a-2-databases/
Share on other sites

Your for loop is not valid. You are doing a for loop where $i<mysql_fetch_assoc($query). mysql_fetch_assoc() does not return a number. Also once you assign the record to $result you need to reference those values correctly. Also, you put the connection parameter within the query string.

 

Change this:

<?php
for ($i=0;$i<($result = mysql_fetch_assoc($query));$i++)
{mysql_query("INSERT INTO probid_users (username, password, email, regdate) VALUES('username', 'password', 'email', 'joindate') ($con2)");
}
?>

 

To this:

f<?php
while ($result = mysql_fetch_assoc($query))
{
    $sql = "INSERT INTO probid_users (username, password, email, regdate)
            VALUES ('".$result['username']."', '".$result['password']."', '".$result['email']."', '".$result['joindate']."')";
    mysql_query($sql, $con2);
}
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.