PHP Connection Class ODBC v1


/ Published in: PHP
Save to your folder(s)

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


Copy this code and paste it in your HTML
  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

You need to login to post a comment.