Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Contenuto File Database.class.php
- <?
- class Database{
- var $conn = "";
- var $debug = false;
- public function __construct( $dbhost = Conf::dbhost, $dbuser = Conf::dbuser, $dbpass = Conf::dbpass, $dbname = Conf::dbname ){
- if (! $this->conn = @mysql_connect( $dbhost, $dbuser, $dbpass, true ) ){
- session_destroy();
- echo "Non รจ stato possibile aprire la connessione con il database";
- }
- mysql_select_db( $dbname, $this->conn ) or die("Selezione del DB fallita !");
- }
- public function close(){
- mysql_close( $this->conn );
- }
- public function delete( $query ){
- if ( !mysql_query( $query ) ) $this->showWarningDialog( "db->getRow", $query );
- if ( $this->debug ) $this->showDebugDialog( "db->delete", $query );
- return mysql_affected_rows();
- }
- public function execute( $query ){
- if ( !$exec = mysql_query( $query ) ) $this->showWarningDialog( "db->getRow", $query );
- if ( $this->debug ) $this->showDebugDialog( "db->execute", $query );
- $fetch = mysql_fetch_row( $exec );
- return $fetch[0];
- }
- public function fetch( $query ){
- $results = array();
- if ( !$exec = mysql_query( $query ) ) $this->showWarningDialog( "db->getRow", $query );
- if ( $this->debug ) $this->showDebugDialog( "db->fetch", $query );
- while ( $fetch = mysql_fetch_assoc( $exec ) ){
- $results[] = $fetch;
- }
- return $results;
- }
- public function getRecords( $table_name, $fields, $where="1", $order_by, $order_type="asc", $from=0, $max=1000 ){
- if ( $where == "" ) $where = 1;
- $results = array();
- $query = "SELECT " . $fields . " FROM " . $table_name . " WHERE " . $where . " ORDER BY " . $order_by . " " . $order_type . " LIMIT " . $from . ", " . $max;
- if ( !$exec = mysql_query( $query ) ) $this->showWarningDialog( "db->getRow", $query );
- if ( $this->debug ) $this->showDebugDialog( "db->getRecords", $query );
- while ( $fetch = mysql_fetch_assoc( $exec ) ){
- $results[] = $fetch;
- }
- return $results;
- }
- public function getRows( $table_name, $fields, $where="1", $order_by, $order_type="asc", $from=0, $max=1000 ){
- $this->getRecords( $table_name, $fields, $where, $order_by, $order_type, $from, $max );
- }
- public function getRow( $table_name, $fields, $field_id_name, $field_id_value ){
- $row = array();
- $query = "SELECT " . $fields . " FROM " . $table_name . " WHERE " . $field_id_name . " = " . $field_id_value . " LIMIT 0,1";
- if ( !$exec = mysql_query( $query ) ) $this->showWarningDialog( "db->getRow", $query );
- if ( $this->debug ) $this->showDebugDialog( "db->getRow", $query );
- $row = mysql_fetch_assoc( $exec );
- return $row;
- }
- public function insert( $query ){
- if ( !mysql_query( $query ) ) $this->showWarningDialog("db->update", $query );
- if ( $this->debug ) $this->showDebugDialog( "db->insert", $query );
- return mysql_insert_id();
- }
- public function update( $query ){
- if ( !mysql_query( $query ) ) $this->showWarningDialog("db->update", $query );
- if ( $this->debug ) $this->showDebugDialog( "db->update", $query );
- return mysql_affected_rows();
- }
- public function __destruct(){
- $this->close();
- }
- public function showDebugDialog( $title, $txt ){
- echo "
- <div class=\"ui-widget\" style=\"margin: 5px 0px 5px 0px;\">
- <div class=\"ui-state-highlight ui-corner-all\" style=\"padding: 0 .7em;\">
- <p>
- <span class=\"ui-icon ui-icon-check\" style=\"float: left; margin-right: .3em;\"></span>
- <strong>" . $title . "</strong><br>" . $txt . "</p>
- </div>
- </div>";
- }
- public function showWarningDialog( $title, $txt ){
- echo "
- <div class=\"ui-widget\" style=\"margin: 5px 0px 5px 0px;\">
- <div class=\"ui-state-error ui-corner-all\" style=\"padding: 0 .7em;\">
- <p>
- <span class=\"ui-icon ui-icon-alert\" style=\"float: left; margin-right: .3em;\"></span>
- <strong>" . $title . "</strong><br>" . $txt . "</p>
- </div>
- </div>";
- }
- }
- ?>
- Contenuto file inc.php
- <?php
- class Conf {
- const dbhost = "100.100.100.100";
- const dbuser = "utente";
- const dbpass = "password";
- const dbname = "nome_database";
- }
- ?>
- Contenuto demo.php
- <?
- require( "config.php" );
- require( "Database.class.php" );
- $db = new Database();
- // Esempio
- // nome tabella "utenti"
- // campi tabella "id, nome, cognome"
- // Prendo 10 utenti della pagina 1 (parte da 0)
- $utenti = $db->getRecords("utenti","id,nome,cognome","nome LIKE 'D%'","cognome","DESC",0,10);
- foreach( $utenti as $utente ){
- echo $utente["id"] . "<br>";
- echo $utente["nome"] . "<br>";
- echo $utente["cognome"] . "<br>";
- }
- // Prendo 10 utenti della pagina 2
- $utenti = $db->getRecords("utenti","id,nome,cognome","nome LIKE 'D%'","cognome","DESC",1,10);
- foreach( $utenti as $utente ){
- echo $utente["id"] . "<br>";
- echo $utente["nome"] . "<br>";
- echo $utente["cognome"] . "<br>";
- }
- // Elimnare un utente
- $db->delete("DELETE FROM utenti WHERE id = 101");
- // Prendere i dati dell'utente con id 102
- $utente = getRow( "utenti", "id,nome,cognome", "id", "102" );
- echo $utente["id"] . "<br>";
- echo $utente["nome"] . "<br>";
- echo $utente["cognome"] . "<br>";
- // Modificare il nome e cognome di un utente
- $db->update( "UPDATE utenti SET nome = \"Roberto\", cognome = \"Sedia\" WHERE id = 102" );
- // Inserimento di un utente
- $db->insert( "INSERT INTO utenti (nome,cognome) values (\"Raffaele\",\"Lanzetta\"" );
- ?>
Add Comment
Please, Sign In to add comment