Jump to content

This while function never stops inserting into database!


TeddyKiller

Recommended Posts

My pc hugely crashed out, as my while function ended up adding about rows into the database until it got to page 17477 (Which was when I managed to close the browser)

 

What I want to do.. for each user registered with activated = 1, to insert a row into another table with there user_id.

What happens is.. it does what it's suppose to do.. nearly, what it doesn't do.. is

1 - It keeps inserting rows in, even with the same user_id. So it's for some reason not checking if it exists or not.

2 - Doesn't stop.

 

Now, whats the problem?

            $query = mysql_query("select * from users where activated = '1'");
            while($row = mysql_fetch_array($query)) {
                $query1 = mysql_query("select * from " . $tablename . "");
                if(mysql_num_rows($query1) > 0) {
                    while($rw = mysql_fetch_array($query1)) {
                        if($row['id'] == $rw['user_id']) {} else {
                            $query2 = mysql_query("insert into " . $tablename . " (user_id) VALUES ('".$row['id']."')");
                        }
                    }
                } else { $query3 = mysql_query("insert into " . $tablename . " (user_id) VALUES ('".$row['id']."')"); }
            }

 

Cheese.

EDIT: That was suppose to be 'cheers'

It keeps inserting rows in, even with the same user_id. So it's for some reason not checking if it exists or not.

 

You have no logic in place to check if the record exists or not, what do you expect?

 

The code looks seriously dodgy to me. SELECT queries should never need to be executed in loops.

What I want to do.. for each user registered with activated = 1, to insert a row into another table with there user_id.

 

if ($result = mysql_query("SELECT id FROM users WHERE activated = 1")) {
  if (mysql_num_rows($result)) {
    while ($row = mysql_fetch_array($result)) {
      if ($result1 = mysql_query("SELECT id FROM $tablename WHERE id = {$row['id']}")) {
        if (!mysql_num_rows($result1)) {
          mysql_query("INSERT INTO $tablename (user_id) VALUES ({$row['id']})");
        }
      }
    }
  }
}

 

Its still a horrible method, where is $tablename coming from?

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.