Advertisement
Guest User

Untitled

a guest
Sep 27th, 2017
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 3.23 KB | None | 0 0
  1. <?php
  2. class MSSMySQLQuery
  3. {
  4.     private $cRes;
  5.     private $cQuery;
  6.     private $cHasQuery;
  7.     private $cResult;
  8.  
  9.     public function __construct(&$pRes, $pQuery = null, $pExec = false)
  10.     {
  11.         $this->setResource($pRes);
  12.         $this->setQuery($pQuery);
  13.         if ($pExec)
  14.         {
  15.             $this->execute();
  16.         }
  17.     }
  18.  
  19.     public function __destruct()
  20.     {
  21.         if ((null != $this->cResult) && is_resource($this->cResult))
  22.         {
  23.             mysql_free_result($this->cResult);
  24.         }
  25.     }
  26.  
  27.     public function setResource(&$pRes)
  28.     {
  29.         if (is_resource($pRes))
  30.         {
  31.             $this->cRes = $pRes;
  32.         }
  33.     }
  34.  
  35.     public function setQuery($pQuery)
  36.     {
  37.         if (null != $pQuery)
  38.         {
  39.             $this->cHasQuery    = true;
  40.         }
  41.         else
  42.         {
  43.             $this->cHasQuery    = false;
  44.         }
  45.         $this->cQuery   = $pQuery;
  46.         $this->cResult  = null;
  47.     }
  48.  
  49.     public function execute()
  50.     {
  51.         if ($this->cHasQuery)
  52.         {
  53.             $this->cResult = mysql_query($this->cQuery, $this->cRes);
  54.             if ($this->cResult)
  55.             {
  56.                 return true;
  57.             }
  58.         }
  59.         return false;
  60.     }
  61.  
  62.     public function result()
  63.     {
  64.         return $this->cResult;
  65.     }
  66.  
  67.     public function select($pType = "assoc")
  68.     {
  69.         if ((null != $this->cResult) && is_resource($this->cResult))
  70.         {
  71.             $rows   = array();
  72.             switch (mysql_num_rows($this->cResult))
  73.             {
  74.             case 0:
  75.                 return false;
  76.                 break;
  77.             default:
  78.                 if ("assoc" == $pType)
  79.                 {
  80.                     while ($row = mysql_fetch_assoc($this->cResult))
  81.                     {
  82.                         $rows[] = $row;
  83.                     }
  84.                 }
  85.                 elseif ("num" == $pType)
  86.                 {
  87.                     while ($row = mysql_fetch_row($this->cResult))
  88.                     {
  89.                         $rows[] = $row;
  90.                     }
  91.                 }
  92.                 else
  93.                 {
  94.                     while ($row = mysql_fetch_array($this->cResult))
  95.                     {
  96.                         $rows[] = $row;
  97.                     }
  98.                 }
  99.                 break;
  100.             }
  101.             return $rows;
  102.         }
  103.         return false;
  104.     }
  105.  
  106.     public function affectedRows()
  107.     {
  108.         if ((null != $this->cResult) && is_resource($this->cResult))
  109.         {
  110.             return mysql_num_rows($this->cResult);
  111.         }
  112.         return mysql_affected_rows();
  113.     }
  114.  
  115.     public function error()
  116.     {
  117.         return mysql_error($this->cRes);
  118.     }
  119. }
  120.  
  121. class MSSMySQL
  122. {
  123.     private $cResource;
  124.     private $cHost;
  125.     private $cUser;
  126.     private $cPassword;
  127.     private $cDb;
  128.  
  129.     public function __construct($pHost = MYSQL_HOST, $pUser = MYSQL_USER, $pPassword = MYSQL_PWD, $pDb = MYSQL_DB, $pAutoConnect = false)
  130.     {
  131.         $this->cHost        = $pHost;
  132.         $this->cUser        = $pUser;
  133.         $this->cPassword    = $pPassword;
  134.         $this->cDb          = $pDb;
  135.         if ($pAutoConnect)
  136.         {
  137.             $this->connect();
  138.         }
  139.     }
  140.  
  141.     public function connect()
  142.     {
  143.         if ($this->cResource = mysql_connect($this->cHost, $this->cUser, $this->cPassword))
  144.         {
  145.             if (mysql_select_db($this->cDb, $this->cResource))
  146.             {
  147.                 return true;
  148.             }
  149.         }
  150.         return false;
  151.     }
  152.  
  153.     public function __destruct()
  154.     {
  155.         if (is_resource($this->cResource))
  156.         {
  157.             mysql_close($this->cResource);
  158.         }
  159.     }
  160.  
  161.     public function query($pQuery, $pIsSelect = false)
  162.     {
  163.         if (is_resource($this->cResource))
  164.         {
  165.             $qry    = new MSSMySQLQuery($this->cResource, $pQuery);
  166.             if (!$qry->execute())
  167.             {
  168.                 return $qry->error();
  169.             }
  170.             if ($pIsSelect)
  171.             {
  172.                 return $qry->select();
  173.             }
  174.             return $qry->affectedRows();
  175.         }
  176.     }
  177.  
  178.     public function customQuery($pQuery)
  179.     {
  180.         return new MSSMySQLQuery($this->cResource, $pQuery);
  181.     }
  182.  
  183.     public function error()
  184.     {
  185.         return mysql_error($this->cResource);
  186.     }
  187. }
  188. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement