3

Basically I made it to create a SQL query to insert about 5000 random numbers in a database $i (currently 10 just to figure it out).

I previously tried to do it solely in MYSQL, but failed and don't want to waste anymore time there.

It randomises a 10 digit number and cross checks that number with and array ($codes) which contains over 100,000 values from different tables in a database. It also adds the new random number to a new array ($newNumbers) so it doesn't make the same new number, then it adds the numbers to an INSERT query. It also has a nested while loop to create a second number, which also has to be completely unique.

Hope that makes sense.. anyway the problem seems to be within the nested while which seems to infinite loop (and not increase $i), but I can't work out why.

$query = "INSERT INTO 'table' ('num_id', `num1`, `num2`) VALUES";

$i = 0;
while ($i <= 10){

    $rand = mt_rand(1000000000, 9999999999);
    if (!in_array($rand, $codes) && !in_array($rand2, $newNumbers) ){
        $newNumbers[] = $rand;

        $second = 0;
        while ($second == 0){
            $rand2 = mt_rand(1000000000, 9999999999);
            if (!in_array($rand2, $codes) && !in_array($rand2, $newNumbers) ){
                $query .= " ('', $rand, $rand2)";
                $newNumbers[] = $rand2;
                $second = 1;

                $i++;
            }else
                $duplicates ++;
        }//end second loop



    }else
        $duplicates ++;
}//while
  • 3
    Check this line:`$query .= " ('', $rand, $rand2")";` parse error here, you have 3 x `"` :) – Vlad Preda Jan 17 '13 at 11:40
  • Thanks, I simplified the query a little for posting, I checked and it was correct on the actual one. :) – stuckinspace Jan 17 '13 at 11:42
  • Use editor that has syntax highlighter NetBeans is a good free alternative. – DaGhostman Dimitrov Jan 17 '13 at 11:42
  • Haha just fixed it myself after an hour of giving the screen a stare-down, I seem to be checking $rand2 in the first IF before its changed to a new one.`if (!in_array($rand, $codes) && !in_array($rand2, $newNumbers) ){` – stuckinspace Jan 17 '13 at 11:47

4 Answers4

2
$query = "INSERT INTO 'table' ('num_id', `num1`, `num2`) VALUES";

$i = 0;
while ($i <= 10){

    $rand = mt_rand(1000000000, 9999999999);
    if (!in_array($rand, $codes) && !in_array($rand2, $newNumbers) ){
        $newNumbers[] = $rand;

        $second = 0;
        while ($second == 0){
            $rand2 = mt_rand(1000000000, 9999999999);
            if (!in_array($rand2, $codes) && !in_array($rand2, $newNumbers) ){
                $query .= " ('', $rand, $rand2)";//missing quotes
                $newNumbers[] = $rand2;
                $second = 1;

                $i++;
            }else
                $duplicates ++;
        }//end second loop



    }else
        $duplicates ++;
}//while
Sibu
  • 4,609
  • 2
  • 26
  • 38
1
$query .= " ('', $rand, $rand2")"; 

check your quotes

0x_Anakin
  • 3,229
  • 5
  • 47
  • 86
0

You have an extraneous " character in your $query .= " ('', $rand, $rand2")"; line. You should amend the code as follows:

$query = "INSERT INTO 'table' ('num_id', `num1`, `num2`) VALUES";

$i = 0;
while ($i <= 10){

    $rand = mt_rand(1000000000, 9999999999);
    if (!in_array($rand, $codes) && !in_array($rand2, $newNumbers) ){
        $newNumbers[] = $rand;

        $second = 0;
        while ($second == 0){
            $rand2 = mt_rand(1000000000, 9999999999);
            if (!in_array($rand2, $codes) && !in_array($rand2, $newNumbers) ){
                $query .= " ('', $rand, $rand2)";
                $newNumbers[] = $rand2;
                $second = 1;

                $i++;
            }else
                $duplicates ++;
        }//end second loop



    }else
        $duplicates ++;
}//while
BenM
  • 52,573
  • 26
  • 113
  • 168
0

So It seems I was checking that $rand2 wasn't in the $newNumbers array in the first IF statement, which after the first loop of $i, $rand2 would be in $newNumbers every time as it won't get regenerated , hence the infinite loop.

Also thanks everyone for spotting the parse error with the extra quotes, I had it correct in TextWrangler (which is epic by the way), but made an error when simplifying the query for posting.

if (!in_array($rand, $codes) && !in_array($rand2, $newNumbers) ){

$query = "INSERT INTO 'table' ('num_id', `num1`, `num2`) VALUES";

$i = 0;
while ($i <= 10){

    $rand = mt_rand(1000000000, 9999999999);
    if (!in_array($rand, $codes) && !in_array($rand, $newNumbers) ){
        $newNumbers[] = $rand;

        $second = 0;
        while ($second == 0){
            $rand2 = mt_rand(1000000000, 9999999999);
            if (!in_array($rand2, $codes) && !in_array($rand2, $newNumbers) ){
                $query .= " ('', $rand, $rand2)";
                $newNumbers[] = $rand2;
                $second = 1;

                $i++;
            }else
                $duplicates ++;
        }//end second loop



    }else
        $duplicates ++;
}//while