Tutoriale PHP
  Comunitatea PHP Romania

 
Tutoriale PHP
Exemple PHP
Categorii Exemple » Exemplu

mysql wrapper


Descriere:
Un mysql wraper
Trimis de:redcom
Voturi:15636
Accesari:4005

Sintaxa:
<?php
class sql {

    var 
$dbhost;         // hostname for connection to mysql
    
var $dbuser;        // username for connection to mysql
    
var $dbpass;        // password for connecting to mysql
    
var $dbase;            // database name for connecting to mysql
    
var $sql_query;        // string that store the sql sentence
    
var $mysql_link;    // link id that store the connection to database
    
var $query_id;        // store the mysql query id
    
var $sql_result;    // store the result
    
var $query_count;   // count the number of query with this class

    
function sql($host$user$pass$db) { // constuctor define hostname, username, password and database to make a connection to mysql
        
$this->dbhost $host;
        
$this->dbuser $user;
        
$this->dbpass $pass;
        
$this->dbase $db;
        
$this->mysql_link "0";
        
$this->query_id="0";
        
$this->query_count "0";
        
$this->sql_result "";
        return 
$this->connection();
    }

    function 
connection() { // function for connecting to database
        
$this->mysql_link mysql_connect$this->dbhost$this->dbuser$this->dbpass ) or $this->errormysql_error$this->mysql_link ), $this->sql_querymysql_errno$this->mysql_link ) );
        
$select=mysql_select_db$this->dbase ) or $this->errormysql_error$this->mysql_link ), $this->sql_querymysql_errno$this->mysql_link ) );
        
        if(
$select && $this->mysql_link) {
            return 
true;
        } else {
            return 
false;
        }
    }

    function 
error$error_msg$sql_query$error_no ) { // write out the error in a user friendly format
        
$error_date date("F j, Y, g:i a");

//Heredoc Strings must be on the far left lines        
$error_page = <<<EOD
<html><head><title>mySQL database Error</title>
<style>P,BODY{ font-family:arial,sans-serif; font-size:11px; }</style></head><body>
&nbsp;<br><br><blockquote><b>There appears to be an error while trying to complete your request.</b><br>
You can try to go back and try again by clicking <a href=\"javascript&#058;history.go(-1);\">here</a>, if you
still get the error you can contact the site administrator by clicking <a href=\'mailto:haiden@asciant.net?subject=SQL+Error\'>here</a>
<br><br><b>Error Returned</b><br>
<form name=\'mysql\'><textarea rows=\"15\" cols=\"60\">mySQL query error: 
{$sql_query}

mySQL error: 
{$error_msg}
mySQL error code: 
{$error_no}
Date: 
{$error_date}</textarea></form><br>We apologise for any inconvenience</blockquote></body></html>
EOD;

echo 
$error_page;
exit();
    }

    function 
close() { // close the connection to database
        
mysql_close$this->mysql_link );
    }

    function 
query$sql_query ) { // function for makeing a query to the data base
        
$this->sql_query $sql_query;
        if (!
$this->mysql_link) { // there is not connection to the mysql server
            
$this->errormysql_error$this->mysql_link ), $this->sql_querymysql_errno$this->mysql_link ) );
        } else {
            
$this->query_id = @mysql_query$sql_query$this->mysql_link );
            if (!
$this->query_id) {
                
$this->errormysql_error$this->mysql_link ), $this->sql_querymysql_errno$this->mysql_link ) );
            }
            
$count $this->query_count;
            
$count = ($count 1);
            
$this->query_count $count;
        }
    }
    function 
free() { // function for freeing result of a SELECT
        //mysql_free_result($this->$query_id);
    
}
    function 
num_r()   { // method for returning the number of result in a sql SELECT query

        
$mysql_rows = @mysql_num_rows$this->query_id );
        if (
$mysql_rows<0) { // if we did not get any result than there is an error
            
$this->errormysql_error$this->mysql_link ), $this->sql_querymysql_errno$this->mysql_link ) );
        }
        return 
$mysql_rows;
    }

    function 
