Advertisement
GWibisono

db.php

Dec 13th, 2013
214
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 8.36 KB | None | 0 0
  1. <?php
  2.     // connect to the database
  3.     if ($is_server) {
  4.         $dbHost = "10.100.3.12";
  5.         $dbUser = "bayiku";
  6.         $dbPass = "b@y!ku";
  7.         $dbName = "bayiku";
  8.     } else {
  9.         $dbHost = "localhost";
  10.         $dbUser = "root";
  11.         $dbPass = "";
  12.         $dbName = "inibayik_bayi";
  13.     }
  14.  
  15. /*Tambahan*/
  16.     $db = new PDO('mysql:host='.$dbHost.';dbname='.$dbName.';charset=utf8', $dbUser, $dbPass);
  17.     $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  18.     $db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
  19. /*end Tambahan*/
  20.  
  21.     $dbConn = mysql_connect ($dbHost, $dbUser, $dbPass);
  22.     if (!$dbConn) {
  23.         common_message_error('Could not connect: ' . mysql_error());
  24.     }
  25.    
  26.     $dbSelected = mysql_select_db($dbName, $dbConn);
  27.     if (!$dbSelected) {
  28.         common_message_error ('Can\'t use $dbName : ' . mysql_error());
  29.     }
  30.    
  31.     function dbQuery ($sql) {
  32.         $sql = trim ($sql);    
  33.         $res = mysql_query($sql);
  34.         if (!$res) {
  35.             $errMessage  = 'Invalid query: ' . mysql_error() . "\n";
  36.             $errMessage .= 'Whole query: ' . $sql;
  37.             common_message_error($errMessage);
  38.         }  
  39.         return $res;
  40.     }
  41.  
  42.     function dbInsertLastModification($sql, $tablename, $form="", $remind_datetime="", $user_id_view="") {
  43.         global $global_user_id;
  44.         $tmp_arr = explode(" ",$sql);
  45.         $action = strtolower($tmp_arr[0]);
  46.  
  47.         $new_sql = "SELECT MAX(id) as last_id FROM last_modification_table;";
  48.         $row = dbQueryAndFetch($new_sql);
  49.         $last_id = $row["last_id"]+1;
  50.  
  51.         if ($remind_datetime=="") $remind_datetime="NOW()";
  52.  
  53.         if (($tablename!="token") && ($tablename!="customer_token")) {
  54.             if (!$user_id_view) $user_id_view = $global_user_id;
  55.             $sql = "insert into last_modification_table (id, table_name, `sql`, form, user_id, user_id_view, `action`, update_at, remind_datetime)
  56.                        values ('".$last_id."','".$tablename."','".mysql_real_escape_string($sql)."','".$form."','".$global_user_id."','".$user_id_view."','".$action."',NOW(),".$remind_datetime.")";
  57.             $res2 = mysql_query($sql);
  58.  
  59.             if (!$res2) {
  60.                 $errMessage  = 'Invalid query: ' . mysql_error() . "\n";
  61.                 $errMessage .= 'Whole query: ' . $sql;
  62.                 common_message_error($errMessage);
  63.             }
  64.         }
  65.     }
  66.  
  67.     function dbExecute($sql,$tablename, $form="", $remind_datetime="", $user_id_view="") {
  68.         global $global_user_id;
  69.         $sql = trim ($sql);
  70.         $res = mysql_query($sql);
  71.         if ($res) {
  72.             dbInsertLastModification($sql,$tablename,$form,$remind_datetime,$user_id_view);
  73.         } else {
  74.             $errMessage  = 'Invalid query: ' . mysql_error() . "\n";
  75.             $errMessage .= 'Whole query: ' . $sql;
  76.             common_message_error($errMessage);
  77.         }
  78.         return $res;
  79.     }
  80.  
  81.     function dbExecuteArray($table, $data, $tablename="", $form="", $remind_datetime="", $exclude = array()) {
  82.         $fields = $values = array();
  83.         if( !is_array($exclude) ) $exclude = array($exclude);
  84.         foreach( array_keys($data) as $key ) {
  85.             if( !in_array($key, $exclude) ) {
  86.                 $fields[] = "`$key`";
  87.                 $values[] = "'" . mysql_real_escape_string($data[$key]) . "'";
  88.             }
  89.         }
  90.         $fields = implode(",", $fields);
  91.         $values = implode(",", $values);
  92.  
  93.         $sql = "INSERT INTO `$table` ($fields) VALUES ($values)";
  94.         $res = mysql_query($sql);
  95.         if($res) {
  96.             if($tablename != '') {
  97.                 $tmp_arr = explode(" ",$sql);
  98.                 $action = strtolower($tmp_arr[0]);
  99.    
  100.                 $new_sql = "SELECT MAX(id) as last_id FROM last_modification_table;";
  101.                 $row = dbQueryAndFetch($new_sql);
  102.                 $last_id = $row["last_id"]+1;
  103.    
  104.                 if ($remind_datetime=="") $remind_datetime="NOW()";
  105.                 $sql = "insert into last_modification_table (id, table_name, `sql`, form, `action`, update_at, remind_datetime)
  106.                             values ('".$last_id."','".$tablename."','".mysql_real_escape_string($sql)."','".$form."','".$action."',NOW(),".$remind_datetime.")";
  107.                 $res2 = mysql_query($sql);
  108.    
  109.                 if (!$res2) {
  110.                     $errMessage  = 'Invalid query: ' . mysql_error() . "\n";
  111.                     echo $errMessage .= 'Whole query: ' . $sql;
  112.                     common_message_error($errMessage);
  113.                 }
  114.             }
  115.         } else {
  116.             $errMessage  = 'Invalid query: ' . mysql_error() . "\n";
  117.             echo $errMessage .= 'Whole query: ' . $sql;
  118.             common_message_error($errMessage);
  119.         }
  120.         return $res;
  121.     }
  122.  
  123. function dbExecuteUpdateArray($table, $data, $id_field, $id_value, $tablename="", $form="", $remind_datetime="") {
  124.     foreach ($data as $field=>$value) {
  125.         $fields[] = sprintf("`%s` = '%s'", $field, mysql_real_escape_string($value));
  126.     }
  127.     $field_list = join(',', $fields);
  128.     $sql = sprintf("UPDATE `%s` SET %s WHERE `%s` = %s", $table, $field_list, $id_field, $id_value);
  129.     $res = mysql_query($sql);
  130.     if($res) {
  131.         if($tablename != '') {
  132.             $tmp_arr = explode(" ",$sql);
  133.             $action = strtolower($tmp_arr[0]);
  134.  
  135.             $new_sql = "SELECT MAX(id) as last_id FROM last_modification_table;";
  136.             $row = dbQueryAndFetch($new_sql);
  137.             $last_id = $row["last_id"]+1;
  138.  
  139.             if ($remind_datetime=="") $remind_datetime="NOW()";
  140.             $sql = "insert into last_modification_table (id, table_name, `sql`, form, `action`, update_at, remind_datetime)
  141.                             values ('".$last_id."','".$tablename."','".mysql_real_escape_string($sql)."','".$form."','".$action."',NOW(),".$remind_datetime.")";
  142.             $res2 = mysql_query($sql);
  143.  
  144.             if (!$res2) {
  145.                 $errMessage  = 'Invalid query: ' . mysql_error() . "\n";
  146.                 echo $errMessage .= 'Whole query: ' . $sql;
  147.                 common_message_error($errMessage);
  148.             }
  149.         }
  150.     } else {
  151.         $errMessage  = 'Invalid query: ' . mysql_error() . "\n";
  152.         echo $errMessage .= 'Whole query: ' . $sql;
  153.         common_message_error($errMessage);
  154.     }
  155.     return $res;
  156. }
  157.    
  158.     function dbFetch($res) {
  159.         return mysql_fetch_array($res);
  160.     }
  161.    
  162.     function dbQueryAndFetch($sql) {       
  163.         $res = dbQuery($sql);      
  164.         if ($res) return dbFetch($res);
  165.     }
  166.    
  167.     /* get number of field from query result */
  168.     function dbNumField($res) {
  169.         return mysql_num_fields($res);
  170.     }
  171.    
  172.     /* get Field Name from query result */
  173.     function dbFieldName($res, $idx) {
  174.         return mysql_field_name($res, $idx);
  175.     }
  176.  
  177.     function dbGetLastID($tablename) {
  178.         $sql = "DESC ".$tablename;
  179.         $res = dbQuery($sql);
  180.         while ($row=dbFetch($res)) {
  181.             if ($row["Key"]=="PRI") { $field_name = $row["Field"]; break; }
  182.         }
  183.  
  184.         $sql = sprintf("SELECT MAX(%s) from %s",$field_name,$tablename);
  185.         $row = dbQueryAndFetch($sql);
  186.         return $row[0];
  187.     }
  188.  
  189.     function dbGetNewID($tablename) {
  190.         $new_id = dbGetLastID($tablename) + 1;
  191.         return $new_id;
  192.     }
  193.     function dbQueryAndFetchAll($sql) {
  194.         $res = dbQuery($sql);
  195.         $arr = array();
  196.         while($row = dbFetch($res)) {
  197.             $arr[] = $row;
  198.         }
  199.         return $arr;
  200.     }
  201.     function dbNumRows($sql) {
  202.         return count(dbQueryAndFetchAll($sql));
  203.     }
  204.    
  205.     function dbExecuteOnline ($sql, $tablename) {
  206.  
  207.         $param = array (
  208.             "username" => "xxx",
  209.             "password" => "7841",
  210.             "sql" => $sql
  211.         );
  212.  
  213.         $param_encrypted = encrypt_text(serialize($param));
  214.  
  215.         $curlstring = sprintf("param=%s"
  216.             , $param_encrypted
  217.         );
  218.  
  219.         $url = 'http://www.inibayiku.com/newxfiles/dbexecute/dbexecuteonline.php';
  220.         $curlHandle = curl_init();
  221.         curl_setopt($curlHandle, CURLOPT_URL, $url);
  222.         curl_setopt($curlHandle, CURLOPT_POSTFIELDS, $curlstring);
  223.         curl_setopt($curlHandle, CURLOPT_HEADER, 0);
  224.         //curl_setopt($curlHandle, CURLOPT_HTTPHEADER,$headers);
  225.         curl_setopt($curlHandle, CURLOPT_TIMEOUT,15);
  226.         curl_setopt($curlHandle, CURLOPT_POST, 1);
  227.         curl_setopt($curlHandle, CURLOPT_RETURNTRANSFER, 1);
  228.         curl_setopt($curlHandle,CURLOPT_SSL_VERIFYPEER,false);
  229.  
  230.  
  231.             $output = curl_exec($curlHandle);
  232.        
  233.         curl_close($curlHandle);
  234.         return $output;
  235.    
  236.     }
  237.    
  238. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement