PDA

View Full Version : Userfield messed up ?? heres the fix


Raptor
12-05-2002, 04:43 PM
AFter a lot of pruing etc i found that my USER table had 33799 rows but my userfield table ahd 134,786 rows. Now we had deleted 3 years worth of zero posters to get these figures - but what about the bloated userfield db ?

i'm sure VB should have prined this up auto - well if it does it didnt in this case - and i doubt it could do it faster than this script.

The script checks that the userid in userfield table exists in user table. If it doesnt it deletes it.

After 30 seconds I have 33799 User and 33799 userfield :)

heres the code - maybe of some use to someone ?


<?php

$link = mysql_connect("dbserver", "database", "password");
mysql_select_db("dforums");

set_time_limit(300000000);

$foundresult = mysql_query("select * from userfield order by userid asc");
$foundnum = mysql_num_rows($foundresult);

echo("Deleting obsolete UserID entries from userfield...<br>\r\n");

for ($i=0;$i<$foundnum;$i++)
{
$row = mysql_fetch_array($foundresult);
$huntId = $row["userid"];

// now lookup user
$huntresult = mysql_query("select * from user where userid='$huntId'");
$huntnum = mysql_num_rows($huntresult);

if ($huntnum == 0)
{
echo("Deleting: $huntId<br>\r\n");
mysql_query("delete from userfield where userid='$huntId'");
}
}

mysql_close();
?>

Xenon
12-05-2002, 07:15 PM
hmm, looks good so
(i have deletet out your dbname and password btw)

just, a question: are you sure you haven't messed up your admin/user.php

normally the entries in userfield are deleted:
$DB_site->query("DELETE FROM user WHERE userid='$userid'");
$DB_site->query("DELETE FROM userfield WHERE userid='$userid'");

Erwin
12-06-2002, 02:23 AM
You may need to repair your userfield table.

Mutt
01-23-2003, 01:37 PM
I had the opposite problem. I had missing userfield entries that needed to be added and it was messing up the admin "search for user" form. I would get no results when searching for a member who didn't have a userfield entry. I made a couple small changes to your script and it worked great. Thanks


<?php

$link = mysql_connect("servername", "dbusername", "dbpassword");
mysql_select_db("dbname");

set_time_limit(300000000);

$foundresult = mysql_query("select * from user order by userid asc");
$foundnum = mysql_num_rows($foundresult);

echo("Adding missing UserID entries to userfield...<br>\r\n");

for ($i=0;$i<$foundnum;$i++)
{
$row = mysql_fetch_array($foundresult);
$huntId = $row["userid"];

// now lookup user
$huntresult = mysql_query("select * from userfield where userid='$huntId'");
$huntnum = mysql_num_rows($huntresult);

if ($huntnum == 0)
{
echo("Adding: $huntId<br>\r\n");
mysql_query("INSERT INTO userfield (userid) VALUES ($huntId)");
}
}

echo("Finshed<br>\r\n");


mysql_close();
?>


it checks the user table and then looks for a userfield entry, if one isn't found it makes one.

make sure to change servername, dbusername, dbpassword, & dbname to your settings.