php - Correct Object-Oriented Programming (Database Objects) -


my company switched php asp , asp.net. hosting asp, asp.net , php on iis running combined sessions. working on nice reusable database connection class , wanted see thought. tried search no fruitful results.

this code non-operational. first attempt @ writing reusable class please bare me.

first, is better create new connection object each time want call database? or better have continuous connection , reuse same connection? suggested crash server if created new connection each database call in script.

here how doing it.

$conn = new connectionclass; $result = $conn->createdataset('select whatever whereever;');  $conn2 = new connectionclas; $result 2 = $conn2->executescalar("insert  (whatever) values ('whereever');") 

i asked set can call in one line on page without having create new object each time want use class functions. like:

$result = $conn::createdataset("select whatever whereever"); $result2 = $conn2::insert("insert  (whatever) values ('whereever');select identity_scope;") 

this class in question.

<?php  /**  * connectionclass - creates connection database , executes      specified function  *  * @version 1.0  * @author mhano00  */ class mssqlconnectionclass { private $servername; private $connectioninfo; private $sql; private $result; private $rows; private $row; private $stmt; private $myarray = array(); private $conn; private $database;    /**  * creates connection server upon instanciation of class  * fires everytime class used  */ public function __construct($_newserver, $_database) {     $this->servername = $_newserver;     $this->database = $_database;     $this->connect(); }    public function __destruct() {     //destruct fires no matter what, free resources     if ($this->stmt)     {         //frees statement         sqlsrv_free_stmt($this->stmt );     }     if ($this->conn)     {         //frees server cache         sqlsrv_close($this->conn);     } }    /**  * creates connection server   *   */ public function connect() {     $this->connectioninfo = ["database" => $this->database, "uid" => 'srs', "pwd" => "st0r3", "returndatesasstrings"=>true];     $this->conn = sqlsrv_connect($this->servername, $this->connectioninfo);     if (!$this->conn === true) {         //if connection fails try 1 more time before erroring out.         $this->conn = sqlsrv_connect($this->servername, $this->connectioninfo);         if (!$this->conn === true) {             //catches error on no connection database,              //todo send auto email error             echo "connection not established.<br />";             die(print_r(sqlsrv_errors(), true));         }         else         {             return $this->conn;         }     }     else     {         return $this->conn;     } }     /**  *  * executes $imcomingsql query , returns first cell in first row  *  * @param string $incomingsql - sql query want execute  */ public function executescalar($incomingsql) {     //select scalar, 1 cell value returned     $this->stmt = sqlsrv_query($this->conn, $incomingsql);     if ($this->stmt) {         $this->rows = sqlsrv_has_rows($this->stmt);         if ($this->rows) {             $this->row = sqlsrv_fetch_array($this->stmt, sqlsrv_fetch_numeric);              $this->result = $this->row[0];             return $this->result;         }     }     else {         return false;     }   }      /**  *   *  * executes sql query , returns array of queried data  *  * @param string $incomingsql - sql query want execute  * @return array $myarray - array of queried value  */ public function createdataset($incomingsql) {     //returns array of selected data     $this->stmt = sqlsrv_query($this->conn, $incomingsql);     if ($this->stmt) {         $this->rows = sqlsrv_has_rows($this->stmt);         if ($this->rows) {              while($this->row = sqlsrv_fetch_array($this->stmt, sqlsrv_fetch_numeric))             {                                 $this->myarray[] = $this->row;                     }                return $this->myarray;         }     }     else     {         return false;     } }   /**  * inserts sql query , returns specified column value  *  * @param string $incomingsql -the sql query want execute  * @param string $returncolumnname - name of column want return  * @return string/int - value of specified $returncolumnname of inserted row  */ public function insert($incomingsql) {     //this function insert values specified table , retrun specified column value, id of inserted row.     $this->sql = $incomingsql.'; select scope_identity();';     $this->result = sqlsrv_query($this->conn,$this->sql);       sqlsrv_next_result($this->result);      sqlsrv_fetch($this->result);     return sqlsrv_get_field($this->result, 0);  }    /**  * sets server ip new ip  *  * accepts 1 parameter  *  * @param int $newserver new ip address want switch  */ public function setserver($_newserver, $_database) {     //must close connection in order connect new 1     if ($this->stmt)     {         //frees statement         sqlsrv_free_stmt($this->stmt );     }     if ($this->conn)     {         //frees server cache         sqlsrv_close($this->conn);     }     $this->servername = $_newserver;     $this->database = $_database;     $this->connect(); }     /**  * simple function return current ip object pointing  *  * @return string/int current ip address  */ public function getserver() {     return $this->servername; }  } 


Comments

Popular posts from this blog

css - SVG using textPath a symbol not rendering in Firefox -

Java 8 + Maven Javadoc plugin: Error fetching URL -

datatable - Matlab struct computations -