home *** CD-ROM | disk | FTP | other *** search
/ H4CK3R 4 / hacker04 / 04_HACK04.ISO / src / PHP / mysqldbclass.php3.txt < prev    next >
Encoding:
Text File  |  2002-05-06  |  11.8 KB  |  418 lines

  1. MySQL Database class 
  2.  
  3. This is a simple class which eases the use of the mysql connections. Should also work with version 3.0.5 and above of php. 
  4.  
  5.  
  6.  
  7. <?php 
  8.  
  9. /****************************************************************************** 
  10. Copyright   : living source gmbh, 1998  http://www.living-source.de 
  11. Author(en)  : Adi Sieker 
  12. Date    : 1999-05-15 
  13. Version     : 1.00 
  14. Datei    : database.inc.php3 
  15. ******************************************************************************/ 
  16.  
  17.  
  18.  
  19.  
  20.  
  21.  
  22.  
  23. /****************************************************************************** 
  24.  
  25. Database Connection Class 
  26. ************** 
  27.  
  28. What it's for: 
  29. This Class Opens a Database Connection. It's useful if you need to make more 
  30. than 1 SQL Statement in a single PHP Script. 
  31. Use it with ReUseConn in the MySQLDatabase class. 
  32.  
  33. ******************************************************************************/ 
  34. class DBConnection 
  35.    var $iConnectId; 
  36.  
  37.        /****************************************************************************** 
  38.  
  39.       Function: Open 
  40.       ************** 
  41.       Parameters: 
  42.          $szDatabase == Name of the Database to connect to. 
  43.          $szHost     == Name of the Host on which szDatabase resides. Usually localhost 
  44.          $szUser     == Name of User to connect as. 
  45.          $szPassword == Password of the User. 
  46.  
  47.       Return: 
  48.          Return true if everything went well else it will return false. 
  49.  
  50.  
  51.       What it's for: 
  52.       This function actually opens the Database Connection. 
  53.  
  54.       ******************************************************************************/ 
  55.       function Open( $szDatabase, $szHost, $szUser, $szPassword ) 
  56.       { 
  57.          $this->iConnectId = false; 
  58.  
  59.          $iRet = true; 
  60.  
  61.          $this->iConnectId = mysql_connect( $szHost , $szUser, $szPassword ); 
  62.  
  63.          if( $this->iConnectId ) 
  64.             $iRet = mysql_select_db( $szDatabase, $this->iConnectId ); 
  65.          else 
  66.             $iRet = false; 
  67.  
  68.          return $iRet; 
  69.           /*echo "DBConnection";*/ 
  70.       } 
  71.  
  72.        /****************************************************************************** 
  73.  
  74.       Function: Connection 
  75.       ************** 
  76.       Parameters: 
  77.          None 
  78.  
  79.       Return: 
  80.          Returns the ConnectionID of the current Databae connection if a 
  81.          Database COnnection has been established else it will return false. 
  82.  
  83.  
  84.       What it's for: 
  85.          This function retruns the Database ConnectionID. Use can pass this 
  86.          functions return Value to the ReUseConn function of the MySQL Database Class. 
  87.       ******************************************************************************/ 
  88.       function Connection() 
  89.       { 
  90.          return $this->iConnectId; 
  91.       } 
  92.  
  93.  
  94.  
  95.  
  96.  
  97. /****************************************************************************** 
  98.  
  99. MySQL Database Class 
  100. ************** 
  101.  
  102. What it's for: 
  103. This class is used to Communicate with a mySQL Database Server. 
  104. ******************************************************************************/ 
  105. class MySQLDatabase 
  106.     // Class Variables 
  107.    var $iResultId; 
  108.    var $szSQLString; 
  109.    var $iNumRows; 
  110.    var $iCurrentRow; 
  111.    var $arRow; 
  112.  
  113.  
  114.        /****************************************************************************** 
  115.  
  116.       Function: Open 
  117.       ************** 
  118.       Parameters: 
  119.          $szDatabase == Name of the Database to connect to. 
  120.          $szHost     == Name of the Host on which szDatabase resides. Usually localhost 
  121.          $szUser     == Name of User to connect as. 
  122.          $szPassword == Password of the User. 
  123.  
  124.       Return: 
  125.          Return true if everything went well else it will return false. 
  126.  
  127.  
  128.       What it's for: 
  129.       This function actually opens the Database Connection. 
  130.  
  131.       ******************************************************************************/ 
  132.       function Open( $szDatabase, $szHost, $szUser, $szPassword ) 
  133.       { 
  134.          $this->iConnectId = false; 
  135.          $this->szSQLString =  ""; 
  136.          $this->iResultId = false; 
  137.          $this->iNumRows = false; 
  138.          $this->arRow = false; 
  139.          $this->iCurrentRow = 0; 
  140.  
  141.          $iRet = true; 
  142.  
  143.          $this->iConnectId = mysql_connect( $szHost , $szUser, $szPassword ); 
  144.  
  145.          if( $this->iConnectId ) 
  146.          {  $iRet = mysql_select_db( $szDatabase, $this->iConnectId ); 
  147.             if( !$iRet ) 
  148.                echo  "Selected Database doesn't exist!!!"; 
  149.          } 
  150.          else 
  151.          {  echo  "Connect didn't work out!!!"; 
  152.             $iRet = false; 
  153.          } 
  154.  
  155.          return $iRet; 
  156.       } 
  157.  
  158.        /****************************************************************************** 
  159.  
  160.       Function: ReUseConn 
  161.       ************** 
  162.       Parameters: 
  163.          $iDBConnection == A valid ConnectionID to a mySQL Database. 
  164.  
  165.       Return: 
  166.          None 
  167.  
  168.  
  169.       What it's for: 
  170.          This function reuses an already established Database connection. 
  171.  
  172.       ******************************************************************************/ 
  173.       function ReUseConn( $iDBConnection ) 
  174.       { 
  175.          $this->iConnectId = false; 
  176.          $this->szSQLString =  ""; 
  177.          $this->iResultId = false; 
  178.          $this->iNumRows = false; 
  179.          $this->arRow = false; 
  180.          $this->iCurrentRow = 0; 
  181.  
  182.          $this->iConnectId = $iDBConnection; 
  183.       } 
  184.  
  185.        /****************************************************************************** 
  186.  
  187.       Function: Query 
  188.       ************** 
  189.       Parameters: 
  190.          $szSelect == This parameter is a little misnamed. It should probably be called 
  191.                       $szQueryString 
  192.  
  193.       Return: 
  194.          true on ok. 
  195.          false on error. 
  196.  
  197.  
  198.  
  199.       What it's for: 
  200.          This function will actually Query the Database and set or reset some internal 
  201.          Variables which are needed. 
  202.  
  203.       ******************************************************************************/ 
  204.       function Query( $szSelect ) 
  205.       { 
  206.          $this->szSQLString =  ""; 
  207.          $this->szSQLString = $szSelect; 
  208.          $this->iResultId = false; 
  209.          $this->iNumRows = false; 
  210.          $this->arRow = false; 
  211.          $this->iCurrentRow = 0; 
  212.  
  213.          $iRet = true; 
  214.  
  215.          if( $this->iConnectId != 0) 
  216.          { 
  217.             if( strlen( $this->szSQLString ) > 0 ) 
  218.             { 
  219.                $this->iResultId = mysql_query( $this->szSQLString, $this->iConnectId ); 
  220.  
  221.                if( $this->iResultId ) 
  222.                { 
  223.                   $tok = strtok($this->szSQLString, " "); 
  224.                   $tok = strtoupper( $tok ); 
  225.  
  226.                   if( !strcmp( $tok,  "SELECT" ) ) 
  227.                      $this->iNumRows = mysql_num_rows( $this->iResultId ); 
  228.                   else 
  229.                      $this->iNumRows = mysql_affected_rows( $this->iResultId ); 
  230.                } 
  231.                else 
  232.                { 
  233.                    //echo "Error no ResultIndex!!!"; 
  234.                   $iRet = false; 
  235.                } 
  236.             } 
  237.             else 
  238.             { 
  239.                echo  "Open called without any SQL Query!!!"; 
  240.                $iRet = false; 
  241.             } 
  242.          } 
  243.  
  244.          return $iRet; 
  245.       } 
  246.  
  247.        /****************************************************************************** 
  248.  
  249.       Function: NumRows 
  250.       ************** 
  251.       Parameters: 
  252.          None 
  253.  
  254.       Return: 
  255.          The number of Rows affected by the last Query.. 
  256.  
  257.  
  258.  
  259.       What it's for: 
  260.          This function will return the number of affected Rows. Regardless if the 
  261.          last Query was a SELECT, UPDATE, DELETE, or INSERT Statment.. 
  262.  
  263.       ******************************************************************************/ 
  264.       function NumRows() 
  265.       { 
  266.          if( $this->iConnectId ) 
  267.             return $this->iNumRows; 
  268.          else 
  269.             return false; 
  270.       } 
  271.  
  272.        /****************************************************************************** 
  273.  
  274.       Function: MoveTo 
  275.       ************** 
  276.       Parameters: 
  277.          int $iRow == The Row to move to. 
  278.  
  279.       Return: 
  280.          true on success, else false. 
  281.  
  282.  
  283.  
  284.       What it's for: 
  285.          Moves the recordset to the specified row in the resultset. 
  286.          !!!! I implemented this function but haven't tested it yet!!!! *sorry* 
  287.  
  288.       ******************************************************************************/ 
  289.       function MoveTo( $iRow ) 
  290.       { 
  291.          if( $this->iConnectId && ($iRow <= $this->iNumRows) ) 
  292.          { 
  293.             if( mysql_data_seek( $this->iResultId , iRow ) ) 
  294.             {  $this->arRow = mysql_fetch_array( $this->iResultId ); 
  295.                $this->iCurrentRow = $iRow; 
  296.                return true; 
  297.             } 
  298.             else 
  299.                return false; 
  300.          } 
  301.          else 
  302.             return false; 
  303.       } 
  304.  
  305.        /****************************************************************************** 
  306.  
  307.       Function: GetField 
  308.       ************** 
  309.       Parameters: 
  310.          $szFeldName == Ups, some German slipped in here. 
  311.                         The field name for which to retrieve the Data. 
  312.  
  313.       Return: 
  314.          The Vaule of the Field. 
  315.  
  316.  
  317.  
  318.       What it's for: 
  319.          Retrieve the Vaule for Field in the current Row. 
  320.  
  321.       ******************************************************************************/ 
  322.       function GetField( $szFeldName ) 
  323.       {  return $this->arRow[$szFeldName]; 
  324.       } 
  325.  
  326.        /****************************************************************************** 
  327.  
  328.       Function: EchoFeld 
  329.       ************** 
  330.       Parameters: 
  331.          $szFeldName == Ups, some German slipped in here. 
  332.                         The field name for which to retrieve the Data. 
  333.  
  334.       Return: 
  335.          None. 
  336.  
  337.  
  338.  
  339.       What it's for: 
  340.          Echo the Vaule for Field in the current Row. 
  341.  
  342.       ******************************************************************************/ 
  343.       function EchoFeld( $szFeldName ) 
  344.       { 
  345.          echo $this->arRow[$szFeldName]; 
  346.       } 
  347.  
  348.        /****************************************************************************** 
  349.  
  350.       Function: NextRow 
  351.       ************** 
  352.       Parameters: 
  353.          None 
  354.  
  355.       Return: 
  356.          true if all OK. 
  357.          If something went wrong it will return false. 
  358.  
  359.  
  360.  
  361.       What it's for: 
  362.          Move to the NextRow in the result Set. 
  363.          This Function has to called directly after a query has been executed, otherwise 
  364.          no Data is available for retrieval. 
  365.  
  366.       ******************************************************************************/ 
  367.       function NextRow() 
  368.       { 
  369.          $iRet = true; 
  370.  
  371.          if( $this->iNumRows ) 
  372.          {  $this->arRow = mysql_fetch_array( $this->iResultId ); 
  373.  
  374.             if( $this->arRow ) 
  375.                $this->iCurrentRow++; 
  376.             else 
  377.                $iRet = false; 
  378.          } 
  379.          else 
  380.          { 
  381.             $iRet = false; 
  382.          } 
  383.          return $iRet; 
  384.  
  385.       } 
  386.  
  387.  
  388.        /****************************************************************************** 
  389.  
  390.       Function: Close 
  391.       ************** 
  392.       Parameters: 
  393.          None 
  394.  
  395.       Return: 
  396.          true on OK else false. 
  397.  
  398.  
  399.  
  400.       What it's for: 
  401.          Close a Databse Connection. 
  402.          Carefull here, if use initialised the Class with the function ReUSeConn. 
  403.          All query on this database connection will fail. This also counts for 
  404.          other instanceses of this Class. 
  405.  
  406.       ******************************************************************************/ 
  407.       function Close() 
  408.       { 
  409.          return mysql_close( $this->iConnectId ); 
  410.       } 
  411. $database_inc_php3 = 1; 
  412.  
  413. ?>
  414.