Location: PHPKode > scripts > swemc > swemc/swemc.php
<?php
/* Do not remove or alter this section***************************

************************Class Description************************
swemc 1.0  (Yet Another Mysql Class)  Jan 2005 Author: Uwe Stein

swemc provides a object for easily accessing mySql-Databases
It  -connects to the database
	-allows select,update,insert and delete-Queries
	-prints tables of query-results ( tables customizeable by CSS )
	-prints tables with single-records ( tables customizeable by CSS )
	-retrieve a specified data-cells (spec. by rec-no, field-no )
	-retrieve a specified record ( spec. by rec-no )
*****************************************************************

************************ sorry and thx ***************************
Please excuse errors in this text. English isnt my native language,
and so  suggestions about the code and the spelling are welcome
*********************Contact and Bug report***********************
 uwe nodot stein at gmx dot de
******************************************************************

********************** why the name swemc ? **********************
swemc is the acronym for "schon wieder eine mySql-Klasse"
means yet another mySql class but yamc is already used :-)
******************************************************************

********************Licence****************************************
This software is covered by The GNU General Public License (GPL)
***************************************************************

**************End of do not remove or alter section*************************/



// name-conventions:
// $aXx = array, $strXx = string, $bXx = bool, $iXx = int


class swemc
{
  var $aResult = array();
  var $iConnID;
  var $strHost;
  var $strUser;
  var $strPass;
  var $strDb;
  var $iFields;
  var $iRecs;
  var $aFieldNames = array();
  var $aTdwidth    = array();
  var $bDebug = FALSE; // if set TRUE, mySql-Errors are displayed

 // connects to the mysql-server, returns the connection-id at succes, otherwise FALSE
 function swemc ()
 {
    require_once("./swemc.inc.php");
    $this->strHost =  MYSQLHOST;
    $this->strUser =  MYSQLUSER;
    $this->strPass =  MYSQLPASS;
    $this->strDb   =  MYSQLDB;

	// default-values
    $this->iFields      = 0;
    $this->iRecs        = 0;
	$this->bDebug = FALSE;

    return  ($this->connect()) ;
 }



 // private func, no need to use
 // param : void
 // return: the connection-id or FALSE
 function connect()
 {
    $this->iConnID=@mysql_connect($this->strHost, $this->strUser, $this->strPass);
    if ($this->iConnID)
    {
            if (@mysql_select_db($this->db))
               return $this->iConnID;
			else
			{
                if ($this->bDebug == TRUE)
				    $this->printerror();
			    return FALSE;
			}

    }
    else
	{
	  if ($this->bDebug == TRUE)
	     $this->printerror();
      return FALSE;
	}

  } // End func connect();


  // manualy close the connection and free the mySql-result
  // normally not needed
  // param : void
  // return: void
  function disconnect()
  {
     $this->free_result();
	 if ( @MYSQL_CLOSE($this->iConnID))
	    $this->iConnID = FALSE;
  }




 // executes the given query:
 // after a successful SELECT query:
 // $this->iRecs shows the number of records in $this->aResult
 // $this->iFields shows the number of fields in a record
 // the field-names are stored in $this->aFieldNames
 // the number of selected records is returned
 //
 // after a successful  INSERT query: the insert-id is returned
 //
 // after a successful  INSERT, DELETE or UPDATE query:
 // the number of mysql_affected_rows ist returned ( may be also 0 at success )
 //
 // param  : the querystring
 // return : s.o.
 function query($querystring)
 {
     if (!$this->iConnID)
	     die("No connection to mySql<br>");
	 $this->aResult = mysql_db_query($this->strDb,$querystring,$this->iConnID);
     if ( !$this->aResult)
     {
         if ($this->bDebug == TRUE)
		    $this->printerror(1); // print the errmsg and exit

     }

	 // it is a SELECT-query ?
     if ( eregi("select",$querystring))
     {
        $this->iRecs   = @mysql_num_rows($this->aResult);
		$this->iFields = @mysql_num_fields($this->aResult);

		 // get the field-names
		 for($x=0;$x<$this->iFields;$x++)
         {
            $this->aFieldNames[$x] = @mysql_field_name($this->aResult,$x);
         }
		 return $this->iRecs;
     }
     // return insert-id after a INSERT-Query
	 elseif ( eregi("insert",$querystring))
	 {
          return mysql_insert_id($this->iConnID);
	 }
     // otherwise it was a  DELETE or UPDATE query
	 else
	 {
        // at this point it was a successful query
		// affected rows is returned
		return ( mysql_affected_rows($this->iConnID));
	 }
 }



