DEMO only
PDO sqlite and PHP
Last active
February 20, 2018 16:38
-
-
Save tps2015gh/fbbdaaf0ff3c69dff3ec6babc7fdce5d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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; | |
} | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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>"; | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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