Return to Snippet

Revision: 21628
at December 16, 2009 15:10 by mpcircuitry


Updated Code
<?php
	class DB_CONNECTION
	{
		private $server;
		private $user;
		private $pass;
		private $db;
		private $constring;
		protected $resultSet;
		private $conn;
		public $rowCount;
		
		
		

		function __construct($connectNow = true)
		{
			$this->server = 'myserver';
			$this->db = 'db_name';
			$this->user = 'db_user';
			$this->pass = 'db_pass';
			$this->constring = "DRIVER={SQL Native Client};SERVER=$this->server;DATABASE=$this->db";
			
			if($connectNow)
			{
				$this->connect();
			}
		}
		
		public function connect()
		{
			$this->conn = odbc_connect($this->constring, $this->user, $this->pass) or die('Could not connect');
		}
		
		public function disconnect()
		{
			odbc_close($this->conn);
		}
		
		public function executeQuery($sqlString, $sqlParams = null, $errMsg =  'Unkwon Error')
		{
			
			if($this->resultSet)
			{
				odbc_free_result($this->resultSet);
			}
			
			if($sqlParams==null)
			{
				$this->resultSet = odbc_exec($this->conn, $sqlString) or die($errMsg);
			}
			else
			{
				$this->resultSet = odbc_prepare($this->conn, $sqlString);
				odbc_execute($this->resultSet, $sqlParams) or die($errMsg);
			}
		}
		
		public function fetchArrayList()
		{
			$row = array();
			$rows = array();
			
			while(odbc_fetch_into($this->resultSet, $row))
			{
				array_push($rows,$row);
			}
			
			$this->fetchRowCount($rows);
			return $rows;
		}
		
		public function fetchArrayListEx()
		{
			$i = 0 ;
			$j = 0;
			$tmpResult = array();
			while(odbc_fetch_row($this->resultSet))
			{
				for($j=1; $j<= odbc_num_fields($this->resultSet); $j++)
				{
					$fieldName = odbc_field_name($this->resultSet,$j);
					$ar[$fieldName] = odbc_result($this->resultSet,$fieldName);
				}
				$tmpResult[$i]  = $ar;
				$i++;
			}
			
			//sets row count property
			$this->fetchRowCount($tmpResult);
			return $tmpResult;
		}
		
		private function fetchRowCount($arrCount )
		{
			if(is_array($arrCount))
			{
				$this->rowCount = sizeof($arrCount);
			}
			
		}
		
		public function printOut($obj)
		{
			echo "<pre>";
			print_r($obj);
			echo "</pre>";
			
		}
	}

?>

Revision: 21627
at December 16, 2009 15:10 by mpcircuitry


Initial Code
<?php
	class DB_CONNECTION
	{
		private $server;
		private $user;
		private $pass;
		private $db;
		private $constring;
		protected $resultSet;
		private $conn;
		public $rowCount;
		
		
		
		//$dsn = "DRIVER={SQL Native Client};SERVER=mac-dev\sqlexpress;DATABASE=MPH_DEV";

		function __construct($connectNow = true)
		{
			$this->server = 'myserver';
			$this->db = 'db_name';
			$this->user = 'db_user';
			$this->pass = 'db_pass';
			$this->constring = "DRIVER={SQL Native Client};SERVER=$this->server;DATABASE=$this->db";
			
			if($connectNow)
			{
				$this->connect();
			}
		}
		
		public function connect()
		{
			$this->conn = odbc_connect($this->constring, $this->user, $this->pass) or die('Could not connect');
		}
		
		public function disconnect()
		{
			odbc_close($this->conn);
		}
		
		public function executeQuery($sqlString, $sqlParams = null, $errMsg =  'Unkwon Error')
		{
			
			if($this->resultSet)
			{
				odbc_free_result($this->resultSet);
			}
			
			if($sqlParams==null)
			{
				$this->resultSet = odbc_exec($this->conn, $sqlString) or die($errMsg);
			}
			else
			{
				$this->resultSet = odbc_prepare($this->conn, $sqlString);
				odbc_execute($this->resultSet, $sqlParams) or die($errMsg);
			}
		}
		
		public function fetchArrayList()
		{
			$row = array();
			$rows = array();
			
			while(odbc_fetch_into($this->resultSet, $row))
			{
				array_push($rows,$row);
			}
			
			$this->fetchRowCount($rows);
			return $rows;
		}
		
		public function fetchArrayListEx()
		{
			$i = 0 ;
			$j = 0;
			$tmpResult = array();
			while(odbc_fetch_row($this->resultSet))
			{
				for($j=1; $j<= odbc_num_fields($this->resultSet); $j++)
				{
					$fieldName = odbc_field_name($this->resultSet,$j);
					$ar[$fieldName] = odbc_result($this->resultSet,$fieldName);
				}
				$tmpResult[$i]  = $ar;
				$i++;
			}
			
			//sets row count property
			$this->fetchRowCount($tmpResult);
			return $tmpResult;
		}
		
		private function fetchRowCount($arrCount )
		{
			if(is_array($arrCount))
			{
				$this->rowCount = sizeof($arrCount);
			}
			
		}
		
		public function printOut($obj)
		{
			echo "<pre>";
			print_r($obj);
			echo "</pre>";
			
		}
	}

?>

Initial URL

                                

Initial Description
This is the first version of my PHP DB connection class using ODBC

Initial Title
PHP Connection Class ODBC v1

Initial Tags
php

Initial Language
PHP