 // by default the function tab_out( prints a table of the current result)
 // uses the same width for every column
 // with td_width u can specify a width for every column
 // all types are possible ( %,px ...)
 // it works only if the given array keeps a value for each column
 // remember to change or unset the array $this->aWidth before you
 // print another table after a new query
 //
 // Param  : Array with width-values
 // Return : void
 function td_width($width_array)
 {
    $x = count($width_array);
    // if there isnt a value for each culumn, return
    if ($x != $this->iFields)
	{
        if ($this->bDebug)
		{
		    echo "incorrect number in width-array<br>";
		    echo " $x in array, $this->iFields in record";
  		}
		return;
	}
    for ($i = 0; $i < $x; $i++)
        $this->aTdwidth[$i] = $width_array[$i];
 }




// prints a simple table of the current result
// if you want to change the layout of the table, provide a style-sheet
// and send the class-name for your table as the 2nd param
// Param : $with_table_head: TRUE or FALSE wether you want the
//                           table-head shown or not
//         $css_class     :  pass the name of your CSS-File if used
// return: void
function tab_out($with_table_head,$css_class="noCSS" )
{

   if (count($this->aTdwidth))  // are there width-values ?
       $bWidth = TRUE;
   else
       $bWidth = FALSE;
   echo "<table  border='1'";
   if ($css_class != "noCSS")
       echo "class=\"$css_class\" ";
   echo ">";

   // with table-head ?
   if ( $with_table_head == TRUE )
   {
      echo "<tr class=\"$css_class\">";
      for ($i=0;$i<$this->iFields;$i++)
      {
         if ($bWidth == TRUE)
             $width = $this->aTdwidth[$i];
         else
             $width = 100/$this->iFields."%";
         echo "<th  class=\"$css_class\" width=\"$width\">";
         echo ucfirst($this->aFieldNames[$i]);
         echo "</th>";
      }
      echo "</tr>";
   }  // end of table-head

   // print the records
   for ($j = 0; $j < $this->iRecs; $j++)
   {
       echo "<tr class=\"$css_class\">";
       for ($k=0;$k<$this->iFields;$k++)
       {
           if ($bWidth == TRUE)
              $width = $this->aTdwidth[$k];
           else
              $width = 100/$this->iFields."%";
           echo "<td width=\"$width\" class=\"$css_class\">";
		   $xtmp = mysql_result($this->aResult,$j,$k);
		   if (strlen($xtmp))
		      echo $xtmp;
		   else
		      echo"&nbsp;";
           echo  "</td>";
        }
        echo "</tr>";
  }

	echo "</table>";
}


//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

function tb_tab_out($css_class="standard", $width="80%")
{



   if (count($this->aTdwidth))  // sind individuelle Breiten hinterlegt?
       $arr_breiten = TRUE;
   echo "<p class=\"center\"><table class=\"$css_class\" width='$width' align='center'>";
   // Tabellenkopf
      echo "<tr>";

         for ($i=0;$i<$this->iFields;$i++)
         {
             // Breite der Spalte aus $td_width
             if ($arr_breiten)
                $width = $this->aTdwidth[$i];
             else
             $width = 100/$this->iFields."%";

             echo "<th width=\"".$width."\">";
             echo      strtoupper($this->aFieldNames[$i]);
             echo "</th>";
         }


      echo "</tr>";  // EndeTabellenkopf


	  for ($j = 0; $j < $this->iRecs; $j++)
      {
            echo "<tr>";
			$l=0;
            for ($k=0;$k<$this->iFields-1;$k++)
            {
                   // Breite der Spalte aus $td_width
                   if ($arr_breiten)
                       $width = $this->aTdwidth[$k];
                   else
                       $width = 100/$this->iFields."%";
                   echo "<td width=\"".$width."\">";
				   $xtmp = mysql_result($this->aResult,$j,$k);
				   if (strlen($xtmp))
				      echo $xtmp;
			       else
				      echo"&nbsp;";

                    echo  "</td>";
					$l=$k+1;
            }
			//++++++++++++++++++++++++++++
		    //
		    // extra: den Button für details ansehen.
                   // Breite der Spalte aus $td_width
                   if ($arr_breiten)
                       $width = $this->aTdwidth[$k];
                   else
                       $width = 100/$this->iFields."%";
                   echo "<td width=\"".$width."\">";
				   $xtmp = mysql_result($this->aResult,$j,$l);
                   $param = "detail";
				   $paramwert = $xtmp;
				   $act_url = "tagesberichte_detail.php";
				   if (strlen($xtmp))
				      echo postButton("Ansehen",$act_url,$param,$paramwert);
			       else
				      echo"&nbsp;";

                    echo  "</td>";
            //++++++++++++++++++++++++++++
            echo "</tr>";
      }

	echo "</table></p>";
}

//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++



// retrieve a single field-value from the result
// param : the rec-no and the field-no or field-name if known
// return: the value-string
function fetch_field($recno,$fieldno_or_fieldname)
{
   return  mysql_result($this->aResult,$recno,$fieldno_or_fieldname);
}

function fetch_record($recno,$arr_type = MYSQL_BOTH)
{
   if (!@mysql_data_seek($this->aResult,$recno))
   {
      if ($this->bDebug == TRUE)
		 $this->printerror(TRUE);// print error and exit
   }
   $arr = mysql_fetch_array($this->aResult,$arr_type);
   return $arr;
}

// prints a table with a single record of the mySql-result
// param  : $recno -> the record-number to be shown
//          $cssClass -> the CSS-class to format the table
// return : void
function single_out($recno,$cssClass="noCSS"  )
{

   echo "<table border=\"1\"";
   if ($cssClass != "noCSS")
       echo " class=\"$cssClass\"";
   echo ">";

       for ($i=0; $i < $this->iFields; $i++)
       {
          echo "<tr>";
          echo       "<td class=\"".$tableclass."\">";
          echo           strtoupper($this->aFieldNames[$i]);
          echo       "</td>";
          echo       "<td class=\"".$tableclass."\">";
		  $xtmp = mysql_result($this->aResult,$recno,$i);
          if (strlen($xtmp))
			 echo $xtmp;
		  else
			 echo"&nbsp;";
          echo       "</td>";
          echo "</tr>";
       }
    echo "</table>";


}


// free the current result
// param : void
// return: void
function free_result()
{
     if($this->aResult)
     {
       @mysql_free_result($this->aResult);
       $this->iRecs = $this->iFields = 0;
     }
}



// prints the last mySql-error if $this->bDebug == TRUE
//param : exitnow if TRUE, the script dies after printing the errmsg
//                if FALSE, only the errmsg is shown
//return: void
function printerror($exitnow = "FALSE")
{
   echo "mySQL-Error No: " . mysql_errno()." - " .mysql_error();
   if ($exitnow)
      exit;
}


}// End class swemc
?>
Return current item: swemc