/ Published in: PHP
This is the first version of my PHP DB connection class using ODBC
Expand |
Embed | Plain Text
<?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() { } 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) { } else { $this->resultSet = odbc_prepare($this->conn, $sqlString); } } public function fetchArrayList() { while(odbc_fetch_into($this->resultSet, $row)) { } $this->fetchRowCount($rows); return $rows; } public function fetchArrayListEx() { $i = 0 ; $j = 0; 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 ) { { } } public function printOut($obj) { echo "<pre>"; echo "</pre>"; } } ?>
Comments
Subscribe to comments
You need to login to post a comment.

just a tip, when you retrieve SQL results from your server, you should go ahead and throw them into a local variable/property and immediately "release" the results to free up resources on the SQL server.
Also, your "fetchRowCould" method, lets say on query #1 it returns 3 records, query #2 returns no results (an update statement for example). If I run query #1 and then query #2 and THEN look at the "rowCount" property, it will say that my update statement returned 3 records (which is obviously false). I think you should put an 'else' block in that method so if "$arrCount" isn't an array (or is an array of zero length), then you should write a zero (0) to "rowCount".
Good job though, most people try to write database classes that try to do everything in the world (98% of if nobody uses). Yours is clean, to the point, all that's needed.