File : postgresqlworking.php. ID : 9145
Skin : Default | Sons-of-obsidian | Sunburst | Highlighter | Frame
<?php
// PostgreSQL in UTF-8 charset
ini_set('max_execution_time', 86400); // 86400 = 60 * 60 * 24 seconds = 24 Hours
/* Section 1 : Configuration */
// 1.1 Main configuration
$db = parse_url(getenv("DATABASE_URL"));
$connect = new PDO("pgsql:" . sprintf("host=%s;port=%s;user=%s;password=%s;dbname=%s",$db["host"],$db["port"],$db["user"],$db["pass"],ltrim($db["path"], "/")));
$host = $db["host"];
$db =  ltrim($db["path"], "/");
$tb = 'car';
$user = $db["user"];
$password =  $db["pass"];
$file = "postgresqlworking.php"; // index.php
$start = microtime();
// 1.2 Security option
$protect_sql_injection = false; // true = safe; false = unsafe
$phpinfo_function_allow = true; // true = can use phpinfo()
// 1.3 Database connection
// $connect = new mysqli($host,$user,$password,$db);
if ($connect->connect_error) die("Connection Database :failed"); 
$tb_found = reccount("select count(*) from information_schema.tables where table_name like '$tb'");
// 1.4 phpinfo function
if (isset($_GET{'action'}) && ($_GET{'action'} == "phpinfo") && ($phpinfo_function_allow == true)) {  phpinfo(1);  exit; }
// 1.5 start html
echo '<html><head><title>PostgreSQL Working</title><meta http-equiv="content-type" content="text/html;charset=UTF-8" />
<style type="text/css">
textarea{font-size:10px;color:blue;scrollbar-base-color:red;scrollbar-arrow-color:white;background-color:#ffffdd;}
input{font-family:microsoft sans serif;font-size:16px;color:black;background:#ffffdd;}
</style></head><body>';
?> 
<!-- Section 2 : Menu screen -->
<form action="" method="post">
<table style="background-color:blue;border-width:15px;border-style:solid;width:740px;margin-left:auto;margin-right:auto;text-align:center;">
<?php if ($tb_found == 0) { ?>
<tr><td style="background-color:white">
<input type="submit" value="Create table" name="action" style="width:350px;height:50px;">
</td></tr>
<?php } else { ?>
<tr><td style="vertical-align:top;background-color:white;width:100;text-align:center;">
<input type="submit" value="post 1000" name="action" style="width:100px;background-color:yellow;">
<input type="submit" value="post_recs" name="action" style="width:100px;background-color:#dddddd;">
<input name="recs" value="1000" size="6">
</td>
<td style="background-color:#dddddd"><input type="submit" value="postmany" name="action" style="width:100px;background-color:#ddffdd;"> 
ข้อมูลข้างล่างนี้สามารถ copy จาก excel มา paste ได้<br/>
<textarea name="manyrecord" rows="4" cols="120" wrap="off">
1	2547		หจก.	กองเกตุเอ๊กซ์เปรส	20	11	2551	8/1 ม.2 แขวงทุ่งครุ 	ทุ่งครุ	กรุงเทพฯ			17		14			3						1		3
2	2547		หจก.	ดาวคะนองการท่องเที่ยว	5	1	2552	10/223 ม.4 แขวงดินแดง	ห้วยขวาง	กรุงเทพฯ			2		2											3
3	2547		บจ.	ธรรมนูญ โพธิ์ทอง	5	1	2552	1407/6 ซ.ตากสิน 7 ถ.สมเด็จพระเจ้าตากสิน แขวงบุคคโล	สายไหม	กรุงเทพฯ			4		2					2						3
4	2547		บจ.	นิติพงษ์ ไพบูลย์ทรานสปอร์ต	6	1	2552	15/20 ถ.นวลจันทร์ แขวงคลองกุ่ม	สายไหม	กรุงเทพฯ			1						1							3
5	2547		นาย	บางแคการท่องเที่ยว	6	1	2552	209/718 ม.ปรีชา 8 ถ.รามคำแหง แขวงหัวหมาก	สะพานสูง	กรุงเทพฯ			1											1		3
6	2547		นาย	ผาน  ตอลบรัมย์	6	1	2552	4/33 ม.6 แขวงสายไหม 	สวนหลวง	กรุงเทพฯ			1						1							3
7	2547		บจ.	วิจิตรประกอบ	7	1	2552	25 ซ.อ่อนนุช 10 ถ.อ่อนนุช แขวงสวนหลวง	ลาดพร้าว	กรุงเทพฯ		02-2743222-3	3		3											3
8	2547		หจก.	สตาร์แปซิฟิคทรานสปอร์ต	26	1	2552	41/9 ม.9 ถ.วิภาวดีรังสิต แขวงสีกัน 	บึงกุ่ม	กรุงเทพฯ			21		1					19	1					3
9	2547		นาย	สมนึก ผลดีนานา	12	1	2552	43 ซ.บรมราชชนนี 14 ถ.บรมราชชนนี แขวงบางบำหรุ 	บางพลัด	กรุงเทพฯ			1							1						3
10	2547		บจ.	สวัสดิภาพทัวร์	13	1	2552	44/73 ม.7 แขวงลาดพร้าว 	บางกะปิ	กรุงเทพฯ			2											2		3
11	2547		นาย	สุชาติ ภรมงคลธรรม	13	1	2552	52/4 ม.13 ถ.กรุงเทพกรีฑา แขวงสะพานสูง	บางกะปิ	กรุงเทพฯ			1											1		3
12	2547		นาย	เสวกทัวร์	13	1	2552	55 ม.9 ซ.ปรีชา แขวงฉิมพลี	ธนบุรี	กรุงเทพฯ			1							1						3
13	2547		หจก.	ไสว วรรณรังสี	24	1	2552	55/4 ม.3 ซ.ประชาอุทิศ 76 ถ.ประชาอุทิศ แขวงทุ่งครุ	กรุงเทพฯ			10							10						3
</textarea>
</td></tr></table>
<table style="background-color:black;border-spacing:5px;text-align:center;width:760px;margin-left:auto;margin-right:auto;"><tr>
<td bgcolor="white" align="center"><input type="submit" value="listall" name="action" style="width:60px;background-color:#ffdddd;">
เริ่ม <input name="begin" value="0" size="1">
จำนวน <input name="total" value="100" size="2">
</td>
<td style="background-color:white;text-align:center;">
<input type="submit" value="findname" name="action"><input name="name" value="หจก." size="3" style="width:60px;background-color:#ffddff;">
</td>
<td style="background-color:white;text-align:center;">
<input type="submit" value="deleterecord" name="action" style="width:100px;background-color:#ddddff;">
<input name="did" value="1" size="1"><input name="dyear" value="2547" size="4">
</td>
<td style="background-color:white;text-align:center;">
<input type="submit" value="deletetable" name="action" style="width:100px;background-color:red;color:white;">
</td></tr>
<?php } ?>
</table>
</form> 
<!-- Section 3 : Activity -->
<table style="background-color:gray;width:760px;margin-left:auto;margin-right:auto;">
<tr><td style="background-color:white;">
<?php
### Start of 10 Activity ###
if (isset($_POST{'action'})) {
###########################
# 1 # create table
###########################
if ($_POST{'action'} == "Create table") {
  $query = "create table $tb (";
  for ($i=1;$i<=26;$i++) $query = $query . "f" . $i . " char(100),";
  $query .= "f27 char(100)";
  $query .= ")";
  echo $query."<br/>";
  if ($connect->query($query)) echo "process : completely<br/>"; else { echo "error to create table<br/>"; exit; }
  echo "<meta http-equiv='refresh' content=\"3;URL='$file'\" />";
  exit;
}
###########################
# 2 # delete table
###########################
if ($_POST{'action'} == "deletetable") {
  $query = "drop table $tb;";
  if ($connect->query($query)) echo "process : completely<br/>"; else { echo "table remove : error<br/>$query"; exit; }
  echo "<meta http-equiv='refresh' content=\"3;URL='$file'\" />";    
  exit;
}
###########################
# 3 # search from keyword
###########################
if ($_POST{'action'} == "findname") {
  if(strlen($_POST{'name'}) > 1) {
    $query    = "select * from $tb where f4 like '". $_POST{'name'} ."%'"; 
    $result = $connect->query($query); 
    if (!$result) { echo "Finding : error<br/>$query"; exit;}
    $rows = $result->fetchAll(PDO::FETCH_OBJ); // $num_rows = count($rows);
    foreach ($rows as $object) {
      echo "<br/>";
      foreach ($object as $o) echo "$o ";
    }
  } else {
    echo '<b>Error</b> : Keyword required > 1 character';
  }
}
###########################
# 4 # delete record
###########################
if ($_POST{'action'} == "deleterecord") {
  $before = reccount("select count(*) from $tb"); 
  $query    = "delete from $tb where f1 = '". $_POST{'did'}."' and f2 = '". $_POST{'dyear'}."'";
  $result = $connect->query($query); 
  $after = reccount("select count(*) from $tb"); 
  if ($result) echo "delete : ". ($before - $after) ." records<br/>"; else { echo "delete : error<br/>$query"; }
}
###########################
# 5 # list all records
###########################
if ($_POST{'action'} == "listall") {
  // $query    = "select * from $tb limit " . $_POST{'begin'}  . "," . $_POST{'total'} ;
  $query    = "select * from $tb limit " . $_POST{'total'}  . " offset " . $_POST{'begin'} ;
  $result = $connect->query($query); 
  if (!$result) { echo "listing : error<br/>$query"; exit;}
  $rows = $result->fetchAll(PDO::FETCH_OBJ); // $num_rows = count($rows);
  echo "<ol start=" . ($_POST{'begin'} + 1) . ">";
  foreach ($rows as $object) {
    echo "<li>";
    foreach ($object as $o) echo "$o ";
  }
  echo "</ol>";  
  /* while ($object = $result->fetch_object()) { echo "<li>"; foreach ($object as $o) echo "$o "; } */
}
###########################
# 6 # post records
###########################
if ($_POST{'action'} == "postmany") {
  $getline = explode("\r\n",$_POST{'manyrecord'});
  for ($j=0;$j<count($getline);$j++) {
    $l = $j + 1;
    $getfield = explode("\t",$getline[$j]);   
    # echo "$l $getfield[0] - $getfield[26]<br/>";
  }
  if ($connect) {
    echo "post from textarea : completely<br/>";
    for ($j=0;$j<count($getline);$j++) {
      if (strlen($getline[$j]) > 1) {
        $l = $j + 1;
        $getfield = explode("\t",$getline[$j]);      
        $query = "insert into $tb values(";
        for ($i=0;$i<=25;$i++) $query = $query . "'$getfield[$i]',";
        $query = $query . "'$getfield[26]'";
        $query = $query . ");";
        echo $query."<br/>";
        $result = $connect->query($query);
      }
    }  
  } else {
    echo "connect : fail"; 
  }
}
###########################
# 7 # post 1000
###########################
if ($_POST{'action'} == "post 1000") {
  if ($connect) {
    echo "Post 1000 : completely";
    for ($j=0;$j<1000;$j++) {
      if(!function_exists("iconv")) {
        $c1 = chr(rand(65,91));
        $c2 = chr(rand(65,91));
        $c3 = chr(rand(65,91));
      } else { //phpversion()
        $c1 = iconv("TIS-620","UTF-8",chr(rand(161,206))); // 196 = ฤ ทำให้มี 45 ตัว
        $c2 = iconv("TIS-620","UTF-8",chr(rand(161,206)));
        $c3 = iconv("TIS-620","UTF-8",chr(rand(161,206)));
      }
      $query = "insert into $tb (f1,f2,f4) values( $j , $j, '" . $c1.$c2.$c3 . "');";
      $result = $connect->query($query);
    }
  } else {
    echo "connect : fail"; 
  }
}
// echo iconv("TIS-620","UTF-8",chr(161)); // output = ก
// echo ord(iconv("UTF-8","TIS-620","ก")); // output = 161
###########################
# 8 # post_recs
###########################
if ($_POST{'action'} == "post_recs") {
  if ($connect) {
    echo "Post " . $_POST{'recs'} ." : completely";
    // echo "iconv:" . function_exists("iconv"); // 000webhosting.com จะ return 1 กลับมา
    for ($j=0;$j<$_POST{'recs'};$j++) {
      if(!function_exists("iconv")) { // ป้องกันการเรียกใช้ iconv ที่ 5GBFree.com
        $c1 = chr(rand(65,91));
        $c2 = chr(rand(65,91));
        $c3 = chr(rand(65,91));
      } else {
        $c1 = iconv("TIS-620","UTF-8",chr(rand(65,91))); // ที่ 5GBFree.com หากเรียกใช้ iconv จะหยุดการทำงาน
        $c2 = iconv("TIS-620","UTF-8",chr(rand(65,91))); // ตรวจสอบการ support ของ iconv ด้วย phpinfo() ได้
        $c3 = iconv("TIS-620","UTF-8",chr(rand(65,91)));
      }
      $query = "insert into $tb (f1,f2, f4) values( $j , $j, '" . $c1.$c2.$c3 . "');";
      $result = $connect->query($query);
    }  
  } else {
    echo "connect : fail"; 
  }
}
###########################
# 9 # Total records
###########################
echo "<hr style='display:block;height:1px;border:0;border-top:1px solid#ccc;margin:1em0;padding:0;' />Total records : ". reccount("select count(*) from $tb");
$connect->close();
### Stop of 10 Activity ###
} // end of isset : action from post
?>
</td></tr></table> 
<!-- Section 4 : Introduction -->
<table style="margin-left:auto;margin-right:auto;width:760px;border-width:5px;border-style:solid;">
<tr><td style="background-color:#ddffdd;">
<fieldset><legend><b>Suggestion about this script</b></legend>
<ol>
<?php if($phpinfo_function_allow) { ?>
<li>เรียกใช้ <a href="?action=phpinfo">phpinfo()</a> เพื่อดูค่าต่าง ๆ ของ server เช่น version ของ php หรือ mysql
<?php } ?>
</ol>
</fieldset>
</td></tr>
<tr><td style="background-color:white;">
<?php
$demo = array("https://myapp630306.herokuapp.com/postgresqlworking.php");
for($demo_no=0;$demo_no<count($demo);$demo_no++) {
  echo "<b>Demo site</b> : <a href='$demo[$demo_no]'>$demo[$demo_no]</a><br/>";
}
list($u_start,$s_start) = explode(" ",$start);  
list($u_stop,$s_stop) = explode(" ",microtime());  
$tstart = $u_start + $s_start;
$tstop = $u_stop + $s_stop;
echo "Page loading in " . ($tstop - $tstart) . " seconds";
?>
</td></tr></table>
</body></html>
<?php
###########################
# 10 # Function
###########################
function reccount ($q) {
  global $connect;	
  $result = $connect->query($q);
  return $result->fetchColumn();	
}	
/* http://thaiabc.ueuo.com/ http://thaiabc.000webhostapp.com/mysqlworking.php */
?>