title: Using transactions in PHP and MySQL
body: <p>I'm a learner of PHP. This commit and rollback process does the task at each line. How can I commit and rollback all at once?</p>
  
  <pre><code>&lt;?php
  $con = mysql_connect('localhost','user', 'abcdefg');
  if(!con)
  {
      echo "Can't connect to db.";
  }else {
      mysql_select_db('test');
  }
  
  if(!file_exits('test.csv')
  {
      echo "Can't find the file.";
  }
  
  $ar_1 = file('test.csv', FILE_IGNORE_NEW_LINES);
  
  foreach ($ar_1 as $ar1)
  {
      $test = explode(",", $ar1); 
      $sql = "SELECT * FROM DB WHERE CD = '$test[0]'";
      $result = mysql_query_($sql);   
      $sql = "INSERT INTO DB(CODE) VALUES ('$test[0]');";
      $result = mysql_query($sql);
      if($result === true)
      {
          //Commit
          $sql = "commit";
          mysql_query($sql);
          echo "Committed";
      }else {
          //Rollback
          $sql = "rollback";
          mysql_query($sql);
          echo "Rollback";
      }
  }
  mysql_close($con);
  ?&gt;
  </code></pre>