Advertisement
Guest User

Untitled

a guest
Sep 17th, 2017
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.08 KB | None | 0 0
  1. <?php
  2.  
  3.  
  4.  
  5.     $db_connection=null;
  6.  
  7.     //PREDEFINED QUERIES
  8.  
  9.     $ADD_NEW_FILE_QUERY="INSERT INTO orario (post_date,data,author,digest) VALUES (:date,':data',':author',':digest')";
  10.  
  11.     $UPDATE_FILE_BY_ID_QUERY="UPDATE orario SET post_date=:date, data=':data', author=':author', digest=':digest' WHERE id = :id";
  12.  
  13.     $GET_LAST_FILE_ID_QUERY="SELECT id FROM orario WHERE post_date = (SELECT MAX(post_date) FROM orario)";
  14.  
  15.     $GET_FILE_CONTENT_BY_ID_QUERY="SELECT data FROM orario WHERE id = :id";
  16.  
  17.     $GET_FILE_DIGEST_BY_ID_QUERY="SELECT digest FROM orario WHERE id = :id";
  18.  
  19.     $GET_FILES_ID_QUERY="SELECT id FROM orario";
  20.  
  21.     $COUNT_FILES_QUERY="SELECT COUNT(*) FROM orario";
  22.  
  23.     //PREDEFINED QUERIES
  24.  
  25.    
  26.  
  27.     function db_connect(){
  28.  
  29.         $host="localhost";
  30.  
  31.         $db_name="my_orarioospedale";
  32.  
  33.         $username="orarioospedale";
  34.  
  35.         $password="sektenufku31";
  36.  
  37.        
  38.  
  39.         global $db_connection;
  40.  
  41.         $db_connection=mysql_connect($host,$username,$password,$db_name);
  42.  
  43.        
  44.  
  45.         if(!$db_connection){
  46.  
  47.             return false;
  48.  
  49.         }
  50.  
  51.         mysql_select_db($db_name);
  52.  
  53.         return true;
  54.  
  55.     }
  56.  
  57.    
  58.  
  59.     function db_disconnect(){
  60.  
  61.         global $db_connection;
  62.  
  63.         mysql_close($db_connection);
  64.  
  65.     }
  66.  
  67.    
  68.  
  69.     function db_add_new_file($filename,$author="Anonimo"){
  70.  
  71.         global $ADD_NEW_FILE_QUERY;
  72.  
  73.        
  74.  
  75.         $fileHandle = fopen($filename, "r");
  76.  
  77.        
  78.  
  79.         $fileContent = fread($fileHandle,filesize($filename));
  80.  
  81.         $md5=md5($fileContent);
  82.  
  83.         $fileContent = addslashes($fileContent);
  84.  
  85.         $query_str=str_replace(array(":date",":author",":data",":digest"),array("NOW()",$author,$fileContent,$md5),$ADD_NEW_FILE_QUERY);
  86.  
  87.  
  88.  
  89.         mysql_query($query_str);
  90.  
  91.  
  92.  
  93.         return true;
  94.  
  95.     }
  96.  
  97.    
  98.  
  99.     function db_update_current_file($filename,$author="Anonimo"){
  100.  
  101.         global $UPDATE_FILE_BY_ID_QUERY,$GET_LAST_FILE_ID_QUERY;
  102.  
  103.         $fileHandle = fopen($filename, "r");
  104.  
  105.        
  106.  
  107.         $fileContent = fread($fileHandle,filesize($filename));
  108.  
  109.         $md5=md5($fileContent);
  110.  
  111.         $fileContent = addslashes($fileContent);
  112.  
  113.        
  114.  
  115.         $result=mysql_query($GET_LAST_FILE_ID_QUERY);
  116.  
  117.        
  118.  
  119.         if(!$result){
  120.  
  121.             return false;
  122.  
  123.         }
  124.  
  125.        
  126.  
  127.         $row=mysql_fetch_row($result);
  128.  
  129.        
  130.  
  131.         $last_id=$row[0];
  132.  
  133.        
  134.  
  135.         $query_str=str_replace(array(":date",":author",":data",":id",":digest"),array("NOW()",$author,$fileContent,$last_id,$md5),$UPDATE_FILE_BY_ID_QUERY);
  136.  
  137.         $result=mysql_query($query_str);
  138.  
  139.         if(!$result){
  140.  
  141.             return false;
  142.  
  143.         }
  144.  
  145.        
  146.  
  147.         return true;
  148.  
  149.     }
  150.  
  151.    
  152.  
  153.     function db_get_number_of_files(){
  154.  
  155.         global $COUNT_FILES_QUERY;     
  156.  
  157.         $result = mysql_query($COUNT_FILES_QUERY);
  158.  
  159.         if(!$result)
  160.  
  161.             return false;
  162.  
  163.            
  164.  
  165.         $row = mysql_fetch_row($result);
  166.  
  167.         return $row[0];
  168.  
  169.     }
  170.  
  171.    
  172.  
  173.     function db_get_last_file_content(){
  174.  
  175.         global $GET_LAST_FILE_ID_QUERY;
  176.  
  177.        
  178.  
  179.         $result = mysql_query($GET_LAST_FILE_ID_QUERY);
  180.  
  181.         if(!$result)
  182.  
  183.             return false;
  184.  
  185.        
  186.  
  187.         $row = mysql_fetch_row($result);
  188.  
  189.         return db_get_file_content_by_id($row[0]);
  190.  
  191.     }
  192.  
  193.    
  194.  
  195.     function db_get_file_digest_by_id($id){
  196.  
  197.         global $GET_FILE_DIGEST_BY_ID_QUERY;
  198.  
  199.        
  200.  
  201.         $query_str=str_replace(array(":id"),array($id),$GET_FILE_DIGEST_BY_ID_QUERY);
  202.  
  203.        
  204.  
  205.         $result = mysql_query($query_str);
  206.  
  207.         if(!$result)
  208.  
  209.             return false;
  210.  
  211.            
  212.  
  213.         $row=mysql_fetch_row($result);
  214.  
  215.         return $row[0];
  216.  
  217.     }
  218.  
  219.    
  220.  
  221.     function db_get_last_file_digest(){
  222.  
  223.         global $GET_LAST_FILE_ID_QUERY;
  224.  
  225.        
  226.  
  227.         $result = mysql_query($GET_LAST_FILE_ID_QUERY);
  228.  
  229.         if(!$result)
  230.  
  231.             return false;
  232.  
  233.        
  234.  
  235.         $row = mysql_fetch_row($result);
  236.  
  237.         return db_get_file_digest_by_id($row[0]);
  238.  
  239.     }
  240.  
  241.    
  242.  
  243.     function db_get_files_id(){
  244.  
  245.         global $GET_FILES_ID_QUERY;
  246.  
  247.        
  248.  
  249.         $result = mysql_query($GET_FILES_ID_QUERY);
  250.  
  251.         if(!$result)
  252.  
  253.             return false;
  254.  
  255.         $ids=array();
  256.  
  257.        
  258.  
  259.         while($row=mysql_fetch_row($result)){
  260.  
  261.             $ids[]=$row[0];
  262.  
  263.         }
  264.  
  265.         return $ids;
  266.  
  267.     }
  268.  
  269.    
  270.  
  271.     function db_get_file_content_by_id($id){
  272.  
  273.         global $GET_FILE_CONTENT_BY_ID_QUERY;
  274.  
  275.        
  276.  
  277.         $query_str=str_replace(array(":id"),array($id),$GET_FILE_CONTENT_BY_ID_QUERY);
  278.  
  279.        
  280.  
  281.         $result = mysql_query($query_str);
  282.  
  283.         if(!$result)
  284.  
  285.             return false;
  286.  
  287.            
  288.  
  289.         $row=mysql_fetch_row($result);
  290.  
  291.         return $row[0];
  292.  
  293.     }
  294.  
  295. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement