PageRenderTime 116ms CodeModel.GetById 40ms RepoModel.GetById 1ms app.codeStats 0ms

/PHP - Server/helper/DBOperation.php

https://bitbucket.org/systech3/hbms
PHP | 516 lines | 345 code | 49 blank | 122 comment | 52 complexity | 00444a676251b91eb13205677f87c9a3 MD5 | raw file
  1. <?php
  2. include("config.php");
  3. //print_r($_SESSION);
  4. /*
  5. if(empty($_SESSION['userlog']['id'])){
  6. header("Location: login.php");
  7. }
  8. */
  9. //require_once("../config/config.php");
  10. /**
  11. * PHP Class for handling database operation with MySql Database.
  12. */
  13. class DBOperation
  14. {
  15. // Class Variable
  16. var $dbCon; // Holds pointer to Database Connection
  17. var $operationError; // Stores error occur during performing database operation.
  18. /*
  19. Contstructor Making connection with database using the default values.
  20. */
  21. function DBOperation()
  22. {
  23. $this->initializeConnection(DB_NAME, DB_HOST, DB_USERNAME, DB_PASSWORD);
  24. //$this->initializeConnection("DATABASE NAME", "SERVER NAME", "USER NAME", "PASSWORD");
  25. }
  26. /*
  27. Function with parameters DatabaseName, MySQL ServerName, MySQL User, Password to access user previledges.
  28. Make connection with database using the parameters.
  29. */
  30. function initializeConnection($dbName, $server, $userName, $password)
  31. {
  32. // Get Connection
  33. $this->dbCon = mysql_connect($server, $userName, $password) or die("Cannot create connection");
  34. // Select a database to work on.
  35. mysql_select_db($dbName, $this->dbCon) or die("not connected");
  36. }
  37. /**
  38. Function to Create table.
  39. Parameter
  40. $tableName : Name of table to create
  41. $fieldValueType : Array with keyvalue pair of Column Name and their Datatype.
  42. Check for isError after calling function to get result of operation.
  43. */
  44. function createTable($tableName,$fieldValueType)
  45. {
  46. $this->operationError = "";
  47. $nameType = "";
  48. while($array_cell = each($fieldValueType))
  49. {
  50. $nameType = $nameType . "`" . $array_cell['key'] . "` " . $array_cell['value'] . ",";
  51. }
  52. $nameType = substr($nameType,0,strlen($nameType)-1);
  53. $query = "create table $tableName ($nameType)";
  54. $result = mysql_query($query);
  55. if(mysql_errno($this->dbCon))
  56. {
  57. $this->operationError = mysql_error($this->dbCon);
  58. return;
  59. }
  60. }
  61. /**
  62. Function to drop table
  63. Parameter
  64. $tableName : Name of table to be dropped.
  65. Check for isError after calling the function to get result of operation.
  66. */
  67. function removeTable($tableName)
  68. {
  69. $this->operationError = "";
  70. mysql_query("drop table $tableName");
  71. if(mysql_errno($this->dbCon))
  72. {
  73. $this->operationError = mysql_error($this->dbCon);
  74. return;
  75. }
  76. }
  77. /**
  78. Function to insert record into table
  79. Parameter
  80. $tableName : Name of table to be dropped.
  81. $nameValueArray : ColumnName and Corresponding values to be inserted.
  82. User "'" with values for string type column e.g. "'New Record Field'".
  83. Check for isError after calling the function to get result of operation.
  84. */
  85. function insertRecords($tableName, $nameValueArray)
  86. {
  87. $name = "";
  88. $values = "";
  89. $this->operationError = "";
  90. if(is_array($nameValueArray))
  91. {
  92. while($array_cell = each($nameValueArray))
  93. {
  94. $name = $name . "`" . $array_cell['key'] . "`,";
  95. $values = $values ."'". $array_cell['value'] . "',";
  96. }
  97. $name = substr($name,0,strlen($name)-1);
  98. $values = substr($values,0,strlen($values)-1);
  99. $query = "insert into $tableName ($name) values($values)";
  100. //echo "<BR><BR>Query =".$query."<BR><BR>";
  101. $result = mysql_query($query);
  102. return $result;
  103. }
  104. else
  105. {
  106. $this->operationError = "No value to insert";
  107. }
  108. }
  109. /*
  110. Function to remove records from table
  111. Parameter
  112. $tableName : Name of table to be dropped.
  113. $nameValueConditionArray : Name of columns and correponding values for limiting the deletion of records
  114. $flagOrAnd : 0 => OR, 1=> AND
  115. Check for isError after calling the function to get result of operation.
  116. */
  117. function deleteRecords($tableName, $nameValueConditionArray, $flagOrAnd)
  118. {
  119. $currentName = "";
  120. $currentValue = "";
  121. $this->operationError = "";
  122. $condition = "";
  123. if(is_array($nameValueConditionArray))
  124. {
  125. while($array_cell = each($nameValueConditionArray))
  126. {
  127. $condition = "`" . $array_cell['key'] . "`" . " = " . $array_cell['value'];
  128. }
  129. }
  130. if($condition == "")
  131. $query = "delete from $tableName";
  132. else
  133. $query = "delete from $tableName where $condition ";
  134. //print($query);
  135. $result = mysql_query($query);
  136. if(mysql_errno($this->dbCon))
  137. {
  138. $this->operationError = mysql_error($this->dbCon);
  139. return;
  140. }
  141. }
  142. /*
  143. Function to update records of table
  144. Parameter
  145. $tableName : Name of table to be dropped.
  146. $nameValueArray : Name of columns and correponding values to update
  147. $conditionArray : Name of columns and correponding values for limiting the updation of records
  148. $flagOrAnd : 0 => OR, 1=> AND
  149. Check for isError after calling the function to get result of operation.
  150. */
  151. function updateRecords($tableName, $nameValueArray, $conditionArray, $flagOrAnd)
  152. {
  153. $setCondition = "";
  154. $this->operationError = "";
  155. while($array_cell = each($nameValueArray))
  156. {
  157. $setCondition = $setCondition . "`" . $array_cell['key'] . "` = " . $array_cell['value'] . ",";
  158. }
  159. $setCondition = substr($setCondition, 0, strlen($setCondition)-1);
  160. if(isset($flagOrAnd))
  161. {
  162. if($flagOrAnd == 1)
  163. $flagOrAnd = "and";
  164. else
  165. $flagOrAnd = "or";
  166. }
  167. else
  168. $flagOrAnd = "or";
  169. $whereCondition = "";
  170. if (is_array($conditionArray))
  171. {
  172. while($array_cell = each($conditionArray))
  173. {
  174. $whereCondition = $whereCondition . " `" . $array_cell['key'] . "` = " . $array_cell['value'] . " " . $flagOrAnd;
  175. }
  176. $whereCondition = substr($whereCondition, 0, strlen($whereCondition) - strlen($flagOrAnd));
  177. }
  178. if($whereCondition == "")
  179. {
  180. $query = "update $tableName set $setCondition";
  181. }
  182. else
  183. {
  184. $query = "update $tableName set $setCondition where $whereCondition";
  185. }
  186. $result = mysql_query($query);
  187. //print("<BR><BR>".$query);
  188. if(mysql_errno($this->dbCon))
  189. {
  190. $this->operationError = mysql_error($this->dbCon);
  191. return;
  192. }
  193. }
  194. /*
  195. Function to get records from table
  196. Parameter
  197. $tableName : Name of table to be dropped.
  198. $requiredFields : Name of columns for which data is to be fetched, leave empty if all columns are required
  199. $fieldNameCondition : Name of columns and correponding values for filtering the selection of records
  200. $limitFieldsCondition: limit the number of record to be fetched ( default = 20)
  201. $flagOrAnd : 0 => OR, 1=> AND ( default = 0 )
  202. Check for isError after calling the function to get result of operation.
  203. Return-Type : recordset if query executed successfully.
  204. */
  205. function getRecords($tableName, $requiredFields, $fieldNameCondition, $limitFieldsCondition, $flagOrAnd)
  206. {
  207. $this->operationError = "";
  208. if(isset($limitFieldsCondition) && $limitFieldsCondition !="")
  209. {
  210. $limitFieldsCondition = $limitFieldsCondition;
  211. }
  212. else
  213. {
  214. $limitFieldsCondition = "";
  215. }
  216. //if(isset($limitFieldsCondition) || $limitFieldsCondition == "")
  217. // $limitFieldsCondition = "0, 20";
  218. $whereCondition = "";
  219. if(isset($flagOrAnd))
  220. {
  221. if($flagOrAnd == 1)
  222. $flagOrAnd = "and";
  223. else
  224. $flagOrAnd = "or";
  225. }
  226. else
  227. $flagOrAnd = "or";
  228. if(is_array($fieldNameCondition))
  229. {
  230. while($array_cell = each($fieldNameCondition))
  231. {
  232. $colName = $array_cell['key'];
  233. if(strpos($colName," ",0) > 0)
  234. $colName = "`" . $array_cell['key']. "`";
  235. $whereCondition = $whereCondition . " " . $colName . " = " . $array_cell['value'] . " " . $flagOrAnd;
  236. }
  237. $whereCondition = substr($whereCondition, 0,(strlen($whereCondition) - strlen($flagOrAnd)));
  238. }
  239. $requireName = "";
  240. if(is_array($requiredFields))
  241. {
  242. while($array_cell = each($requiredFields))
  243. {
  244. $colName = $array_cell['value'];
  245. $colValue = $array_cell['key'];
  246. /*if(strpos($colName, " ", 0) > 0)
  247. $colName = "`" . $colName . "`";
  248. if(strpos($colValue, " ", 0) > 0)
  249. $colValue = "`" . $colValue . "`";
  250. */
  251. $requireName = $requireName . $colName . " as " . $colValue . ",";
  252. }
  253. if($requireName == "")
  254. $requireName = " * ";
  255. }
  256. else
  257. {
  258. $requireName = " * ";
  259. }
  260. //$whereCondition = substr($whereCondition,0,strlen($whereCondition)-1);
  261. $requireName = substr($requireName,0,strlen($requireName)-1);
  262. if ($whereCondition == "")
  263. {
  264. if ($limitFieldsCondition == "")
  265. {
  266. $query = "select $requireName from $tableName ";
  267. }
  268. else
  269. {
  270. $query = "select $requireName from $tableName limit $limitFieldsCondition";
  271. }
  272. }
  273. else
  274. {
  275. if ($limitFieldsCondition == "")
  276. {
  277. $query = "select $requireName from $tableName where $whereCondition ";
  278. }
  279. else
  280. {
  281. $query = "select $requireName from $tableName where $whereCondition limit $limitFieldsCondition";
  282. }
  283. }
  284. //print("<BR>Query " . $query."<BR>");
  285. $result = mysql_query($query);
  286. if(mysql_errno($this->dbCon))
  287. {
  288. $this->operationError = mysql_error($this->dbCon);
  289. return;
  290. }
  291. return $result;
  292. }
  293. /**
  294. Count the number of records fetched.
  295. Paramter : $recordset : Recordset
  296. Return-Type : Integer => Number of records.
  297. */
  298. function recordCount($recordSet)
  299. {
  300. return mysql_num_rows($recordSet);
  301. }
  302. /**
  303. Check whether the recordset is empty or not.
  304. Paramter : $recordset : Recordset
  305. Return-Type : Boolean => false if records available.
  306. */
  307. function isEmpty($recordSet)
  308. {
  309. // return count($recordSet) == 0;
  310. return mysql_num_rows($recordSet) == 0;
  311. }
  312. /**
  313. Execute query
  314. Paramter :
  315. $query => SQL-Text to perform database operation.
  316. $queryType => Type of query
  317. Return-Type : based on Result of operaion.
  318. Check for isError after calling the function
  319. */
  320. function executeQuery($query,$queryType)
  321. {
  322. $this->operationError = "";
  323. $result = mysql_query($query);
  324. if(mysql_errno($this->dbCon))
  325. {
  326. $this->operationError = mysql_error($this->dbCon);
  327. return;
  328. }
  329. $arr = mysql_fetch_array($result);
  330. return $arr;
  331. }
  332. /**
  333. Check whether there is error in sql operation performed.
  334. Return-Type : Boolean => true if error occurs.
  335. */
  336. function isError()
  337. {
  338. return $this->operationError;
  339. }
  340. /**
  341. Print the error message
  342. */
  343. function printError()
  344. {
  345. print "<br> Error : " . $this->operationError;
  346. }
  347. /**
  348. Check whether the table is empty or not.
  349. Parameter : $tableName => Name of table.
  350. Return-Type : Boolean => True if table is empty
  351. Check for isError after calling the function
  352. */
  353. function isTableEmpty($tableName)
  354. {
  355. $this->operationError = "";
  356. $query = "select count(*) as numCount from $tableName";
  357. $result = mysql_query($query);
  358. if(mysql_errno($this->dbCon))
  359. {
  360. $this->operationError = mysql_error($this->dbCon);
  361. return;
  362. }
  363. return $result['numCount'] == 0;
  364. }
  365. function numField($result)
  366. {
  367. return mysql_num_fields($result);
  368. }
  369. /*
  370. Close database connection.
  371. */
  372. function closeConnection()
  373. {
  374. mysql_close($this->dbCon);
  375. }
  376. function getInsertId()
  377. {
  378. return mysql_insert_id();
  379. }
  380. // function getLocateRecord($tableName, $requiredFields, $fieldNameCondition, $limitFieldsCondition, $flagOrAnd,$fieldName)
  381. function getLocateRecord($tableName,$requiredFields,$fieldNameCondition,$delimiter,$fieldName,$limitFieldsConditionString,$flagOrAnd)
  382. {
  383. $this->operationError = "";
  384. $searchWordString = "";
  385. $limitFieldsCondition="";
  386. $searchWordArr = array();
  387. $searchWordArr = explode($delimiter,$fieldNameCondition);
  388. if(isset($limitFieldsConditionString) && $limitFieldsConditionString !="")
  389. {
  390. $limitFieldsCondition = $limitFieldsConditionString;
  391. }
  392. else
  393. {
  394. $limitFieldsCondition = "";
  395. }
  396. $whereCondition = "";
  397. if(isset($flagOrAnd))
  398. {
  399. if($flagOrAnd == 1)
  400. $flagOrAnd = "and";
  401. else
  402. $flagOrAnd = "or";
  403. }
  404. else
  405. $flagOrAnd = "or";
  406. if(is_array($searchWordArr))
  407. {
  408. $whereCondition = " Visible=1 and";
  409. //$whereCondition = $whereCondition." (Category_ID in ('Select Category_ID FROM category_info WHERE (Category_Visible = 1)'))) and";
  410. for($i=0;$i<sizeof($searchWordArr);$i++)
  411. {
  412. $searchWordString = $searchWordArr[$i];
  413. //$whereCondition = $whereCondition . " " . $colName . " = " . $array_cell['value'] . " " . $flagOrAnd;
  414. $whereCondition = $whereCondition . " Locate('".$searchWordString. "',".$fieldName.") ". $flagOrAnd;
  415. }
  416. $whereCondition = substr($whereCondition, 0,(strlen($whereCondition) - strlen($flagOrAnd)));
  417. }
  418. $requireName = "";
  419. if(is_array($requiredFields))
  420. {
  421. while($array_cell = each($requiredFields))
  422. {
  423. $colName = $array_cell['value'];
  424. $colValue = $array_cell['key'];
  425. }
  426. if($requireName == "")
  427. $requireName = " * ";
  428. }
  429. else
  430. {
  431. $requireName = " * ";
  432. }
  433. //$whereCondition = substr($whereCondition,0,strlen($whereCondition)-1);
  434. $requireName = substr($requireName,0,strlen($requireName)-1);
  435. if ($whereCondition == "")
  436. {
  437. if ($limitFieldsCondition == "")
  438. {
  439. $query = "select $requireName from $tableName";
  440. }
  441. else
  442. {
  443. $query = "select $requireName from $tableName limit $limitFieldsCondition";
  444. }
  445. }
  446. else
  447. {
  448. if ($limitFieldsCondition == "")
  449. {
  450. $query = "select $requireName from $tableName where $whereCondition";
  451. }
  452. else
  453. {
  454. $query = "select $requireName from $tableName where $whereCondition limit $limitFieldsCondition";
  455. }
  456. }
  457. //print("<BR>Query " . $query."<BR>");
  458. $result = mysql_query($query);
  459. if(mysql_errno($this->dbCon))
  460. {
  461. $this->operationError = mysql_error($this->dbCon);
  462. return;
  463. }
  464. return $result;
  465. }
  466. }
  467. ?>