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
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);
}
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.