简体   繁体   中英

Pause foreach and continue

I'm trying to loop data from a api and then post these values to a MySQL db.

something like this:

$values = json_decode(file_get_contents("my-json-file"));

$SQL = new mysqli(SQL_HOST, SQL_USER, SQL_PASS, DB_NAME);
$SQL->autocommit(FALSE);

foreach($values As $item)
{
    $query = "INSERT INTO my_table VALUES ('".$item->value1."', '".$item->value2.";)";
    $SQL->query($query);
    if(!$SQL->commit())
    {
        echo "ERROR ON INSERT: [" . $query . "]<hr/>";
    }
}
$SQL->close();

Since the loop is too fast, the SQL can't catch up. (Yea!) I would then need something like this:

foreach($values As $item)
{
    /**** STOP/PAUSE LOOP ****/

    $query = "INSERT INTO my_table VALUES ('".$item->value1."', '".$item->value2.";");
    $SQL->query($query);
    if($SQL->commit())
    {
        /**** START THE LOOP AGAIN ****/
    }
    else
    {
        echo "ERROR ON INSERT: [" . $query . "]<hr/>";
    }
}

Or how should I do this the right way?

EDIT: It inserts random posts every time.

EDIT 2: This is just example code. It does escape and all that, and yes the semi colon is wrong here but since so many commented on it i will not change it. This was not the problem in the real case. I tried to run it on another server and there it worked. The problem was fixed by restarting MAMP.

Firstly, your idea that the loop runs too fast for MySQL to keep up is completely totally wrong. The $SQL->query() call will wait for the MySQL to return a response before proceeding, so the loop won't run any faster than MySQL is responding.

Now onto the actual problem.... your query:

$query = "INSERT INTO my_table VALUES ('".$item->value1."', '".$item->value2.";)";

There's a semi-colon in there at the end, after value2 which is invalid. I guess you intended to type a quote mark there? The semi-colon will be causing all your queries to fail and throw errors.

This may be the cause of your problem but you haven't got any error checking in there, so you won't know. Add some error checking to your code after calling the query; even if the query is right, it's still possible to get errors, and your code should check for them. See the examples on this manual page: http://www.php.net/manual/en/mysqli-stmt.error.php

Finally, since you're using the mysqli API, it's worth mentioning that your code would be a lot better and probably more secure if you used prepared statements. See the examples in PHP manual here: http://www.php.net/manual/en/mysqli-stmt.bind-param.php

[EDIT]

Another possible reason your query is failing is that you're not escaping the input values. If any of the input values contains a quote character (or any other character that is illegal in SQL) then the query will fail. In addition, this problem makes your code vulnerable to a SQL injection hacking attack.

You need to escape your input using $SQL->real_escape_string() OR by changing your query to use prepared statements (as recommended above).

您的查询在循环内,这意味着循环将一直等到您的查询完成执行后才继续执行,按顺序处理php代码...

Has @phpalix said, PHP goes in order, and waits for the previous action to finish.

I think you SQL is wrong. Try replacing your INSERT with this:

$query = "INSERT INTO my_table VALUES ('".$item->value1."', '".$item->value2."');";

And don't forget to run at least mysql_real_escape_string for each variable, for security measures.

As many of the answers and comments say, it does not continue until the SQL is done. The problem was in my local apache/mysql server. It was fixed by restarting it. Yes, stupid post.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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