views:

103

answers:

6
function addAds($n) {
 for ($i=0;$i<=$n;$i++) {
  while($row=mysql_fetch_array(mysql_query("SELECT * FROM users"))) {
   $aut[]=$row['name'];
  }
  $author=$aut[rand(0,mysql_num_rows(mysql_query("SELECT * FROM users")))];
  $name="pavadinimas".rand(0,3600);
  $rnd=rand(0,1);
  if($rnd==0) {
   $type="siulo";
  } else {
   $type="iesko";
  }
  $text="tekstas".md5("tekstas".rand(0,8000));
  $time=time()-rand(3600,86400);
  $catid=rand(1,9);
  switch ($catid) {
   case 1:
    $subid=rand(1,8);
    break;
   case 2:
    $subid=rand(9,16);
    break;
   case 3:
    $subid=rand(17,24);
    break;
   case 4:
    $subid=rand(25,32);
    break;
   case 5:
    $subid=rand(33,41);
    break;
   case 6:
    $subid=rand(42,49);
    break;
   case 7:
    $subid=rand(50,56);
    break;
   case 8:
    $subid=rand(57,64);
    break;
   case 9:
    $subid=rand(65,70);
    break;
  }
  mysql_query("INSERT INTO advert(author,name,type,text,time,catid,subid) VALUES('$author','$name','$type','$text','$time','$catid','$subid')") or die(mysql_error());
 }
 echo "$n adverts successfully added.";
}

The problem with this function, is that it never loads. As I noticed, my while loop causes it. If i comment it, everything is ok. It has to get random user from my db and set it to variable $author.

+5  A: 

The problem is that the query is in the loop, so it gets run every time (so you start from the beginning every time). Just move the mysql_query() part to right before the while loop and store it in a variable:

$query = mysql_query("SELECT * FROM users");
while($row=mysql_fetch_array($query))
Brendan Long
A: 

You're starting a new query each time you run the loop.

SLaks
+2  A: 

The condition of a while loop is executed and evaluated with each iteration. So mysql_query is called with every iteration and retunrs true.

Just execute your database query once and cache the result:

function addAds($n) {
    $result = mysql_query("SELECT * FROM users");
    $aut = array();
    while ($row = mysql_fetch_array($result)) {
        $aut[]=$row['name'];
    }
    $rowCount = count($aut);
    for ($i=0; $i<=$n; $i++) {
        $author=$aut[rand(0,$rowCount)];
        // …
        mysql_query("INSERT INTO advert(author,name,type,text,time,catid,subid) VALUES('$author','$name','$type','$text','$time','$catid','$subid')") or die(mysql_error());
    }
    echo "$n adverts successfully added.";
}
Gumbo
+1  A: 

It's a lot of time that I don't use PHP but I think that the assignment

$row=mysql_fetch_array(mysql_query("SELECT * FROM users"))

should always returns true, it executes the query again and again on every iteration..

Jack
+3  A: 

You can replace this mega switch with one line:

$subid = rand(($catid * 8) - 7, min($catid * 8, 70));
Svisstack
+1 for discovering TRWTF
Brendan Long
That’s not quite correct. But it may suffice Tom’s needs.
Gumbo
@Gumbo: Thinking about this randoms, i think is a good approx ;-) But if they dont want aproxx they can add to this 3 lines and get 100% reflection. 4 lines is better than 29.
Svisstack
@Svisstack: Well, since it’s using `rand` you could also just write `$subid=4;`. ;-)
Gumbo
@Gumbo: ;-D heh
Svisstack
+2  A: 

I also think the problem is your functions are way too big to understand(quickly). You should make them smaller and test them with a unit testing framework like phpunit.

Alfred