控制php

时间:2017-02-03 09:22:14

标签: php while-loop infinite-loop

我已经通过php

在mysql中插入了一些数据

此代码有效:

$i = 0;
while($i<6)
{   
    $i++;
    $params['name'] = 'test';
    $last_inserted_id = $database->insert('item',$params);//medoo databaseb lib

}

它在数据库中插入5行:

但我有一个功能可以对图像产生一些影响。 (它返回一些json代码)。该功能没有任何错误。

$i = 0;
    while($i<6)
    {   
        $i++;
        $_SESSION["debug_msg"][] ='counter in while:'.$i;  
        $params['name'] = 'test';
        $params['file'] = '';
        if($last_inserted_id = $database->insert('item',$params))
        {
            $proccessImageJson =  proccessImage($last_inserted_id);
            if($proccessImageJson)
            {
                $params['file'] = '$proccessImageJson';
                $database->update(TPREFIX.'item',$params,['id'=>$last_inserted_id]);
            }
        }

    }

此代码也有效...但不限制为5!可能是40或50或更多...

我怎么能控制它?

我也尝试(尝试/捕获)功能部分,但我没有错误。

try
{
     if($proccessImageJson =  proccessImage($last_inserted_id))
     {
          $params['file'] = '$proccessImageJson';
          $database->update(TPREFIX.'item',$params,['id'=>$last_inserted_id]);
      }
  }
  catch (SomeException $e)
  {
       ob_start();
       echo 'error:';
       echo $e->getMessage();
       $_SESSION["debug_msg"][] ='function error:'.ob_get_clean();    
  }

最后我用$ _SESSION计算循环:

$_SESSION["debug_msg"][] ='<br>counter in while :'.$i;

打印$ i表格1到5并重复agian再次!

所以我认为可能会多次调用URL。

我的真实地址:     example.com?id=admin&do=insertdemo

但我改变了它.htaccess到此:     example.com/admin/insertdemo

使用此代码:

RewriteCond %{THE_REQUEST} (?:index\.php)?\?id=([^\s&]+)&do=([^\s&]+)\sHTTP [NC]
RewriteRule ^ %1/%2? [L,R]

RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]

RewriteRule ^([^/]+)/([^/]+)/?$ index.php?id=$1&do=$2 [L,QSA]

所以我删除.httacess代码我用orginall地址调用该页面:

example.com?id=admin&do=insertdemo

但是我得到了无限循环agian ......

我错了什么?

3 个答案:

答案 0 :(得分:0)

您的代码似乎没问题,它永远不会进入无限循环。我只能建议你尝试将$i++;行移到循环的末尾,而不是在开始时。

代码进入无限循环可能是因为代码中存在一些错误。您可以通过在代码的开头添加以下行然后再次执行来检查:

ini_set('display_errors', '1');

答案 1 :(得分:0)

你问题中显示的循环不是无限的。你有一个名为long的变量,一个自然数的结束符号,你可以在每次迭代中增加它。结果你有一个执行这个循环的循环。它可以在您的PHP代码中,也可以重复加载页面,或者多个用户可能正在运行同一页面。

答案 2 :(得分:0)

谁想知道如何解决我的问题:

在proccessImage函数中,我有这样的形式:

proccessImage($last_inserted_id)
{
    $this->anotherFunction();
}

原因是$this->anotherFunction()

$this->anotherFunction()不会返回任何值。

我改变了这个:

proccessImage($last_inserted_id)
 {
        if($this->anotherFunction())
        { 
            /continue
        }
 }

anotherFunction() /makes images via GD lib
{
    //some code
    return true;
}

我不知道为什么我有循环,但有了这份工作,我可以解决我的问题。