MoonFox
MoonFox

Reputation: 31

Using transactions inside double for loop

How can I create transactions for queries ? for every new account I'm creating 210 new records into the database. I need transactions because the 210 queries taking time and I use the mysql_insert_id() to get the last inserted id. here the code:

$sql = "INSERT INTO bla bla....";

$result =  mysql_query($sql);
$lastid = mysql_insert_id();

if($result)
{
    for($i=0; $i<=6; $i++)
    {
        for($j=1; $j<=30; $j++)
        {
            $query_day = date('Y-m-d', strtotime('+'.$i.' days 1 hours'));
            $sql_insert = "INSERT INTO `e-heal`.`scr` (`sc_id`, `d_id`, `hour_id`, `date`, `a`) VALUES (NULL, '$lastid', '$j', '$query_day', '0');";
            mysql_query($sql_insert);
         }
    }     
}

Upvotes: 1

Views: 91

Answers (1)

zerkms
zerkms

Reputation: 255005

You don't need transactions here - mysql_insert_id() GUARANTEES to return the latest autoincrement value from the current session.

Upvotes: 1

Related Questions