create script => no need to database backup

This commit is contained in:
Adrien Marquès 2018-09-26 15:51:27 +02:00
parent 49b45199b8
commit 477285c775
1 changed files with 57 additions and 1 deletions

View File

@ -1,4 +1,60 @@
<?php <?php
echo "my app"; try{
$pdo = new PDO('mysql:host=mariadb;dbname=vhost;charset=utf8', "php", "Ew1RmFFqeZOA1zwkJkocAjmepQA4JmnUW0nqDFh79jHY8aubtPO_12E9wgbNvScX0Uiv1GuUllb8YCJ00PAoh0fvNS");
$pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
$pdo->setAttribute(PDO::ATTR_TIMEOUT, 5);
$pdo->setAttribute(PDO::ERRMODE_EXCEPTION, true);
$pdo->setAttribute(PDO::ATTR_STRINGIFY_FETCHES, false);
$pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
}catch(Exception $e){
printf("Cannot connect to database\n");
die();
}
// 1. Create table
$create = "CREATE TABLE IF NOT EXISTS nonce (
id_nonce int NOT NULL AUTO_INCREMENT PRIMARY KEY,
created_at int NOT NULL DEFAULT CURRENT_TIMESTAMP
);";
if( $errcode = $pdo->exec($create) > 0 ){
printf("* [%d] cannot create table\n\n", $errcode);
die();
}
// 2. Insert new line
$pr = $pdo->prepare("INSERT INTO nonce(id_nonce, created_at) VALUES(DEFAULT, :tnow);");
if( $pr !== false ){
$ok = $pr->execute([":tnow" => time()]);
if( !$ok ){
printf("* cannot insert data\n\n");
}
}else{
printf("* cannot insert data\n\n");
}
// 3. select all
$st = $pdo->query("SELECT * FROM nonce ORDER BY id_nonce;");
if( $st !== false ){
$nonces = $st->fetchAll();
printf("<pre>\n");
foreach( $nonces as $key=>$value ){
printf("[%d] nonce id %s created at %s\n", $key, $value['id_nonce'], date('H:i:s d/m/Y', $value['created_at']));
}
printf("</pre>\n");
}else{
printf("* cannot fetch data\n\n");
}
echo "my app";