简体   繁体   English

PHP MYSQL Insert语句在FOR循环中挂起

[英]PHP MYSQL Insert statement in FOR loop Hanging

I have spent many hours debugging, and scouring the internet for a solution to this unusual problem. 我花了许多时间进行调试,并在Internet上搜寻有关此异常问题的解决方案。 Heres the deal: 这是交易:

I am working on a Work Order Submission and Tracking system. 我正在工作单提交和跟踪系统上。 There are two databases involved: 涉及两个数据库:

  1. The database where the submissions data gets posted, which is located on the same physical machine, but on a separate virtual machine as the webserver serving the php. 投递提交数据的数据库,该数据库位于同一台物理计算机上,但与服务php的Web服务器位于不同的虚拟机上。 They are on the same class C subnet. 它们在同一C类子网中。
  2. The database of our tracking system. 我们的跟踪系统的数据库。 Located on a different physical server on a different IP altogether, also a virtual machine. 完全位于不同IP上的不同物理服务器上,也位于虚拟机上。

Our work order system allows for multiple 'services requested', stored in an array. 我们的工作单系统允许将多个“请求的服务”存储在一个数组中。 In our sumbissions database, this is stored as a comma separated string, ie "40,60,70" but in our tracking system database, each 'service requested' needs a separate entry, as to allow the different aspects of the project to be tracked and completed at different times, by different staff. 在我们的sumbissions数据库中,它以逗号分隔的字符串形式存储,即“ 40,60,70”,但是在我们的跟踪系统数据库中,每个“请求的服务”都需要一个单独的条目,以使项目的各个方面得以实现。由不同人员在不同时间跟踪和完成。

THE PROBLEM IS: When I place my second insert statement, the one destined for the tracking database, in a for loop, it completely hangs, and takes maybe 5 to 15 minutes, before it passes that point in the code, and sends the confirmation email. 问题是:当我将第二条插入语句(用于跟踪数据库的一条语句)放置在for循环中时,它完全挂起,可能需要5到15分钟,然后它才能通过代码中的该点并发送确认电子邮件。 The data does not get inserted either. 数据也不会插入。

When I take it out of the for loop and simply do one insert in the submissions database and one insert into the tracking system, it works fine. 当我将它从for循环中取出并在提交数据库中进行一次插入,然后在跟踪系统中进行一次插入时,效果很好。

First, Ill post the code that works, but only posts one 'service' to the tracking system: 首先,我会发布有效的代码,但只会向跟踪系统发布一项“服务”:

 public function insertOrder()
{
    $services = implode( ',', $this->model->chk );
    $curdate = $this->model->getMySQLDate( $this->model->curdate );
    $dueDate = $this->model->getMySQLDate( $this->model->dueDate );

    $sql = "INSERT INTO orders VALUES(DEFAULT,
            {$this->sanitize($services)},
            {$this->sanitize($curdate)},
            {$this->sanitize($this->model->submittedBy)},
            {$this->sanitize($this->model->shortDesc)},
            {$this->sanitize($this->model->projDetails)},
            {$this->sanitize($dueDate)},
            {$this->sanitize($this->model->dueDateNotes)},
            {$this->sanitize( $this->model->approveBy)},
            {$this->sanitize( $this->model->cost )} )";

    $this->execute( $sql );

    $this->convertServicesToTracks();
    $notes = $this->model->getTracksNotes();
    $dueDate = $dueDate.' 12:00:00';
    $shortDescNoQuotes = str_replace("\"","'",$this->model->shortDesc);

    $sqlTracks = "INSERT INTO todos VALUES(DEFAULT,
            {$this->sanitizeTracks($this->model->chk[0])},
            NULL,
            {$this->sanitizeTracks($shortDescNoQuotes)},
            {$this->sanitizeTracks($notes)},
            now(),
            {$this->sanitizeTracks($dueDate)},
            NULL,
            12,
            NULL,
            'active',
            NULL,
            now() );";

    //echo $sqlTracks;

    $this->executeTracks( $sqlTacks );
}    private function executeTracks( $sql )
{
    $db = $this->getTracksDB( );


    $this->check4Error( $db, $sql );

    return $result;
}

private function getTracksDB()
{
    if (!$this->tracksdb) $this->tracksdb = new mysqli(AbstractSQL::TRACKS_HOST, AbstractSQL::USER, AbstractSQL::PASS, AbstractSQL::TRACKS_SCHEMA);
    return $this->tracksdb;
}

private function convertServicesToTracks()
{
   //converts submission data to tracking system data
}



private function sanitizeTracks($arg)
{
    if (!isset($arg)) return "NULL";
    if (is_numeric($arg) && !is_double( $arg) ) return $arg;
    return "'{$this->getTracksDB()->escape_string($arg)}'";
}

When I add this simple for loop around the second INSERT statement, it hangs, even if the array only has one item! 当我在第二条INSERT语句周围添加这个简单的for循环时,即使数组只有一项,它也会挂起!

    for($i = 0; $i < count($this->model->chk); ++$i)
    {
        $sqlTracks = "INSERT INTO todos VALUES(DEFAULT,
            {$this->sanitizeTracks($this->model->chk[$i])},
            NULL,
            {$this->sanitizeTracks($shortDescNoQuotes)},
            {$this->sanitizeTracks($notes)},
            now(),
            {$this->sanitizeTracks($dueDate)},
            NULL,
            12,
            NULL,
            'active',
            NULL,
            now() );";

    //echo $sqlTracks;

    $this->executeTracks( $sqlTacks );
    }

Any help would be greatly appreciated. 任何帮助将不胜感激。 And I apologize for the long code snippets!! 对于冗长的代码片段,我深表歉意!!

Is it iterating through the for loop? 是否在for循环中迭代? I see you have an echo, did that write anything out? 我看到你有回声,那有没有写出来? How many items does it have to iterate through? 它必须迭代多少项? 5 min seems like a long time but if there are a lot of items that could be why it's taking so long. 5分钟似乎很长一段时间,但是如果有很多物品,可能就是为什么要花这么长时间。 Are you seeing any errors in your logs? 您是否在日志中看到任何错误?

Something you might try is hold the count in a variable so it doesn't have to calculate that each time. 您可以尝试将计数保存在变量中,这样就不必每次都进行计算。 It might speed up your for loop but I'm not sure it will insert the data. 它可能会加快for循环的速度,但是我不确定它将插入数据。

for($i = 0, $count = count($this->model->chk); $i < $count; ++$i)
    {
        $sqlTracks = "INSERT INTO todos VALUES(DEFAULT,
            {$this->sanitizeTracks($this->model->chk[$i])},
            NULL,
            {$this->sanitizeTracks($shortDescNoQuotes)},
            {$this->sanitizeTracks($notes)},
            now(),
            {$this->sanitizeTracks($dueDate)},
            NULL,
            12,
            NULL,
            'active',
            NULL,
            now() );";

    //echo $sqlTracks;

    $this->executeTracks( $sqlTacks );
    }

I found this in the PHP for loop reference: http://php.net/manual/en/control-structures.for.php 我在PHP for循环参考中找到了这一点: http : //php.net/manual/en/control-structures.for.php

Well, this may not be the problem, but shouldn't you generally use a foreach loop to avoid hitting parts of the array that may not exist? 好吧,这可能不是问题,但是通常不应该使用foreach循环来避免命中可能不存在的数组部分吗? There is more about this here . 还有更多关于此这里 If you loop through an empty index, it would break your SQL statement. 如果您循环通过一个空索引,它将破坏您的SQL语句。 Like this: 像这样:

foreach($this->model->chk as $val)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM