Jump to content

[SOLVED] Updating database


cheechm

Recommended Posts

Hi,

I can't seem to make this work. Nothing happens:

 

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

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

$sql = "SELECT * FROM ****";
$query = mysql_query($sql, $con1) or die(mysql_error());
if (mysql_num_rows($query) == 0) { // The record doesn't exist, so insert it

while($result=mysql_fetch_assoc($query))
{
$isql="INSERT INTO ****(username,password,email,regdate,birthdate)
VALUES('".$result['username']."','".$result['password']."','".$result['email']."','".$result['joindate']."','".$result['birthday_search']."')";
mysql_query($isql, $con2);
}
} else // The record already exists, so update it.
{
while ($result=mysql_fetch_assoc($query))
{
$usql = "UPDATE **** SET username = '".$result['username']."', password = '".$result['password']."', email = '".$result['email']."', regdate = '".$result['joindate']."', birthdate = '".$result['birthday_search']."' WHERE username = '".$result['username']."'";
mysql_query($usql, $con2) or die(mysql_error());
}
}


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

echo "finished";

?>

 

Do you have any idea why no data is going in to the database?

Thanks

Link to comment
https://forums.phpfreaks.com/topic/71160-solved-updating-database/
Share on other sites

But that is what I was trying to do with this:

 


if (mysql_num_rows($query) == 0) { // No records
    while($result=mysql_fetch_assoc($query)) // So it's pointless trying to loop over them.

 

It is taking info from one database and putting it in another.

 

No, it's not. We perform $query and see that the number of rows returned is 0,

 

mysql_num_rows($query) == 0 <-- The number of rows from $query is 0. There is no matching data.

 

but then we try to loop over those 0 records:

 

while($result=mysql_fetch_assoc($query)) <-- We already know there are no rows in $query, so this will never do anything. ever.

 

$query has 0 rows, so there is no data.

 

Replace that loop with a simple INSERT INTO TABLE () statement. If your database is correct, there should be no need to check for duplicate keys, nor increment anything after the INSERT.

 

 

But the number of rows returned will never be 0. There will be always data, it is just a question of has that data been inserted into the other database and if so it should be updated instead of inserted again.

 

So effectively the mysql_num_rows($query) does absolutley nothing, because there will always be data fetched.

if the number of rows will always be != 0, then the lower part of the code simplifies from this:

 

if (mysql_num_rows($query) == 0) { // The record doesn't exist, so insert it

while($result=mysql_fetch_assoc($query))
{
$isql="INSERT INTO ****(username,password,email,regdate,birthdate)
VALUES('".$result['username']."','".$result['password']."','".$result['email']."','".$result['joindate']."','".$result['birthday_search']."')";
mysql_query($isql, $con2);
}
} else // The record already exists, so update it.
{
while ($result=mysql_fetch_assoc($query))
{
$usql = "UPDATE **** SET username = '".$result['username']."', password = '".$result['password']."', email = '".$result['email']."', regdate = '".$result['joindate']."', birthdate = '".$result['birthday_search']."' WHERE username = '".$result['username']."'";
mysql_query($usql, $con2) or die(mysql_error());
}
}

 

to this:

 

while ($result=mysql_fetch_assoc($query)) {
$usql = "UPDATE **** SET username = '".$result['username']."', password = '".$result['password']."', email = '".$result['email']."', regdate = '".$result['joindate']."', birthdate = '".$result['birthday_search']."' WHERE username = '".$result['username']."'";
mysql_query($usql, $con2) or die(mysql_error());
}

 

Now we're getting somewhere. Loop over the records from db1 and grab the values for each field. Look for those values in db2. If there is no match, insert the record into db2.

 

while ($result=mysql_fetch_assoc($query)) {
      $t_sql = "SELECT username FROM ***** WHERE username = '{$result['username']}' AND password = '{$result['password']}' AND email = '{$result['email']}' AND birthdate = '{$result['birthdate']}";
      $t_result = mysql_query($t_sql, $con2) or die(mysql_error());
      if (mysql_numrows($t_result) > 0) { // Record exists in other db, so update
            $usql = "UPDATE **** SET username = '".$result['username']."', password = '".$result['password']."', email = '".$result['email']."', regdate = '".$result['joindate']."', birthdate = '".$result['birthday_search']."' WHERE username = '".$result['username']."'";
      } else { // Record doesn't exist in other db, so insert it.
            $usql = "INSERT INTO **** SET username = '".$result['username']."', password = '".$result['password']."', email = '".$result['email']."', regdate = '".$result['joindate']."', birthdate = '".$result['birthday_search']."'";
      }

       mysql_query($usql, $con2) or die(mysql_error());
}

 

 

 

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.