Guest User

Untitled

a guest
Oct 20th, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.41 KB | None | 0 0
  1. Contenuto File Database.class.php
  2. <?
  3. class Database{
  4. var $conn = "";
  5. var $debug = false;
  6. public function __construct( $dbhost = Conf::dbhost, $dbuser = Conf::dbuser, $dbpass = Conf::dbpass, $dbname = Conf::dbname ){
  7. if (! $this->conn = @mysql_connect( $dbhost, $dbuser, $dbpass, true ) ){
  8. session_destroy();
  9. echo "Non รจ stato possibile aprire la connessione con il database";
  10. }
  11.  
  12. mysql_select_db( $dbname, $this->conn ) or die("Selezione del DB fallita !");
  13. }
  14.  
  15. public function close(){
  16. mysql_close( $this->conn );
  17. }
  18.  
  19. public function delete( $query ){
  20. if ( !mysql_query( $query ) ) $this->showWarningDialog( "db->getRow", $query );
  21. if ( $this->debug ) $this->showDebugDialog( "db->delete", $query );
  22. return mysql_affected_rows();
  23. }
  24.  
  25. public function execute( $query ){
  26. if ( !$exec = mysql_query( $query ) ) $this->showWarningDialog( "db->getRow", $query );
  27. if ( $this->debug ) $this->showDebugDialog( "db->execute", $query );
  28. $fetch = mysql_fetch_row( $exec );
  29. return $fetch[0];
  30. }
  31.  
  32. public function fetch( $query ){
  33. $results = array();
  34. if ( !$exec = mysql_query( $query ) ) $this->showWarningDialog( "db->getRow", $query );
  35. if ( $this->debug ) $this->showDebugDialog( "db->fetch", $query );
  36. while ( $fetch = mysql_fetch_assoc( $exec ) ){
  37. $results[] = $fetch;
  38. }
  39. return $results;
  40. }
  41.  
  42. public function getRecords( $table_name, $fields, $where="1", $order_by, $order_type="asc", $from=0, $max=1000 ){
  43. if ( $where == "" ) $where = 1;
  44.  
  45. $results = array();
  46. $query = "SELECT " . $fields . " FROM " . $table_name . " WHERE " . $where . " ORDER BY " . $order_by . " " . $order_type . " LIMIT " . $from . ", " . $max;
  47. if ( !$exec = mysql_query( $query ) ) $this->showWarningDialog( "db->getRow", $query );
  48. if ( $this->debug ) $this->showDebugDialog( "db->getRecords", $query );
  49. while ( $fetch = mysql_fetch_assoc( $exec ) ){
  50. $results[] = $fetch;
  51. }
  52. return $results;
  53. }
  54.  
  55. public function getRows( $table_name, $fields, $where="1", $order_by, $order_type="asc", $from=0, $max=1000 ){
  56. $this->getRecords( $table_name, $fields, $where, $order_by, $order_type, $from, $max );
  57. }
  58.  
  59. public function getRow( $table_name, $fields, $field_id_name, $field_id_value ){
  60. $row = array();
  61. $query = "SELECT " . $fields . " FROM " . $table_name . " WHERE " . $field_id_name . " = " . $field_id_value . " LIMIT 0,1";
  62. if ( !$exec = mysql_query( $query ) ) $this->showWarningDialog( "db->getRow", $query );
  63. if ( $this->debug ) $this->showDebugDialog( "db->getRow", $query );
  64. $row = mysql_fetch_assoc( $exec );
  65. return $row;
  66. }
  67.  
  68. public function insert( $query ){
  69. if ( !mysql_query( $query ) ) $this->showWarningDialog("db->update", $query );
  70. if ( $this->debug ) $this->showDebugDialog( "db->insert", $query );
  71. return mysql_insert_id();
  72. }
  73.  
  74. public function update( $query ){
  75. if ( !mysql_query( $query ) ) $this->showWarningDialog("db->update", $query );
  76. if ( $this->debug ) $this->showDebugDialog( "db->update", $query );
  77. return mysql_affected_rows();
  78. }
  79.  
  80. public function __destruct(){
  81. $this->close();
  82. }
  83.  
  84. public function showDebugDialog( $title, $txt ){
  85. echo "
  86. <div class=\"ui-widget\" style=\"margin: 5px 0px 5px 0px;\">
  87. <div class=\"ui-state-highlight ui-corner-all\" style=\"padding: 0 .7em;\">
  88. <p>
  89. <span class=\"ui-icon ui-icon-check\" style=\"float: left; margin-right: .3em;\"></span>
  90. <strong>" . $title . "</strong><br>" . $txt . "</p>
  91. </div>
  92. </div>";
  93. }
  94.  
  95. public function showWarningDialog( $title, $txt ){
  96. echo "
  97. <div class=\"ui-widget\" style=\"margin: 5px 0px 5px 0px;\">
  98. <div class=\"ui-state-error ui-corner-all\" style=\"padding: 0 .7em;\">
  99. <p>
  100. <span class=\"ui-icon ui-icon-alert\" style=\"float: left; margin-right: .3em;\"></span>
  101. <strong>" . $title . "</strong><br>" . $txt . "</p>
  102. </div>
  103. </div>";
  104. }
  105. }
  106. ?>
  107.  
  108.  
  109.  
  110.  
  111.  
  112.  
  113.  
  114.  
  115.  
  116.  
  117.  
  118. Contenuto file inc.php
  119. <?php
  120. class Conf {
  121. const dbhost = "100.100.100.100";
  122. const dbuser = "utente";
  123. const dbpass = "password";
  124. const dbname = "nome_database";
  125. }
  126. ?>
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
  136.  
  137.  
  138. Contenuto demo.php
  139. <?
  140. require( "config.php" );
  141. require( "Database.class.php" );
  142.  
  143.  
  144. $db = new Database();
  145.  
  146. // Esempio
  147. // nome tabella "utenti"
  148. // campi tabella "id, nome, cognome"
  149.  
  150. // Prendo 10 utenti della pagina 1 (parte da 0)
  151. $utenti = $db->getRecords("utenti","id,nome,cognome","nome LIKE 'D%'","cognome","DESC",0,10);
  152. foreach( $utenti as $utente ){
  153. echo $utente["id"] . "<br>";
  154. echo $utente["nome"] . "<br>";
  155. echo $utente["cognome"] . "<br>";
  156. }
  157.  
  158. // Prendo 10 utenti della pagina 2
  159. $utenti = $db->getRecords("utenti","id,nome,cognome","nome LIKE 'D%'","cognome","DESC",1,10);
  160. foreach( $utenti as $utente ){
  161. echo $utente["id"] . "<br>";
  162. echo $utente["nome"] . "<br>";
  163. echo $utente["cognome"] . "<br>";
  164. }
  165.  
  166. // Elimnare un utente
  167. $db->delete("DELETE FROM utenti WHERE id = 101");
  168.  
  169. // Prendere i dati dell'utente con id 102
  170. $utente = getRow( "utenti", "id,nome,cognome", "id", "102" );
  171. echo $utente["id"] . "<br>";
  172. echo $utente["nome"] . "<br>";
  173. echo $utente["cognome"] . "<br>";
  174.  
  175.  
  176. // Modificare il nome e cognome di un utente
  177. $db->update( "UPDATE utenti SET nome = \"Roberto\", cognome = \"Sedia\" WHERE id = 102" );
  178.  
  179. // Inserimento di un utente
  180. $db->insert( "INSERT INTO utenti (nome,cognome) values (\"Raffaele\",\"Lanzetta\"" );
  181. ?>
Add Comment
Please, Sign In to add comment