0

I want something like this:

$i = 1;
$y = 1;

while ($i <= 10 and $y <= 5) {
    if ($i == 10) {
        echo 'I = ' . $i . ', Y = ' . $y . '<br>';
        $i = 1;
        $y = $y + 1;
    } else {
        echo 'I = ' . $i . ', Y = ' . $y . '<br>';
        $i = $i + 1;
    }
}

Which returns

I = 1, Y = 1
I = 2, Y = 1
....
I = 1, Y = 2
I = 2, Y = 2
....
I = 1, Y = 3
I = 2, Y = 3

etc, etc, to use with cURL. But it doesn't work. What am I doing wrong?

$i = 1;
$y = 1;

while ($y <= 9) {
    while ($i == 499) {
        if ($connection = db_connect()) {
            $post = array(
                'something' => 'abc',
                'value_of_y' => '' . $y . '',
                'value_of_i' => '' . $i . ''
            );
            $str = curl_grab_page('http://localhost/send.php?true=1', '', 'off', $post);
            $str2 = mysql_real_escape_string($str);
            $sql = "INSERT INTO abc (txt) VALUES ('$str2')";
    
            $i = 1;
            $y = $y + 1;
        } else {
            echo 'Database connection error!';
        }
    }
    while ($i != 499) {
        if ($connection = db_connect()) {
            $post = array(
                'something' => 'abc',
                'value_of_y' => '' . $y . '',
                'value_of_i' => '' . $i . ''
            );
            $str = curl_grab_page('http://localhost/send.php?true=1', '', 'off', $post);
    
            $str2 = mysql_real_escape_string($str);
            $sql = "INSERT INTO abc (txt) VALUES ('$str2')";
    
            $i = $i + 1;
        } else {
            echo 'Database connection error!';
        }
    }
}

But the page keeps loading and nothing happens. I have to use if($connection = db_connect()){, but I think this is what keeps messing up my code.

3
  • Why are you connecting 500 times? I'd connect first, and then start the loop. Also, the two cURL posts are identical: keep it DRY; Don't Repeat Yourself. Commented Jul 16, 2012 at 10:10
  • If your database connection fails your loop will just go forever since you never exit. Commented Jul 16, 2012 at 10:18
  • Right, forgot about mysql query. Thx Commented Jul 16, 2012 at 10:36

2 Answers 2

2

My solution would be as follows:

$y = $x = 1;

if (!$connection = db_connect()) {
   echo "connection failed!";
} else {
   while ($y <= 9) {
     $x = 1;
     while ($x <= 499) {
       // do your things with $x and $y, don't forget mysql_query() !

       $x++;   
     }
     y++;
   }
 }
Sign up to request clarification or add additional context in comments.

Comments

-1

Maybe this is a start?

$sql = "INSERT INTO abc (txt) VALUES ('".$str2."')";

3 Comments

I dont think so, because it works without the while loops, but I will try your way too
This syntax works but is not required. The INSERT code from the OP will work just fine because the string is in double quotes.
Yeah, this changed nothing, still nothing is being added into DB

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.