K
K
khodos_dmitry2018-02-26 10:08:44
PHP
khodos_dmitry, 2018-02-26 10:08:44

Why doesn't this while construct work?

while ($i<5) {
    $query = "SELECT DISTINCT(`url`) FROM `str` WHERE `parsed` = 0";
    $result = mysqli_query($link, $query) or die('Не могу взять из базы url '.mysqli_error($link));
    $rows = mysqli_fetch_all($result, MYSQLI_ASSOC);
//		if (count($rows) == 0) break;
    $i++;
    foreach ($rows as $row) {
      $url = $row['url'];
  //		$options[CURLOPT_PROXY] = $proxy_array[ mt_rand(0, count($proxy_array) - 1) ];
      $AC->request($url, 'GET', NULL, NULL, NULL);
    }
  }

It should select unparsed pages from the database, then the page is parsed in the request function and new urls are added to the database.
Then new urls are selected, parsed, and so on.
But for some reason it doesn't work correctly. So parsit only 1 time.
When you run over and over again without while, then everything works. But, when you set while, it is executed only once, no matter how many cycles there are. When while(true) if (count($rows) == 0) break; runs indefinitely, and nothing changes in the database.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
ivankomolin, 2018-02-27
@ivankomolin

In general, while is a very simple construction, and if $i = 0, then the content will be executed exactly 5 times (naturally, if there is no stop in the content)
First: print a wardump inside the loop to judge the number of iterations of the loop consciously.
Example:

while ($i<5) {
   var_dump($i);
...

If 0, 1, 2, 3, 4 are displayed in turn, then we consider that while works more than it does not work and go to the second one))
Second: check the code inside the loop, say that it is strange - to say nothing.
What for to do 5 identical requests in a DB? Do you have something changing in the database during the time between iterations? Specifically, there is nothing of the kind in this code, judging by this, it will write the same thing in $rows 5 times. And accordingly, the rikquests inside the second cycle will also be repeated.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question