FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


An Example to mySQL class





roeenoy
hello
i did not tell to you but i am a PHP Progammer too...

here is mysql class to help you while using mysql on php:

Code:
<?php 
class DB_Class 

var $db; 
function DB_Class($dbname, $username, $password) 

$this->db = mysql_connect ('localhost', $username, $password) 
       or die ("Unable to connect to Database Server"); 
mysql_select_db ($dbname, $this->db) 
       or die ("Could not select database"); 

function query($sql) 

$result = mysql_query ($sql, $this->db) 
       or die ("Invalid query: " . mysql_error()); 
return $result; 

function fetch($sql) 

$data = array(); 
$result = $this->query($sql); 
while($row = mysql_fetch_assoc($result)) { 
$data[] = $row; 

return $data; 

function getone($sql) 

$result = $this->query($sql); 
if(mysql_num_rows($result) == 0) 
$value = FALSE; 
else 
$value = mysql_result($result, 0); 
return $value; 


?> 


use like


Code:
<? 
$dbconnect = new DB_Class('table', 'user', 'password'); 
$query = "SELECT aaa FROM bbb WHERE $match = $search ORDER BY ccc DESC"; 
$result = $dbconnect->fetch($query); 
?>
roeenoy
hi all

in this guild i'll explain how to make a redirection in your php file

just edit this code:

Code:
<?

$mysite = "http://frihost.com"; // ENTER HERE YOUR SITE URL
header("Location: $mysite");

?>
Grimboy
Urgh... No indentation.
SystemWisdom
You should consider making a DB Abstraction Layer (DBAL) so that your code is portable to other databases..

The kind I currently use is as follows:

Base DB Class: (consider this a Virtual class, it MUST be overridden, not instantiated)
Code:

class CBaseDB
{
    // Holds DB Connection Handle
    var $m_iConnID;

    // Holds Current DB Query ID (Result Handle)
    var $m_iQueryID;

    // Holds Total Number of DB Queries During Script Execution
    var $m_iQueryCount;

    // Holds DB Error Num (If Error Occurs)
    var $m_iErrNum;

    // Holds DB Error Message (If Error Occurs)
    var $m_szErrStr;

    // Holds Result Set from DB Queries
    var $m_aResults;

   
    // Constructor
    function CBaseDB()
    {
        $this->m_iConnID     = 0;
        $this->m_iQueryID    = 0;
        $this->m_iQueryCount = 0;
        $this->m_iErrNum     = 0;
        $this->m_szErrStr    = '';
        $this->m_aResults    = array();
    }

    // Establishes a Connection to a Database
    function Connect( $aData = array() )
    {   trigger_error( 'Developer Error: Failure to Override '.__CLASS__.'::'.__FUNCTION__.'!', E_USER_WARNING );
        return false;
    }

    // Establishes a Persistent Connection to a Database
    function PConnect( $aData = array() )
    {   trigger_error( 'Developer Error: Failure to Override '.__CLASS__.'::'.__FUNCTION__.'!', E_USER_WARNING );
        return false;
    }

    // Disconnects from a Database
    function Disconnect( $aData = array() )
    {   trigger_error( 'Developer Error: Failure to Override '.__CLASS__.'::'.__FUNCTION__.'!', E_USER_WARNING );
        return;
    }

    // Selects a Database to Query From
    function SelectDB( $aData = array() )
    {   trigger_error( 'Developer Error: Failure to Override '.__CLASS__.'::'.__FUNCTION__.'!', E_USER_WARNING );
        return;
    }

    // Submits a Query to a Database
    function SubmitQuery( $aData = array() )
    {   trigger_error( 'Developer Error: Failure to Override '.__CLASS__.'::'.__FUNCTION__.'!', E_USER_WARNING );
        return;
    }

    // Submits a Query to a Database
    function SubmitDBQuery( $aData = array() )
    {   trigger_error( 'Developer Error: Failure to Override '.__CLASS__.'::'.__FUNCTION__.'!', E_USER_WARNING );
        return;
    }

    // Prepares a Statement
    function DBPrepare( $aData = array() )
    {   trigger_error( 'Developer Error: Failure to Override '.__CLASS__.'::'.__FUNCTION__.'!', E_USER_WARNING );
        return;
    }

    // Executes a Statement
    function DBExecute( $aData = array() )
    {   trigger_error( 'Developer Error: Failure to Override '.__CLASS__.'::'.__FUNCTION__.'!', E_USER_WARNING );
        return;
    }

    // Returns a Result Set from Last Query
    function FetchResult( $aData = array() )
    {   trigger_error( 'Developer Error: Failure to Override '.__CLASS__.'::'.__FUNCTION__.'!', E_USER_WARNING );
        return;
    }

    // Returns an Associative Result Set from Last Query
    function FetchAssoc( $aData = array() )
    {   trigger_error( 'Developer Error: Failure to Override '.__CLASS__.'::'.__FUNCTION__.'!', E_USER_WARNING );
        return;
    }

    // Frees the Result Set from Last Query
    function FreeResult( $aData = array() )
    {   trigger_error( 'Developer Error: Failure to Override '.__CLASS__.'::'.__FUNCTION__.'!', E_USER_WARNING );
        return;
    }

    // Submits a Query to a Database and Returns First Result Set
    function QueryFirst( $aData = array() )
    {   trigger_error( 'Developer Error: Failure to Override '.__CLASS__.'::'.__FUNCTION__.'!', E_USER_WARNING );
        return;
    }
   
    // Commits a Query to a Database
    function DBCommit( $aData = array() )
    {   trigger_error( 'Developer Error: Failure to Override '.__CLASS__.'::'.__FUNCTION__.'!', E_USER_WARNING );
        return;
    }

    // Rolls back the last Query to a Database
    function DBRollBack( $aData = array() )
    {   trigger_error( 'Developer Error: Failure to Override '.__CLASS__.'::'.__FUNCTION__.'!', E_USER_WARNING );
        return;
    }

    // Toggles Auto-Commit on a Database
    function DBAutoCommit( $aData = array() )
    {   trigger_error( 'Developer Error: Failure to Override '.__CLASS__.'::'.__FUNCTION__.'!', E_USER_WARNING );
        return;
    }

    // Sets the Current Row in a Multi-Row Result Set
    function RowPosition( $aData = array() )
    {   trigger_error( 'Developer Error: Failure to Override '.__CLASS__.'::'.__FUNCTION__.'!', E_USER_WARNING );
        return;
    }

    // Returns a Count of the Number of Rows in a Result Set
    function RowCount( $aData = array() )
    {   trigger_error( 'Developer Error: Failure to Override '.__CLASS__.'::'.__FUNCTION__.'!', E_USER_WARNING );
        return;
    }

    // Returns a Count of the Number of Rows Affected by QueryID
    function AffectedRowCount( $aData = array() )
    {   trigger_error( 'Developer Error: Failure to Override '.__CLASS__.'::'.__FUNCTION__.'!', E_USER_WARNING );
        return;
    }

    // Returns the last AutoID inserted into an AUTO_INCREMENT Field
    // Should be called immediately after Insert Query
    function LastAutoID( $aData = array() )
    {   trigger_error( 'Developer Error: Failure to Override '.__CLASS__.'::'.__FUNCTION__.'!', E_USER_WARNING );
        return;
    }

    // Returns the Error Number from the Last Database Error (If any occured)
    function ErrorNum( $aData = array() )
    {   trigger_error( 'Developer Error: Failure to Override '.__CLASS__.'::'.__FUNCTION__.'!', E_USER_WARNING );
        return;
    }

    // Returns the Error Message from the Last Database Error (If any occured)
    function ErrorDesc( $aData = array() )
    {   trigger_error( 'Developer Error: Failure to Override '.__CLASS__.'::'.__FUNCTION__.'!', E_USER_WARNING );
        return;
    }

    // Escapes Quotes in a Query String
    function EscapeQuotes( &$szRawData )
    {   trigger_error( 'Developer Error: Failure to Override '.__CLASS__.'::'.__FUNCTION__.'!', E_USER_WARNING );
        return;
    }

    // Returns a Total Count of Queries Made During Script Execution
    function GetQueryCount()
    {
        return $this->m_iQueryCount;
    }
   
    // Returns a Complete Result Set from Last Query
    function &FetchAll( $aData = array() )
    {
        $aRows = array();
        while( $oRow = $this->FetchResult( $aData ) )
            array_push( $aRows, &$oRow );
        return $aRows;
    }
   
    // Returns a Complete Associative Result Set from Last Query
    function &FetchAllAssoc( $aData = array() )
    {
        $aRows = array();
        while( $oRow = FetchAssoc( $aData ) )
            array_push( $aRows, &$oRow );
        return $aRows;
    }
}



MySQL DB Class: (Instantiate an object of this class)
Code:

class CDatabase extends CBaseDB
{
    // Constructor
    function CDatabase()
    {
        // Call Parent Constructor
        $this->CBaseDB();
    }

    // Establishes a Connection to a Database
    function Connect( $aData = array() )
    {   
        switch( count( $aData ) )
        {
            case 0:  $this->m_iConnID = mysql_connect(); break;
            case 1:  $this->m_iConnID = mysql_connect( $aData[0] ); break;
            case 2:  $this->m_iConnID = mysql_connect( $aData[0], $aData[1] ); break;
            default: $this->m_iConnID = mysql_connect( $aData[0], $aData[1], $aData[2] ); break;
        }
        return true;
    }

    // Establishes a Persistent Connection to a Database
    function PConnect( $aData = array() )
    {   
        switch( count( $aData ) )
        {
            case 0:  $this->m_iConnID = mysql_pconnect(); break;
            case 1:  $this->m_iConnID = mysql_pconnect( $aData[0] ); break;
            case 2:  $this->m_iConnID = mysql_pconnect( $aData[0], $aData[1] ); break;
            default: $this->m_iConnID = mysql_pconnect( $aData[0], $aData[1], $aData[2] ); break;
        }
        return true;
    }

    // Disconnects from a MySQL Database
    function Disconnect( $aData = array() )
    {
        return @mysql_close( $this->m_iConnID );
    }

    // Selects a Database within MySQL to Query From
    function SelectDB( $aData = array() )
    {
        return @mysql_select_db( $aData[0], $this->m_iConnID );
    }

    // Submits a Query to a MySQL Database
    function SubmitQuery( $aData = array() )
    {   global $Cfg_CountSessionQueries; // If Sessions are saved in DB..

        if( is_array( $aData ) )
        {
            $szQuery = $aData[0];
            $bSession = $aData[1];
        }else
        {
            $szQuery = $aData;
            $bSession = false;
        }
       
        if( strlen( $szQuery ) )
        {
            $this->EscapeQuotes( $szQuery );
            $this->m_iQueryID = @mysql_query( $szQuery, $this->m_iConnID );

            if( $bSession ) // Session Query?
            {   if( $Cfg_CountSessionQueries ){ $this->m_iQueryCount++; }
            }else
            {   $this->m_iQueryCount++;
            }
        }
        return $this->m_iQueryID;
    }

    // Submits a Query to a MySQL Database
    function SubmitDBQuery( $aData = array() )
    {   global $Cfg_CountSessionQueries; // If Sessions are saved in DB..

        if( !is_array( $aData ) ) return;
        if( strlen( $aData[1] ) )  // Query String
        {
            $this->EscapeQuotes( $aData[1] );
            $this->m_iQueryID = @mysql_query( $aData[0], $aData[1], $this->m_iConnID );

            if( $aData[2] ) // Session Query?
            {   if( $Cfg_CountSessionQueries ){ $this->m_iQueryCount++; }
            }else
            {   $this->m_iQueryCount++;
            }
        }
        return $this->m_iQueryID;
    }

    // Prepares a Statement
    function DBPrepare( $aData = array() )
    {   
        return true;
    }

    // Executes a Statement
    function DBExecute( $aData = array() )
    {   
        return $this->SubmitQuery( $aData );
    }

    // Returns a Result Set from Last Query
    function FetchResult( $aData = array() )
    {
        $this->m_aResults = @mysql_fetch_array( $this->m_iQueryID );
        return $this->m_aResults;
    }

    // Returns an Associative Result Set from Last Query
    function FetchAssoc( $aData = array() )
    {
        $this->m_aResults = @mysql_fetch_assoc( $this->m_iQueryID );
        return $this->m_aResults;
    }

    // Frees the Result Set from Last Query
    function FreeResult( $aData = array() )
    {
        @mysql_free_result( $this->m_iQueryID );
        return;
    }

    // Submits a Query to a MySQL Database and Returns First Result Set
    function QueryFirst( $aData = array() )
    {
        if( $this->SubmitQuery( $aData ) )
        {
            $this->m_aResults = $this->FetchResult();
            $this->FreeResult();
            return $this->m_aResults;
        }
        return false;
    }
   
    // Commits a Query to a Database
    function DBCommit( $aData = array() )
    {
        return true;
    }

    // Rolls back the last Query to a Database
    function DBRollBack( $aData = array() )
    {
        return false;
    }

    // Toggles Auto-Commit on a Database
    function DBAutoCommit( $aData = array() )
    {
        return true;
    }

    // Sets the Current Row in a Multi-Row Result Set
    function RowPosition( $iRow )
    {
        return @mysql_data_seek( $this->m_iQueryID, $iRow );
    }

    // Returns a Count of the Number of Rows in a Result Set
    function RowCount()
    {
        return @mysql_num_rows( $this->m_iQueryID );
    }

    // Returns a Count of the Number of Rows Affected by QueryID
    function AffectedRowCount()
    {
        return @mysql_affected_rows( $this->m_iQueryID );
    }

    // Returns the last AutoID inserted into an AUTO_INCREMENT Field
    // Should be called immediately after Insert Query
    function LastAutoID()
    {
        return @mysql_insert_id( $this->m_iConnID );
    }

    // Returns the Error Number from the Last MySQL Error (If any occured)
    function ErrorNum()
    {
        $this->m_iErrNum = @mysql_errno();
        return $this->m_iErrNum;
    }

    // Returns the Error Message from the Last MySQL Error (If any occured)
    function ErrorDesc()
    {
        $this->m_szErrStr = @mysql_error();
        return $this->m_szErrStr;
    }

    // Escapes Quotes in a MySQL Query String
    function EscapeQuotes( &$szRawData )
    {
        if( get_magic_quotes_gpc() ) $szRawData = stripslashes( $szRawData );

        if( version_compare( phpversion(), '4.3.0' ) == '-1' )
            mysql_escape_string( $szRawData );
        else
            mysql_real_escape_string( $szRawData, $this->m_iConnID );
        return;
    }
}


Of course, you could create an 'extended' class for any DB type you may be using, like Oracle, PostGreSQL, MS SQL, Etc... You would just create it in another file aptly named after the DB of your choice, and make sure to name the class:
Code:

class CDatabase extends CBaseDB
{
   // [...]
}


So that you only have to instantiate once like so:
Code:

$oDB = new CDatabase;


Now, to use the DBAL in your code, you would include the correct "extended" db layer (like MySQL for example), and be sure to "Execute and Commit" all of your Non-Select queries (Insert, Update, Delete). Some queries may even be "Prepared" first, so be sure to use that function as well, or your code may break when switching DBs!

Hope that helps, enjoy!
imc
roeenoy wrote:
hi all

in this guild i'll explain how to make a redirection in your php file

just edit this code:

Code:
<?

$mysite = "http://frihost.com"; // ENTER HERE YOUR SITE URL
header("Location: $mysite");

?>


My friend can you please explain it to me, because i dont know what to do with it , and can you please explain how it works
Related topics
my advance guestbook doesnt work..
phpBB "integration"
OCD (Obsessive Compulsive Disorder)
A question for all you monotheists.
Tables created in the database but problem in access
JAVA queries
Knight of the Force Preview
mySQL class with Debugger
Getting host to connect to my local mysql?
Object-oriented programming Vs procedural
how to connect php with mysql?
Why is creating a PHP class useful?
[TuT]JOOMLA Installation Guide (Less Easy Way)
What is easier for the moderators?
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.