affected_r() { // method for returning the number of affected rows in a UPDATE / DELETE mysql query
        
$mysql_affected_rows mysql_affected_rows($this->mysql_link);
        return 
$mysql_affected_rows;
    }        
    
    function 
last_ins_id() { // method for returning the last inserted id in a INSERT mysql query
        
$mysql_last_inserted_id mysql_insert_id$this->mysql_link );
        return 
$mysql_last_inserted_id;
    }
    
    function 
fetch_array() { // return the result of the SELECT as an associat array (1 row) of data
        
$ret='';
        if ( 
$this->num_r() > ) {
            
$mysql_array mysql_fetch_assoc$this->query_id );
            if (!
is_array$mysql_array )) {
                
$ret=FALSE;
            } else {
                
$ret=$mysql_array;
            }
        }
        return 
$ret;
    }
    function 
fetch_all_assoc() { // return the result of the SELECT as an associative array as an array of array of data something like $array[i][\'fieldname\']
        
$ret='';
        if(
$this->num_r() >0) {
            while(
$mysql_array=@mysql_fetch_assoc($this->query_id)) {
                
$ret[]=$mysql_array;
            }
        }
        return 
$ret;
    }
    
    function 
fetch_rows() { // return 1 row of data as an  simple array for the SELECT statement
        
$ret='';
        if ( 
$this->num_r() > ) {
            
$mysql_array = @mysql_fetch_row($this->query_id);
            if (!
is_array$mysql_array )) {
                
$ret=FALSE;
            } else {
                
$ret=$mysql_array;
            }
        }
        return 
$ret;
    }
    function 
fetch_all_rows() {// return an array of arrays of result for a SELECT query
        
$ret='';
        if(
$this->num_r() > 0) {
            while(
$mysql_array = @mysql_fetch_row($this->query_id)) {
                
$ret[]=$mysql_array;            
            }
        }
        return 
$ret;
    }
    function 
fetch_obj() { // return an row of object for a SELECT query
        
$ret='';
        if(
$this->num_r() > ) {
            
$obj=@mysql_fetch_object($this->query_id);
            if(!
is_object($obj)) {
                
$ret=FALSE;
            } else {
                
$ret=$obj;
            }
        }
        return 
$ret;
    }
    function 
fetch_all_obj() { // return an array of objects for a SELECT query
        
$ret='';
        if(
$this->num_r() >0) {
            while(
$obj=@mysql_fetch_object($this->query_id)) {
                
$ret[]=$obj;
            }
        }
        return 
$ret;
    }
    function 
query_count() { // return the number of the query for an instance
        
return $this->query_count;
    }
    function 
info() { // print out the version of the server and the name of the server and the clinet name
        
printf("Host info: %s<br>\\r\\n", @mysql_get_host_info($this->mysql_link));
        
printf("Client info: %s<br>\\r\\n", @mysql_get_client_info());
        
printf("Server version: %s<br>\\r\\n", @mysql_get_server_info($this->mysql_link));
    }
// end of class
?>

Ultimele discutii in forum RSS Forum

Ultimele articole Ultimele articole

Top membri

Pirahna Pirahna
la birou
carco carco
Bucuresti
Birkoff Birkoff
Bucuresti
mihaitha mihaitha
Sibiu
Mascka Mascka
Braila
gabysolomon gabysolomon
Bacau
whooper whooper
Toronto ON
raul_ raul_
dechim dechim
Drobeta Turnu Severin
Amenthes Amenthes

Newsletter


Email:
 inscriere
 renuntare
 
 Arhiva newsletter

Site-ul lunii

blg
 Blgpunctro

Propune un site

Online

173 utilizatori online

Parteneriat



Copyright © 2001-2008 PHP Romania Add PHPRomania to Google Add PHPRomania to Del.icio.us Add PHPRomania to Stumbleupon Add PHPRomania to Yahoo! Add PHPRomania to Digg Add PHPRomania to Blink Car Finance | Repair Bad Credit | Refinance | Repair Bad Credit | Loans
Ads: Partener Way2Web Nework: gazduire web | inregistrare domenii | web design | imobiliare | web hosting
Powered by Simplis