Location: PHPKode > scripts > MySQL Table Abstraction Class > mysql-table-abstraction-class/mtac.php
<?
/* mtac.php - Database abstraction class for generic objects.
 *
 * 21-Jul-2005 V0.1 NNS - Original Draft
 */
define('MTAC_GET_BYID','MTAC_GET_BYID');
define('MTAC_GET_BYNAME','MTAC_GET_BYNAME');
define('MTAC_GET_UNSPEC','MTAC_GET_UNSPEC');

class mtac {
    var $table;      // Database table for this object
    var $d;          // Associative array of data for this object
    var $db;
    /*
     * Constructor
     * Parameters: $table - the table this object represents
     *             $id - Either an integer ID or a string name
     *
     * Throws Exception when:
     *          No table or db handle
     *          id is neither integer or string
     *          mtac->Get returns false
     */
    function __construct($db, $table = "",$id = 0) {

        if((empty($table) || $table == "") || !is_resource($db))
          throw new Exception("Attempt to instantiate mtac with no table and/or db handle");

        $type = MTAC_GET_UNSPEC;
        $vt = gettype($id);
        $this->db = $db;
        $this->table = $table;
        switch($vt) {
         case "integer" :
            if($id == 0)
              $type = MTAC_GET_UNSPEC;
            else {
                $type = MTAC_GET_BYID;
                $this->d['id'] = $id;
            }
            break;
         case "string" :
            if($id == "")
              $type = MTAC_GET_UNSPEC;
            else {
                $type = MTAC_GET_BYNAME;
                $this->d['name'] = $id;
            }
            break;
         default:
            throw new Exception("mtac: Invalid variable type ($vt)");
        }
        if($type == TYPE_UNSPEC)
          return;
        if(!$this->Get($type))
          throw new Exception("mtac->Get($type) failed: " . mysql_error($this->db));
    }
    /*
     * Get - Load object data from database.
     * Parameter: $type - either MTAC_GET_BYID or MTAC_GET_BYNAME
     */
    function Get($type = MTAC_GET_BYID) {
        switch($type) {
         case MTAC_GET_BYID:
            $q = "select * from $this->table where id=". $this->d['id'];
            break;
         case MTAC_GET_BYNAME:
            $q = "select * from $this->table where name='" . $this->d['name'] . "'";
            break;
         default:
            return false;
        }
        $r = mysql_query($q);
        if($r === false)
          return $r;

        if(($i = mysql_num_rows($r)) == false)
          return false;
        $this->d = mysql_fetch_assoc($r);
        mysql_free_result($r);
        return true;
    }
    /*
     * Put - Put object data into database.
     * Returns: false on error, true on success.
     *
     * If $this->d['id'] == 0 a new record is written, otherwise
     * the data is replaced
     */
    function Put() {
        $qi = "insert into $this->table (";
        $qr = "replace into $this->table (";
        foreach($this->d as $key => $value) {
            if($qn != "") {
                $qn .= ",";
                $qv .= ",";
            }
            $qn .= "`$key`";
            $qv .= "'$value'";
        }
        $qn .= ") values( ";
        $qv .= ")";

        if($this->d['id'] == 0)
          $q = $qi . $qn . $qv;
        else
          $q = $qr . $qn . $qv;

        $r = mysql_query($q);
        return $r;
    }
    /*
     * Drop this table row.
     * if $this->d['id'] == 0 it is an error (cant delete what isnt there)
     * Returns status of mysql query (true/false)
     */
    function Drop() {
        if($this->d['id'] == 0)
          return false;
        $r = mysql_query("delete from $this->table where id=" . $this->d['id']);
        return $r;
    }

}
Return current item: MySQL Table Abstraction Class