Posted By

mpcircuitry on 12/16/09


Tagged

php db odbc DBConnect


Versions (?)

PHP Connection Class ODBC v1


 / Published in: PHP
 

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

  1. <?php
  2. class DB_CONNECTION
  3. {
  4. private $server;
  5. private $user;
  6. private $pass;
  7. private $db;
  8. private $constring;
  9. protected $resultSet;
  10. private $conn;
  11. public $rowCount;
  12.  
  13.  
  14.  
  15.  
  16. function __construct($connectNow = true)
  17. {
  18. $this->server = 'myserver';
  19. $this->db = 'db_name';
  20. $this->user = 'db_user';
  21. $this->pass = 'db_pass';
  22. $this->constring = "DRIVER={SQL Native Client};SERVER=$this->server;DATABASE=$this->db";
  23.  
  24. if($connectNow)
  25. {
  26. $this->connect();
  27. }
  28. }
  29.  
  30. public function connect()
  31. {
  32. $this->conn = odbc_connect($this->constring, $this->user, $this->pass) or die('Could not connect');
  33. }
  34.  
  35. public function disconnect()
  36. {
  37. odbc_close($this->conn);
  38. }
  39.  
  40. public function executeQuery($sqlString, $sqlParams = null, $errMsg = 'Unkwon Error')
  41. {
  42.  
  43. if($this->resultSet)
  44. {
  45. odbc_free_result($this->resultSet);
  46. }
  47.  
  48. if($sqlParams==null)
  49. {
  50. $this->resultSet = odbc_exec($this->conn, $sqlString) or die($errMsg);
  51. }
  52. else
  53. {
  54. $this->resultSet = odbc_prepare($this->conn, $sqlString);
  55. odbc_execute($this->resultSet, $sqlParams) or die($errMsg);
  56. }
  57. }
  58.  
  59. public function fetchArrayList()
  60. {
  61. $row = array();
  62. $rows = array();
  63.  
  64. while(odbc_fetch_into($this->resultSet, $row))
  65. {
  66. array_push($rows,$row);
  67. }
  68.  
  69. $this->fetchRowCount($rows);
  70. return $rows;
  71. }
  72.  
  73. public function fetchArrayListEx()
  74. {
  75. $i = 0 ;
  76. $j = 0;
  77. $tmpResult = array();
  78. while(odbc_fetch_row($this->resultSet))
  79. {
  80. for($j=1; $j<= odbc_num_fields($this->resultSet); $j++)
  81. {
  82. $fieldName = odbc_field_name($this->resultSet,$j);
  83. $ar[$fieldName] = odbc_result($this->resultSet,$fieldName);
  84. }
  85. $tmpResult[$i] = $ar;
  86. $i++;
  87. }
  88.  
  89. //sets row count property
  90. $this->fetchRowCount($tmpResult);
  91. return $tmpResult;
  92. }
  93.  
  94. private function fetchRowCount($arrCount )
  95. {
  96. if(is_array($arrCount))
  97. {
  98. $this->rowCount = sizeof($arrCount);
  99. }
  100.  
  101. }
  102.  
  103. public function printOut($obj)
  104. {
  105. echo "<pre>";
  106. print_r($obj);
  107. echo "</pre>";
  108.  
  109. }
  110. }
  111.  
  112. ?>

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: MMDeveloper on December 17, 2009

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.

You need to login to post a comment.