Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- // parametri di configurazione MySQL RDBMS
- // Fixme: Leonardo 28/07/2017: Modificare parametri con quelli di unisa
- define('DB_HOST','localhost');
- define('DB_NAME','tirocini');
- define('DB_USER','root');
- define('DB_PSW','');
- define('DEBUG','true');
- define('NOME_MAIL_SEGRETERIA','Sistema Gestione Tirocini');
- define('MAIL_SEGRETERIA','no-reply@unisa.it');
- ?>
- <?php
- class db{
- var $hostName;
- var $dbUser;
- var $dbPassword;
- var $dbName;
- var $hostPort;
- var $dbType;
- private $conn;
- private $dbResultLine; //NOTE: Leonardo 15/12/16: Aggiunta: mi dava errore
- /*
- *NOTE: Leonardo 30/11/16: Riscrivo il costruttore (I costruttori PHP4 sono deprecati in PHP7)
- * In realtà sto implementando un costruttore in PHP5 (ancora compatibili)
- */
- /* NOTE: Leonardo 23/1/16: Commentato
- function __construct($host,$user,$pass,$name,$port = 3306,$type = 'mysql') {
- $this->hostName = $host;
- $this->dbUser = $user;
- $this->dbPassword = $pass;
- $this->dbName = $name;
- $this->hostPort = $port;
- $this->dbType = $type;
- }*/
- //NOTE: Leonardo 30/11/16: Scrivo questo metodo per mantenere la compatibilità con il resto del codice
- function db($host,$user,$pass,$name,$port = 3306,$type = 'mysql'){
- $this->hostName = $host;
- $this->dbUser = $user;
- $this->dbPassword = $pass;
- $this->dbName = $name;
- $this->hostPort = $port;
- $this->dbType = $type;
- /* NOTE: Leonardo 23/1/16: Commentato
- self::__construct($host,$user,$pass,$name,$port = 3306,$type = 'mysql');
- */
- }
- // Apre una connessione al server e restituisce un identificativo di connessione
- function connect(){
- $this->conn = @mysql_connect($this->hostName, $this->dbUser, $this->dbPassword) or die ('_CONNECTION_ERROR_');
- mysql_select_db($this->dbName) or die ('_CONNECTION_ERROR_'.$this->error()."<br><br>");
- //mysql_set_charset('utf8',$this->conn);
- mysql_query("SET NAMES 'UTF8'");
- return $this->conn;
- }
- // effettua la query
- function doQuery($query){
- $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);
- return $this->dbQryResult;
- }
- function fetchArray($result = ""){
- if ($result!="")
- $this->dbResultLine = @mysql_fetch_array($result);
- return $this->dbResultLine;
- }
- // Restituisce una riga del risultato come un array associativo, un array numerico o entrambi.
- function getData($result = ""){
- if ($result!="") {
- return $this->fetchArray($result);
- } else return $this->fetchArray($this->dbQryResult);
- }
- // restituisce il numero delle righe
- function numRows(){
- return @mysql_num_rows($this->dbQryResult);
- }
- function lastId(){
- return @mysql_insert_id($this->conn);
- }
- // chiude la connessione al server
- function close(){
- @mysql_close($this->conn);
- }
- // Restituisce il messaggio dell'eventuale errore
- function error(){
- return @mysql_error($this->conn);
- }
- function throw_ex($er){
- throw new Exception($er);
- }
- /*
- * NOTE: Leonardo 30/11/16: Aggiungo questo getter perchè ho bisogno di questo valore per alcune funzionalità
- * (Potrei recuperarlo dalla connessione ma dovrei modificare molte righe di molti files)
- */
- function getConnection() {
- return $this->conn;
- }
- /*
- * esegue una query.
- * Parametri:
- * $sql: la stringa contenente la query da eseguire
- * $param: il parametro che deve essere preso dalla query (per non restituire array di array)
- * se lasciato vuoto restituisce la query come array di array
- */
- public static function query($sql, $param = -1) {
- $db = new db ( DB_HOST, DB_USER, DB_PSW, DB_NAME );
- $db->connect ();
- $result = $db->doQuery ( $sql );
- $return = array ();
- while ( $rigo = $db->getData ( $result ) ) {
- if($param != -1) {
- array_push ( $return, $rigo [$param] );
- } else {
- array_push ( $return, $rigo);
- }
- }
- return $return;
- }
- }
- ?>
- Help me.
Add Comment
Please, Sign In to add comment