Tuesday, November 21, 2017


##connect database
try{
$db = new PDO('mysql:host=localhost;dbname=boxcontrol;charset=utf8mb4', 'root', '');
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
} catch (PDOException $e){
echo 'Connection failed: ' . $e->getMessage();
exit();
}

##execute sql string
$sql="INSERT INTO all_record (barcodeid,status,plant)
values ('$barcodeid','$status','Matco1')";
$error_check=0
try {
$affected_rows = $db->exec($sql);
} catch (PDOException $e) {
echo 'Connection failed: ' . $e->getMessage();
$error_check=1;
}

## delete 1 parameter Array parameter

$error_check=0
try {
$stmt = $db->prepare("DELETE FROM matco1 WHERE id=:id");
$stmt->execute(array(':id'=>$_REQUEST['id']));
} catch (PDOException $e) {
echo 'Connection failed: ' . $e->getMessage();
$error_check=1;
}

##execute sql select

$error_check=0
try {
$stmt = $db->query($sql);
} catch (PDOException $e) {
echo 'Connection failed: ' . $e->getMessage();
$error_check=1;
}

while($row = $stmt->fetch(PDO::FETCH_ASSOC))

$num_rows = $stmt->fetchColumn();