Skip to content

Instantly share code, notes, and snippets.

@ederrafo
Created January 4, 2017 22:52
Show Gist options
  • Save ederrafo/1fd3ee3fc0f01b3dbd657b9d749018f7 to your computer and use it in GitHub Desktop.
Save ederrafo/1fd3ee3fc0f01b3dbd657b9d749018f7 to your computer and use it in GitHub Desktop.
php pdo insert update
<?php
include 'libraries/PhpExcelReader/excel_reader.php';// include the class
$fileName = '/home/user/vagrant/ubuntu14.04-64/projects/html/php/excel-read/files/Formato-Migracion-Account.xls';
$data = new PhpExcelReader;
$data->read($fileName);
$dataBase = "database";
$host = "xxxxx.amazonaws.com";
$port = "3306";
$user = "root";
$password = 'xxxxxxxxxxxxxxx';
try{
$pdo = new PDO("mysql:host=$host;port=$port;dbname=$dataBase", $user, $password);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
$pdo->beginTransaction();
for ($i = 1; $i <= $data->sheets[0]['numRows']; $i++) {
$agencyID = $data->sheets[0]['cells'][$i][1];
$agencyRUC = $data->sheets[0]['cells'][$i][2];
$query = "UPDATE table SET ruc = :ruc WHERE AccountID = :accountid";
$stmt = $pdo->prepare($query);
$stmt->bindParam(':ruc', $agencyRUC, PDO::PARAM_STR);
$stmt->bindParam(':accountid', $agencyID, PDO::PARAM_STR);
$stmt->execute();
}
$pdo->commit();
} catch(PDOException $err) {
$pdo->rollback();
die($err->getMessage());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment