MSSQL class.

Results 1 to 3 of 3
  1. #1
    Hi, I'm Omar! Vusion is offline
    MemberRank
    Jan 2011 Join Date
    HereLocation
    1,658Posts

    MSSQL class.

    Well, I wanted to try something out and to test some stuff, and then I ended up writing this class.

    It's completely unusable, well, at least, in the state it is in right now. But I pretty much feel great for writing it as I stumbled across some few points in PHP that I was weak at(arrays, specifically.).

    So, yeah, here it is.

    Code:
    <?php
    /*--------------------------------------------
    * @author: AUTHOR
    * @framework-author: Omar Wael
    *---------------------------------------------
    ********************USAGE*********************
    * $_database['host']	= "OMAR-PC\SQLEXPRESS";
    * $_database['user']	= "sa";
    * $_database['pass']	= "password";
    * $_database['dbn']	= "GunzDB";
    *
    *
    * $db = new mssql();
    *
    * $db->executeQuery(array("AID", "UGradeID"), "Account", "WHERE AID IN (SELECT AID FROM Character WHERE Name = 'Vusion' AND DeleteFlag != 1)");
    * echo $db->result();
    * $db->disconnect();
    * --------------------------------------------*/
    
    class mssql
    {
    	/* Variable holding the server name. */
    	var $host;
    	
    	/* Variable holding the username. */
    	var $user;
    	
    	/* Variable holding the password. */
    	var $pass;
    	
    	/* Variable holding the database name. */
    	var $dbn;
    
    	/* Variable holding the database connection. */
    	var $link;
    	
    	/* Boolean telling whether we're connected or not. */
    	var $isCon;
    	
    	/* Variable holding the current class query object. */
    	var $classQuery;
    	
    	/* Variable holding the rows selected in the current class query. */
    	private $queryRows;
    
    	/* Return array for the current class query. */
    	private $retArr = array();
    	
    	public function __construct()
    	{
    		$this->host = $_database['host'];
    		$this->user = $_database['user'];
    		$this->pass = $_database['pass'];
    		$this->dbn  = $_database['dbn'];
    		
    		$this->connect();
    	}
    	
    	public function connect()
    	{
    		$this->link = mssql_connect($this->host, $this->user, $this->pass) or die("Could not connect to MSSQL!");
    		$this->isCon = true;
    		$this->selectDatabase($this->dbn);
    	}
    	
    	public function selectDatabase($dbn)
    	{
    		mssql_select_db($dbn, $this->link);
    	}
    	
    	public function disconnect()
    	{
    		mssql_close($this->link);
    		$this->isCon = false;
    	}
    	
    	public function executeQuery($rows, $table, $conditions = "")
    	{
    		if(!$this->isCon)	return;
    		
    		$this->queryRows = $rows;
    		$this->classQuery = mssql_query("SELECT ".((is_array($rows)) ? implode(", ", $rows) : $rows)." FROM ".$table." ".$conditions."");
    	}
    	
    	public function result($method = "implode")
    	{
    		if(!isset($this->classQuery))	return;
    		
    		if(mssql_num_rows($this->classQuery) < 1)
    			return false;
    			
    		$fetch = mssql_fetch_array($this->classQuery);
    			
    		for($i = 0; $i < ((is_array($this->queryRows)) ? count($this->queryRows) : 1); $i++)
    			array_push($this->retArr, $fetch[$i]);
    		
    		if($method == "implode")
    			return ((is_array($this->queryRows)) ? implode(", ", $this->retArr) : implode("", $this->retArr));
    		else
    			return $this->retArr;
    	}
    }
    ?>
    Example for usage:
    Code:
    <?php
    $_database['host']	= "OMAR-PC\SQLEXPRESS";
    $_database['user']	= "sa";
    $_database['pass']	= "mazemania1";
    $_database['dbn']	= "GunzDB";
    
    
    $db = new mssql();
    
    $db->executeQuery(array("AID", "UGradeID"), "Account", "WHERE AID IN (SELECT AID FROM Character WHERE Name = 'Vusion' AND DeleteFlag != 1)");
    echo $db->result();
    $db->disconnect();
    ?>
    Last edited by Vusion; 17-01-13 at 05:01 PM. Reason: Worked a bit more on it.


  2. #2
    Omega Ron is offline
    MemberRank
    Apr 2005 Join Date
    Location
    8,990Posts

    Re: MSSQL class.

    A bit of unnecessary code, for instance your $db->isCon variable is pointless as the script dies if you don't connect. If the script doesn't die then the connection is obviously okay.

    Also, use __construct to create the connection when you use $db = new dbclass(); and use __destruct() to automatically close the connection when the class instance is destroyed. Prevents accidentally leaving open connections. And uses less lines in the future.

  3. #3
    Hi, I'm Omar! Vusion is offline
    MemberRank
    Jan 2011 Join Date
    HereLocation
    1,658Posts

    Re: MSSQL class.

    Quote Originally Posted by Ron View Post
    A bit of unnecessary code, for instance your $db->isCon variable is pointless as the script dies if you don't connect. If the script doesn't die then the connection is obviously okay.

    Also, use __construct to create the connection when you use $db = new dbclass(); and use __destruct() to automatically close the connection when the class instance is destroyed. Prevents accidentally leaving open connections. And uses less lines in the future.
    isCon variable is for the lulz, though.

    But yeah, I guess you're right with the __destruct() remark, thanks.



Advertisement