Guest User

Untitled

a guest
Mar 22nd, 2018
274
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.35 KB | None | 0 0
  1. <?php
  2. // parametri di configurazione MySQL RDBMS
  3. // Fixme: Leonardo 28/07/2017: Modificare parametri con quelli di unisa
  4. define('DB_HOST','localhost');
  5. define('DB_NAME','tirocini');
  6. define('DB_USER','root');
  7. define('DB_PSW','');
  8.  
  9. define('DEBUG','true');
  10. define('NOME_MAIL_SEGRETERIA','Sistema Gestione Tirocini');
  11. define('MAIL_SEGRETERIA','no-reply@unisa.it');
  12. ?>
  13.  
  14. <?php
  15. class db{
  16. var $hostName;
  17. var $dbUser;
  18. var $dbPassword;
  19. var $dbName;
  20. var $hostPort;
  21. var $dbType;
  22. private $conn;
  23. private $dbResultLine; //NOTE: Leonardo 15/12/16: Aggiunta: mi dava errore
  24.  
  25. /*
  26. *NOTE: Leonardo 30/11/16: Riscrivo il costruttore (I costruttori PHP4 sono deprecati in PHP7)
  27. * In realtà sto implementando un costruttore in PHP5 (ancora compatibili)
  28. */
  29. /* NOTE: Leonardo 23/1/16: Commentato
  30. function __construct($host,$user,$pass,$name,$port = 3306,$type = 'mysql') {
  31. $this->hostName = $host;
  32. $this->dbUser = $user;
  33. $this->dbPassword = $pass;
  34. $this->dbName = $name;
  35. $this->hostPort = $port;
  36. $this->dbType = $type;
  37. }*/
  38.  
  39. //NOTE: Leonardo 30/11/16: Scrivo questo metodo per mantenere la compatibilità con il resto del codice
  40. function db($host,$user,$pass,$name,$port = 3306,$type = 'mysql'){
  41. $this->hostName = $host;
  42. $this->dbUser = $user;
  43. $this->dbPassword = $pass;
  44. $this->dbName = $name;
  45. $this->hostPort = $port;
  46. $this->dbType = $type;
  47. /* NOTE: Leonardo 23/1/16: Commentato
  48. self::__construct($host,$user,$pass,$name,$port = 3306,$type = 'mysql');
  49. */
  50. }
  51.  
  52.  
  53. // Apre una connessione al server e restituisce un identificativo di connessione
  54. function connect(){
  55. $this->conn = @mysql_connect($this->hostName, $this->dbUser, $this->dbPassword) or die ('_CONNECTION_ERROR_');
  56. mysql_select_db($this->dbName) or die ('_CONNECTION_ERROR_'.$this->error()."<br><br>");
  57. //mysql_set_charset('utf8',$this->conn);
  58. mysql_query("SET NAMES 'UTF8'");
  59. return $this->conn;
  60. }
  61.  
  62. // effettua la query
  63. function doQuery($query){
  64. $this->dbQryResult = mysql_query($query);// or die (_QUERY_ERROR1_.$this->error()."<br><br>".$query);//or throw_ex("errore"); or die (_QUERY_ERROR_.$this->error()."<br><br>".$query);
  65. return $this->dbQryResult;
  66. }
  67.  
  68.  
  69. function fetchArray($result = ""){
  70. if ($result!="")
  71. $this->dbResultLine = @mysql_fetch_array($result);
  72. return $this->dbResultLine;
  73. }
  74.  
  75. // Restituisce una riga del risultato come un array associativo, un array numerico o entrambi.
  76. function getData($result = ""){
  77. if ($result!="") {
  78. return $this->fetchArray($result);
  79. } else return $this->fetchArray($this->dbQryResult);
  80. }
  81.  
  82. // restituisce il numero delle righe
  83. function numRows(){
  84. return @mysql_num_rows($this->dbQryResult);
  85. }
  86.  
  87. function lastId(){
  88. return @mysql_insert_id($this->conn);
  89. }
  90.  
  91. // chiude la connessione al server
  92. function close(){
  93. @mysql_close($this->conn);
  94. }
  95.  
  96. // Restituisce il messaggio dell'eventuale errore
  97. function error(){
  98. return @mysql_error($this->conn);
  99. }
  100.  
  101. function throw_ex($er){
  102. throw new Exception($er);
  103. }
  104.  
  105. /*
  106. * NOTE: Leonardo 30/11/16: Aggiungo questo getter perchè ho bisogno di questo valore per alcune funzionalità
  107. * (Potrei recuperarlo dalla connessione ma dovrei modificare molte righe di molti files)
  108. */
  109. function getConnection() {
  110. return $this->conn;
  111. }
  112.  
  113. /*
  114. * esegue una query.
  115. * Parametri:
  116. * $sql: la stringa contenente la query da eseguire
  117. * $param: il parametro che deve essere preso dalla query (per non restituire array di array)
  118. * se lasciato vuoto restituisce la query come array di array
  119. */
  120. public static function query($sql, $param = -1) {
  121. $db = new db ( DB_HOST, DB_USER, DB_PSW, DB_NAME );
  122. $db->connect ();
  123. $result = $db->doQuery ( $sql );
  124.  
  125. $return = array ();
  126. while ( $rigo = $db->getData ( $result ) ) {
  127. if($param != -1) {
  128. array_push ( $return, $rigo [$param] );
  129. } else {
  130. array_push ( $return, $rigo);
  131. }
  132. }
  133.  
  134.  
  135.  
  136. return $return;
  137. }
  138. }
  139.  
  140.  
  141. ?>
  142.  
  143. Help me.
Add Comment
Please, Sign In to add comment