Advertisement
Guest User

Untitled

a guest
Jan 25th, 2016
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.03 KB | None | 0 0
  1. <?php
  2. class db{
  3. var $Connection = array(
  4. 'Host' => 'localhost' , // Host of Connection.
  5. 'User' => 'root' , // Database's User
  6. 'Password' => 'toor' , // User Password
  7. 'Database' => 'higienizacao' , // Default Database
  8. 'Db_Type' => 'mysql' , // Type of Database. (Can be: Mysql , Mssql , SQLserv , PGsql).
  9. );
  10. var $Query = '' ; // Do not change.
  11. var $MySQLi = '' ; // Do not change.
  12. var $DBConnection = '' ; // Do not change.
  13. public function __construct($NewDatabase = '', $DebugMode = false, $SetDiferentConn = ''){
  14. /* About:
  15. -> NewDatabase: set a new database to connect (like: users or topics).
  16. -> DebugMode: Mode to debug your application, it shows everything that is done.
  17. -> SetDiferentConn: Set another connection, must to pass the initial array model ($Connection).
  18. */
  19. $this->Generalinfo = array(
  20. 'DebugMode' => $DebugMode ,
  21. 'Started_Time' => date('Y-m-d H:i:s') ,
  22. );
  23. if( ! empty( $NewDatabase ) )
  24. $this->Connection['Database'] = $NewDatabase;
  25. if( ! empty( $SetDiferentConn ) )
  26. $this->Connection = $SetDiferentConn;
  27. $this->Connection['Db_Type'] = strtolower($this->Connection['Db_Type']);
  28.  
  29. $this->Connect();
  30. }
  31. public function __destruct(){
  32. /* About:
  33. -> Close the connection and returns the elapsed time.
  34. */
  35. if ( ! empty( $this->Generalinfo['DebugMode'] ) )
  36. $this->ErrorHandling();
  37. switch ($this->Connection['Db_Type']) {
  38. case 'mysql':
  39. $this->MySQLi->close();
  40. break;
  41. case 'mssql':
  42. mssql_close($this->DBConnection);
  43. break;
  44. case 'sqlserv':
  45. sqlsrv_close($this->DBConnection);
  46. break;
  47. case 'pgsql':
  48. pg_close($this->DBConnection);
  49. break;
  50. }
  51. return $this->ElapsedTime();
  52. }
  53. public function Query($Query){
  54. /* About:
  55. -> Query($Query): Do a SQL Query. (Returns the SQL Query in a String).
  56. */
  57. $this->SQL = $Query;
  58.  
  59. if ( ! empty( $this->Generalinfo['DebugMode'] ) )
  60. $this->PrintReport($Query);
  61.  
  62. switch ($this->Connection['Db_Type']) {
  63. case 'mysql':
  64. $this->Query = $this->MySQLi->query($Query)
  65. or trigger_error(
  66. $this->PrintReport($Query . ' - ' . $this->MySQLi->error),
  67. E_USER_ERROR
  68. );
  69. break;
  70. case 'mssql':
  71. $this->Query = mssql_query(
  72. $Query,
  73. $this->DBConnection
  74. )
  75. or trigger_error(
  76. $this->PrintReport(mssql_get_last_message()),
  77. E_USER_ERROR
  78. );
  79. break;
  80. case 'pgsql':
  81. $this->Query = pg_query(
  82. $Query,
  83. $this->DBConnection
  84. ) or trigger_error(
  85. $this->PrintReport(pg_last_error($this->DBConnection)),
  86. E_USER_ERROR
  87. );
  88. break;
  89. case 'sqlserv':
  90. $this->Query = sqlsrv_query(
  91. $this->DBConnection,
  92. $Query,
  93. array(),
  94. array(
  95. "Scrollable" => "buffered"
  96. )
  97. ) or trigger_error(
  98. $this->PrintReport(print_r(sqlsrv_errors())),
  99. E_USER_ERROR
  100. );
  101. break;
  102. }
  103. return $this->Query;
  104. }
  105. public function Fetch($Query = ''){
  106. /* About:
  107. -> Fetch($Query): Fetch rows of the SQL Query. (If no rows: returns the SQL Query in a String).
  108. */
  109. if ( ! empty( $Query ) )
  110. $this->Query($this->SQL);
  111. $Query = $this->Query;
  112. switch ($this->Connection['Db_Type']) {
  113. case 'mysql':
  114. if(is_object($this->Query))
  115. return $this->Query->fetch_array(MYSQLI_ASSOC);
  116. break;
  117. case 'mssql':
  118. echo 'fetching..';
  119. return mssql_fetch_row($Query);
  120. break;
  121. case 'pgsql':
  122. return pg_fetch_row($Query);
  123. break;
  124. case 'sqlserv':
  125. return sqlsrv_fetch_array($Query, SQLSRV_FETCH_ASSOC);
  126. break;
  127. }
  128. }
  129. public function NumRows($Query = ''){
  130. /* About:
  131. -> NumRows($Query): Returns the number of rows in a SQL Query.
  132. */
  133. if ( ! empty( $Query ) )
  134. $this->Query($this->SQL);
  135. $RowsNumber = 0;
  136. $Query = $this->Query;
  137. switch ($this->Connection['Db_Type']) {
  138. case 'mysql':
  139. $RowsNumber = $this->Query->num_rows;
  140. break;
  141. case 'mssql':
  142. $RowsNumber = mssql_num_rows($Query);
  143. break;
  144. case 'pgsql':
  145. $RowsNumber = pg_num_rows($Query);
  146. break;
  147. case 'sqlserv':
  148. $RowsNumber = sqlsrv_num_rows($Query);
  149. break;
  150. }
  151. if ( ! empty( $this->Generalinfo['DebugMode'] ) )
  152. $this->PrintReport(' '.$RowsNumber.' Lines Found');
  153.  
  154. return $RowsNumber;
  155. }
  156. public function HaveRows($Query=''){
  157. /* About:
  158. -> NumRows($Query): Returns if there are rows in SQL Query.
  159. */
  160. if ( ! empty( $Query ) )
  161. $this->Query($this->SQL);
  162. $Query = $this->Query;
  163. if ($this->NumRows($Query) > 0){
  164. if ( ! empty( $this->Generalinfo['DebugMode'] ) )
  165. $this->PrintReport('Have Rows: TRUE');
  166. return true;
  167. }else{
  168. if ( ! empty( $this->Generalinfo['DebugMode'] ) )
  169. $this->PrintReport('Have Rows: FALSE');
  170. return false;
  171. }
  172. }
  173. public function AffectedRows(){
  174. /* About:
  175. -> AffectedRows(): Returns the number of affected rows in a SQL Query.
  176. */
  177. $RowsNumber = 0;
  178. switch ($this->Connection['Db_Type']) {
  179. case 'mysql':
  180. $RowsNumber = $this->MySQLi->affected_rows;
  181. break;
  182. case 'mssql':
  183. $RowsNumber = mssql_rows_affected($this->DBConnection);
  184. break;
  185. case 'pgsql':
  186. $RowsNumber = pg_affected_rows($this->Query);
  187. break;
  188. case 'sqlserv':
  189. $RowsNumber = sqlsrv_rows_affected($this->Query);
  190. break;
  191. }
  192. if ( ! empty( $this->Generalinfo['DebugMode'] ) )
  193. $this->PrintReport('Rows Affected: '.$RowsNumber);
  194. return $RowsNumber;
  195. }
  196. public function QueryResult(){
  197. /* About:
  198. -> QueryResult(): Returns true if there aren't errors and false if there are.
  199. */
  200. if ($this->AffectedRows() >= 0 )
  201. $resultado = true;
  202. if ($this->NumRows() >= 0)
  203. $resultado = true;
  204. if ( !empty( $resultado ) ){
  205. if ( ! empty( $this->Generalinfo['DebugMode'] ) )
  206. $this->PrintReport('Query Result: NO ERRORS');
  207. return true;
  208. }else{
  209. if ( ! empty( $this->Generalinfo['DebugMode'] ) )
  210. $this->PrintReport('Query Result: ERRORS IN EXECUTION');
  211. return false;
  212. }
  213. }
  214. public function ElapsedTime(){
  215. /* About:
  216. -> ElapsedTime(): Returns the elapsed time of the object execution.
  217. */
  218. $TimeNow = date('Y-m-d H:i:s');
  219. return (strtotime($TimeNow) - strtotime($this->Generalinfo['Started_Time']));
  220. }
  221. public function Close(){
  222. switch ($this->Connection['Db_Type']) {
  223. case 'mysql':
  224. $this->MySQLi->close();
  225. break;
  226. case 'mssql':
  227. mssql_close($this->DBConnection);
  228. break;
  229. case 'sqlserv':
  230. sqlsrv_close($this->DBConnection);
  231. break;
  232. case 'pgsql':
  233. pg_close($this->DBConnection);
  234. break;
  235. }
  236. }
  237. // Support Functions
  238. private function PrintReport($String){
  239. return '<p><b>T-Rex Engine Report:</b> '. $String . '</p>';
  240. }
  241. private function Connect(){
  242. switch ($this->Connection['Db_Type']) {
  243. case 'mysql':
  244. $this->MySQLi = new MySQLi(
  245. $this->Connection['Host'] ,
  246. $this->Connection['User'] ,
  247. $this->Connection['Password'] ,
  248. $this->Connection['Database']
  249. )
  250. or trigger_error(
  251. $this->PrintReport(MySQLi_connect_error()),
  252. E_USER_ERROR
  253. );
  254. break;
  255. case 'mssql':
  256. $this->DBConnection = mssql_connect(
  257. $this->Connection['Host'] ,
  258. $this->Connection['User'] ,
  259. $this->Connection['Password']
  260. )
  261. or trigger_error(
  262. $this->PrintReport(mssql_get_last_message()),
  263. E_USER_ERROR
  264. );
  265. mssql_select_db($this->Connection['Database'], $this->DBConnection);
  266. break;
  267. case 'sqlserv':
  268. $this->DBConnection = sqlsrv_connect(
  269. $this->Connection['Host'],
  270. array(
  271. "Database" => $this->Connection['Database'] ,
  272. 'UID' => $this->Connection['User'] ,
  273. 'PWD' => $this->Connection['Password']
  274. )
  275. )
  276. or trigger_error(
  277. $this->PrintReport( print_r(sqlsrv_errors()) ),
  278. E_USER_ERROR
  279. );
  280. break;
  281. case 'pgsql':
  282. $this->DBConnection = pg_connect(
  283. "host=" . $this->Connection['Host'] .
  284. "dbname=" . $this->Connection['Database'] .
  285. "user=" . $this->Connection['User'] .
  286. "password=" . $this->Connection['Password']
  287. )
  288. or trigger_error(
  289. $this->PrintReport(pg_last_error()),
  290. E_USER_ERROR
  291. );
  292. break;
  293. }
  294. }
  295. private function ErrorHandling(){
  296. switch ($this->Connection['Db_Type']) {
  297. case 'mysql':
  298. if ( ! empty( $this->MySQLi->error ) )
  299. return trigger_error(
  300. $this->PrintReport($this->MySQLi->error),
  301. E_USER_ERROR
  302. );
  303. break;
  304. case 'mssql':
  305. if (!empty(mssql_get_last_message()))
  306. return trigger_error(
  307. $this->PrintReport(mssql_get_last_message()),
  308. E_USER_ERROR
  309. );
  310. break;
  311. case 'pgsql':
  312. if (!empty(pg_last_error($this->DBConnection)))
  313. return trigger_error(
  314. $this->PrintReport(pg_last_error($this->DBConnection)),
  315. E_USER_ERROR
  316. );
  317. break;
  318. case 'sqlserv':
  319. if (!empty(sqlsrv_errors()))
  320. return trigger_error(
  321. $this->PrintReport(print_r(sqlsrv_errors())),
  322. E_USER_ERROR
  323. );
  324. break;
  325. }
  326. }
  327. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement