PageRenderTime 58ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/images/class.db.php

https://bitbucket.org/muzamilqadir/buildium
PHP | 575 lines | 357 code | 150 blank | 68 comment | 51 complexity | f6f1df0231315e704f5f2af1d98d2c9d MD5 | raw file
  1. <?php
  2. // ==================================================================
  3. define("EZSQL_DB_USER", "devmcom_demodb"); // <-- mysql db user
  4. define("EZSQL_DB_PASSWORD", "bismillah"); // <-- mysql db password
  5. define("EZSQL_DB_NAME", "devmcom_realestate"); // <-- mysql db pname
  6. define("EZSQL_DB_HOST", "localhost"); // <-- mysql server host
  7. define("EZSQL_VERSION","1.01");
  8. define("OBJECT","OBJECT",true);
  9. define("ARRAY_A","ARRAY_A",true);
  10. define("ARRAY_N","ARRAY_N",true);
  11. // ==================================================================
  12. // The Main Class
  13. class db {
  14. // ==================================================================
  15. // DB Constructor - connects to the server and selects a database
  16. function db()
  17. {
  18. $this->dbh = @mysql_connect(EZSQL_DB_HOST,EZSQL_DB_USER,EZSQL_DB_PASSWORD);
  19. if ( ! $this->dbh )
  20. {
  21. $this->print_error("<ol><b>Error establishing a database connection!</b><li>Are you sure you have the correct user/password?<li>Are you sure that you have typed the correct hostname?<li>Are you sure that the database server is running?</ol>");
  22. }
  23. $this->select(EZSQL_DB_NAME);
  24. }
  25. // ==================================================================
  26. // Select a DB (if another one needs to be selected)
  27. function select($db)
  28. {
  29. if ( !@mysql_select_db($db,$this->dbh))
  30. {
  31. $this->print_error("<ol><b>Error selecting database <u>$db</u>!</b><li>Are you sure it exists?<li>Are you sure there is a valid database connection?</ol>");
  32. }
  33. }
  34. // ==================================================================
  35. // Print SQL/DB error.
  36. function print_error($str = "")
  37. {
  38. if ( !$str ) $str = mysql_error();
  39. // If there is an error then take note of it
  40. print "<blockquote><font face=arial size=2 color=ff0000>";
  41. print "<b>SQL/DB Error --</b> ";
  42. print "[<font color=000077>$str</font>]";
  43. print "</font></blockquote>";
  44. }
  45. // ==================================================================
  46. // Basic Query - see docs for more detail
  47. function query($query, $output = OBJECT)
  48. {
  49. // Log how the function was called
  50. $this->func_call = "\$db->query(\"$query\", $output)";
  51. // Kill this
  52. $this->last_result = null;
  53. $this->col_info = null;
  54. // Keep track of the last query for debug..
  55. $this->last_query = $query;
  56. // Perform the query via std mysql_query function..
  57. $this->result = mysql_query($query,$this->dbh);
  58. if ( mysql_error() )
  59. {
  60. // If there is an error then take note of it..
  61. $this->print_error();
  62. }
  63. else
  64. {
  65. // In other words if this was a select statement..
  66. if ( $this->result )
  67. {
  68. // =======================================================
  69. // Take note of column info
  70. $i=0;
  71. while ($i < @mysql_num_fields($this->result))
  72. {
  73. $this->col_info[$i] = @mysql_fetch_field($this->result);
  74. $i++;
  75. }
  76. // =======================================================
  77. // Store Query Results
  78. $i=0;
  79. while ( $row = @mysql_fetch_object($this->result) )
  80. {
  81. // Store relults as an objects within main array
  82. $this->last_result[$i] = $row;
  83. $i++;
  84. }
  85. @mysql_free_result($this->result);
  86. // If there were results then return true for $db->query
  87. if ( $i )
  88. {
  89. return true;
  90. }
  91. else
  92. {
  93. return false;
  94. }
  95. }
  96. }
  97. }
  98. function ExeQuersys($qry)
  99. {
  100. //echo $qry;
  101. $exe = mysql_query($qry);
  102. return $exe;
  103. }
  104. function NumRows($qry)
  105. {
  106. //echo $qry;
  107. $exe = $this->ExeQuersys($qry);
  108. $nof = mysql_num_rows($exe);
  109. return $nof;
  110. }
  111. // ==================================================================
  112. // Get one variable from the DB - see docs for more detail
  113. function get_var($query=null,$x=0,$y=0)
  114. {
  115. // Log how the function was called
  116. $this->func_call = "\$db->get_var(\"$query\",$x,$y)";
  117. // If there is a query then perform it if not then use cached results..
  118. if ( $query )
  119. {
  120. $this->query($query);
  121. }
  122. // Extract var out of cached results based x,y vals
  123. if ( $this->last_result[$y] )
  124. {
  125. $values = array_values(get_object_vars($this->last_result[$y]));
  126. }
  127. // If there is a value return it else return null
  128. return $values[$x]?$values[$x]:null;
  129. }
  130. // ==================================================================
  131. // Get one row from the DB - see docs for more detail
  132. function get_row($query=null,$y=0,$output=OBJECT)
  133. {
  134. // Log how the function was called
  135. $this->func_call = "\$db->get_row(\"$query\",$y,$output)";
  136. // If there is a query then perform it if not then use cached results..
  137. if ( $query )
  138. {
  139. $this->query($query);
  140. }
  141. // If the output is an object then return object using the row offset..
  142. if ( $output == OBJECT )
  143. {
  144. return $this->last_result[$y]?$this->last_result[$y]:null;
  145. }
  146. // If the output is an associative array then return row as such..
  147. elseif ( $output == ARRAY_A )
  148. {
  149. return $this->last_result[$y]?get_object_vars($this->last_result[$y]):null;
  150. }
  151. // If the output is an numerical array then return row as such..
  152. elseif ( $output == ARRAY_N )
  153. {
  154. return $this->last_result[$y]?array_values(get_object_vars($this->last_result[$y])):null;
  155. }
  156. // If invalid output type was specified..
  157. else
  158. {
  159. $this->print_error(" \$db->get_row(string query,int offset,output type) -- Output type must be one of: OBJECT, ARRAY_A, ARRAY_N ");
  160. }
  161. }
  162. // ==================================================================
  163. // Function to get 1 column from the cached result set based in X index
  164. // se docs for usage and info
  165. function get_col($query=null,$x=0)
  166. {
  167. // If there is a query then perform it if not then use cached results..
  168. if ( $query )
  169. {
  170. $this->query($query);
  171. }
  172. // Extract the column values
  173. for ( $i=0; $i < count($this->last_result); $i++ )
  174. {
  175. $new_array[$i] = $this->get_var(null,$x,$i);
  176. }
  177. return $new_array;
  178. }
  179. // ==================================================================
  180. // Return the the query as a result set - see docs for more details
  181. function get_results($query=null, $output = OBJECT)
  182. {
  183. // Log how the function was called
  184. $this->func_call = "\$db->get_results(\"$query\", $output)";
  185. // If there is a query then perform it if not then use cached results..
  186. if ( $query )
  187. {
  188. $this->query($query);
  189. }
  190. // Send back array of objects. Each row is an object
  191. if ( $output == OBJECT )
  192. {
  193. return $this->last_result;
  194. }
  195. elseif ( $output == ARRAY_A || $output == ARRAY_N )
  196. {
  197. if ( $this->last_result )
  198. {
  199. $i=0;
  200. foreach( $this->last_result as $row )
  201. {
  202. $new_array[$i] = get_object_vars($row);
  203. if ( $output == ARRAY_N )
  204. {
  205. $new_array[$i] = array_values($new_array[$i]);
  206. }
  207. $i++;
  208. }
  209. return $new_array;
  210. }
  211. else
  212. {
  213. return null;
  214. }
  215. }
  216. }
  217. // ==================================================================
  218. // Function to get column meta data info pertaining to the last query
  219. // see docs for more info and usage
  220. function get_col_info($info_type="name",$col_offset=-1)
  221. {
  222. if ( $this->col_info )
  223. {
  224. if ( $col_offset == -1 )
  225. {
  226. $i=0;
  227. foreach($this->col_info as $col )
  228. {
  229. $new_array[$i] = $col->{$info_type};
  230. $i++;
  231. }
  232. return $new_array;
  233. }
  234. else
  235. {
  236. return $this->col_info[$col_offset]->{$info_type};
  237. }
  238. }
  239. }
  240. // ==================================================================
  241. // Dumps the contents of any input variable to screen in a nicely
  242. // formatted and easy to understand way - any type: Object, Var or Array
  243. function vardump($mixed)
  244. {
  245. echo "<blockquote><font color=000090>";
  246. echo "<pre><font face=arial>";
  247. if ( ! $this->vardump_called )
  248. {
  249. echo "<font color=800080><b>ezSQL</b> (v".EZSQL_VERSION.") <b>Variable Dump..</b></font>\n\n";
  250. }
  251. print_r($mixed);
  252. echo "\n\n<b>Last Query:</b> ".($this->last_query?$this->last_query:"NULL")."\n";
  253. echo "<b>Last Function Call:</b> " . ($this->func_call?$this->func_call:"None")."\n";
  254. echo "<b>Last Rows Returned:</b> ".count($this->last_result)."\n";
  255. echo "</font></pre></font></blockquote>";
  256. echo "\n<hr size=1 noshade color=dddddd>";
  257. $this->vardump_called = true;
  258. }
  259. // Alias for the above function
  260. function dumpvars($mixed)
  261. {
  262. $this->vardump($mixed);
  263. }
  264. // ==================================================================
  265. // Displays the last query string that was sent to the database & a
  266. // table listing results (if there were any).
  267. // (abstracted into a seperate file to save server overhead).
  268. function debug()
  269. {
  270. echo "<blockquote>";
  271. // Only show ezSQL credits once..
  272. if ( ! $this->debug_called )
  273. {
  274. echo "<font color=800080 face=arial size=2><b>ezSQL</b> (v".EZSQL_VERSION.") <b>Debug..</b></font><p>\n";
  275. }
  276. echo "<font face=arial size=2 color=000099><b>Query --</b> ";
  277. echo "[<font color=000000><b>$this->last_query</b></font>]</font><p>";
  278. echo "<font face=arial size=2 color=000099><b>Query Result..</b></font>";
  279. echo "<blockquote>";
  280. if ( $this->col_info )
  281. {
  282. // =====================================================
  283. // Results top rows
  284. echo "<table cellpadding=5 cellspacing=1 bgcolor=555555>";
  285. echo "<tr bgcolor=eeeeee><td nowrap valign=bottom><font color=555599 face=arial size=2><b>(row)</b></font></td>";
  286. for ( $i=0; $i < count($this->col_info); $i++ )
  287. {
  288. echo "<td nowrap align=left valign=top><font size=1 color=555599 face=arial>{$this->col_info[$i]->type} {$this->col_info[$i]->max_length}<br><font size=2><b>{$this->col_info[$i]->name}</b></font></td>";
  289. }
  290. echo "</tr>";
  291. // ======================================================
  292. // print main results
  293. if ( $this->last_result )
  294. {
  295. $i=0;
  296. foreach ( $this->get_results(null,ARRAY_N) as $one_row )
  297. {
  298. $i++;
  299. echo "<tr bgcolor=ffffff><td bgcolor=eeeeee nowrap align=middle><font size=2 color=555599 face=arial>$i</font></td>";
  300. foreach ( $one_row as $item )
  301. {
  302. echo "<td nowrap><font face=arial size=2>$item</font></td>";
  303. }
  304. echo "</tr>";
  305. }
  306. } // if last result
  307. else
  308. {
  309. echo "<tr bgcolor=ffffff><td colspan=".(count($this->col_info)+1)."><font face=arial size=2>No Results</font></td></tr>";
  310. }
  311. echo "</table>";
  312. } // if col_info
  313. else
  314. {
  315. echo "<font face=arial size=2>No Results</font>";
  316. }
  317. echo "</blockquote></blockquote><hr noshade color=dddddd size=1>";
  318. $this->debug_called = true;
  319. }
  320. ///////////////////////////INSERt//////////////////////////////////
  321. function iInsert($table, $postData = array()){
  322. $q = "DESC $table";
  323. $q = mysql_query($q);
  324. $getFields = array();
  325. while ($field = mysql_fetch_array($q)){
  326. $getFields[sizeof($getFields)] = $field['Field'];
  327. }
  328. $fields = "";
  329. $values = "";
  330. if (sizeof($getFields) > 0){
  331. foreach($getFields as $k){
  332. if (isset($postData[$k])){
  333. $postData[$k] = mysql_real_escape_string($postData[$k]);
  334. $fields .= "`$k`, ";
  335. $values .= "'$postData[$k]', ";
  336. }
  337. }
  338. $fields = substr($fields, 0, strlen($fields) - 2);
  339. $values = substr($values, 0, strlen($values) - 2);
  340. $insert = "INSERT INTO $table ($fields) VALUES ($values)";
  341. if (mysql_query($insert)){
  342. return true;
  343. }else{
  344. echo mysql_error();
  345. return false;
  346. }
  347. }else{
  348. return false;
  349. }
  350. }
  351. ///////////////////////////Update//////////////////////////////////
  352. function iUpdate($table, $postData = array(), $conditions = array()){
  353. $q = "DESC $table";
  354. $q = mysql_query($q);
  355. $getFields = array();
  356. while ($field = mysql_fetch_array($q)){
  357. $getFields[sizeof($getFields)] = $field['Field'];
  358. }
  359. $values = "";
  360. $conds = "";
  361. if (sizeof($getFields) > 0){
  362. foreach($getFields as $k){
  363. if (isset($postData[$k])){
  364. $postData[$k] = mysql_real_escape_string($postData[$k]);
  365. $values .= "`$k` = '$postData[$k]', ";
  366. }
  367. }
  368. $values = substr($values, 0, strlen($values) - 2);
  369. foreach($conditions as $k => $v){
  370. $v = mysql_real_escape_string($v);
  371. $conds .= "`$k` = '$v'";
  372. }
  373. $update = "UPDATE $table SET $values WHERE $conds";
  374. if (mysql_query($update)){
  375. return true;
  376. }else{
  377. echo mysql_error();
  378. return false;
  379. }
  380. }else{
  381. return false;
  382. }
  383. }
  384. function iDisconnect(){
  385. if (mysql_close($this->db_conn)){
  386. return true;
  387. }else{
  388. echo mysql_error();
  389. return false;
  390. }
  391. }
  392. function SelectDrop($qry,$sel="",$value = "")
  393. {
  394. $result = $this->ExeQuersys($qry);
  395. while($row=mysql_fetch_array($result))
  396. {
  397. if($sel!= "")
  398. {
  399. if($sel == $row[0]){$sl = 'selected="selected"';}else{$sl = '';}
  400. }
  401. if($value!= "")
  402. {
  403. $lab = $row[$value];
  404. }else{ $lab = $row[1]; }
  405. $tr .= ' <option value="'.$row[0].'" '.$sl.' >'.$lab.'</option>';
  406. }
  407. return $tr;
  408. }
  409. function MultiSelectDrop($qry,$sel="",$value = "")
  410. {
  411. $result = $this->ExeQuersys($qry);
  412. while($row=mysql_fetch_array($result))
  413. {
  414. if($sel!= "")
  415. {
  416. $select = $this->get_row("select * from userstates where user_id = '".$sel."' ");
  417. if($select->state == $row[0]){$sl .= 'selected="selected"';}else{$sl = '';}
  418. }
  419. if($value!= ""){$lab = $row[$value];}else{ $lab = $row[1]; }
  420. $tr .= ' <option value="'.$row[0].'" '.$sl.' >'.$lab.'</option>';
  421. }
  422. return $tr;
  423. }
  424. }
  425. ?>