Skip to content

Instantly share code, notes, and snippets.

@tps2015gh
Last active February 20, 2018 16:38
Show Gist options
  • Save tps2015gh/fbbdaaf0ff3c69dff3ec6babc7fdce5d to your computer and use it in GitHub Desktop.
Save tps2015gh/fbbdaaf0ff3c69dff3ec6babc7fdce5d to your computer and use it in GitHub Desktop.

DEMO only
PDO sqlite and PHP

<?php
// REF http://www.if-not-true-then-false.com/2012/php-pdo-sqlite3-example/
//
date_default_timezone_set("Asia/Bangkok");
//$db = new PDO('sqlite::memory:');
$db = new PDO('sqlite:db.sqlite');
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// REF: http://stackoverflow.com/questions/1717495/check-if-a-database-table-exists-using-php-pdo
function is_table_exists($db, $table) {
try {
$result = $db->query("SELECT 1 FROM $table LIMIT 1");
} catch (Exception $e) {
return FALSE;
}
return $result !== FALSE;
}
<script
src="https://code.jquery.com/jquery-3.2.1.min.js"
integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4="
crossorigin="anonymous"></script>
<script
src="https://code.jquery.com/ui/1.12.1/jquery-ui.min.js"
integrity="sha256-VazP97ZCwtekAsvgPBSUwPFKdrwD3unUfSGVYrahUqU="
crossorigin="anonymous"></script>
<link rel="stylesheet" type="text/css" href="https://code.jquery.com/ui/1.12.1/themes/blitzer/jquery-ui.css">
<script>
// REF: https://code.jquery.com/
// REF: http://stackoverflow.com/questions/3423842/jquery-dialog-with-dynamic-content
var show_dialog_add = function(){
// alert("ok");
var newDiv = $(document.createElement('div'));
$(newDiv).html('Enter Title: <input type="text" name="title" > ');
$(newDiv).dialog();
};
</script>
<?php
echo "<H1>PDO sqlite memory demo </H1>" ;
echo "<a href='index.php'>Home</a>";
echo " | " ;
echo "<a href='init_data.php'>init_data</a>";
echo " | " ;
echo "<span onclick='show_dialog_add();'>Add</span>";
echo "<hr>";
<?php
require("_db.php");
require("_menu.php");
echo "<h2>Display Result </h2>";
if(is_table_exists($db, "paper_pack")){
// QURERY
$result = $db->query("SELECT * from paper_pack order by id DESC ");
foreach($result as $row){
$obj = (object)$row;
$htm_row = "$obj->id/$obj->title/$obj->note_number/$obj->processed ";
echo $htm_row;
echo "<br>";
}
}else{
echo "<font color=red> Table Still not Exist</font><br><br>";
}
echo "<br><u>End of Page</u>";
<?php
// init_data.php
//
require("_db.php");
require("_menu.php");
if(! is_table_exists($db,"paper_pack")){
$db->exec("CREATE TABLE IF NOT EXISTS paper_pack(
id INTERGER PRIMARY KEY ,
title TEXT,
note_number TEXT ,
processed TEXT DEFAULT 'N'
)
"
);
$insert = "INSERT INTO paper_pack(id,title,note_number)
VALUES ( :id ,:title , :note_number )";
$stmt = $db->prepare($insert);
$stmt->bindParam(':id',$id);
$stmt->bindParam(':title',$title);
$stmt->bindParam(':note_number',$note_number);
$data = [
[1,'pack 1','aa xxx/xxx'],
[2,'pack 2','bb xxx/xxx'],
[3,'pack 2','bb xxx/xxx'],
];
foreach($data as $idx=>$row_array){
list($id,$title,$note_number) = $row_array;
echo "<BR>DBG: INSERT $id/$title/$note_number";
$stmt->execute();
}
}else{
echo "Table paper_pack already Exist";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment