Posted By

coevolve on 03/05/10


Tagged

mysql database sql php mssql Oracle


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

nerdfiles


LoneDatabaseClass


 / Published in: PHP
 

  1. /*
  2.  
  3. LoneDatabaseClass * PHP SQL class for MySQL, Oracle and MS SQL
  4. ref: lonedb ver: 0.5 date:20100305
  5. Copyright (c) 2009 Milan Stankovic
  6. [email protected], [email protected]
  7. License: MIT, http://www.opensource.org/licenses/mit-license.php
  8.  
  9. ____________________________________________________________________________
  10. todo:
  11. ora: bind and execute always
  12. ora: insert -add sequence name
  13.  
  14. ____________________________________________________________________________
  15. changes:
  16.  
  17. 20100305
  18. stable mssql support
  19. 20100120
  20. mssql support
  21. 20091210
  22. merged my and ora versions
  23.  
  24. ____________________________________________________________________________
  25. public:
  26.  
  27. $con, $rcount, $keys, $mode
  28. select($table, $cols = '*', $where = null, $order = null, $opt = 0)
  29. insert($table, $values, $cols = null, $filename='', $filefield='')
  30. delete($table, $where = null)
  31. update($table, $where, $values, $filename='', $filefield='')
  32. selectAssociative($table, $cols = '*', $where = null, $order = null, $opt = 0)
  33. getResult()
  34. setConnection($host, $user, $pass, $dbname)
  35. setDatabase($dbname)
  36. disconnect()
  37. cleanup($data, $write = false)
  38. errDetails($rsc=null)
  39.  
  40. ____________________________________________________________________________
  41. private:
  42.  
  43. $db_host, $db_user, $db_pass, $db_name, $result, $oracon, $orast
  44. connect()
  45. tableExists($table)
  46. cleanupSlashes($data, $write = false)
  47. ms_ : select(), update(), insert(), delete(), selectAssociative()
  48. my_ : select(), update(), insert(), delete(), selectAssociative()
  49. ora_ : select(), update(), insert(), delete(), selectAssociative(), updateFile(), insertFile()
  50.  
  51. ____________________________________________________________________________
  52. notes:
  53.  
  54. All methods return true if completed successfully and false if an error occurred, except getResult and cleanup.
  55. On setConnection, setDatabase, insert, delete and update, result is empty array, on disconnect result is intact.
  56. For update and insert, filename and filefield used in oracle mode for file handling (bind mode).
  57.  
  58. ____________________________________________________________________________
  59.  
  60. */
  61.  
  62.  
  63. class LoneDatabaseClass {
  64.  
  65. var $db_host = ''; // use setConnection()
  66. var $db_user = '';
  67. var $db_pass = '';
  68. var $db_name = '';
  69. var $result = array(); // use getResult()
  70. var $con = false; // connection active flag
  71. var $rcount = -1;
  72. var $keys = array();
  73. var $oracon = null;
  74. var $orast = null;
  75. var $mode = '';
  76.  
  77.  
  78. function select($table, $cols = '*', $where = null, $order = null, $opt = 0) {
  79. // required: table: table name
  80. // optional: cols: columns requested, comma separated
  81. // where: expresion as a string
  82. // order: 'column DESC' expresion as a string
  83. // opt: oracle options: OCI_BOTH | ( OCI_ASSOC | OCI_NUM ) & OCI_RETURN_NULLS & OCI_RETURN_LOBS
  84.  
  85. if (!$this->tableExists($table)) { return false; }
  86. $q = 'SELECT '.$cols.' FROM '.$table;
  87. if ($where != null) { $q .= ' WHERE '.$where; }
  88. if ($order != null) { $q .= ' ORDER BY '.$order; }
  89. if ($this->mode=='mysql') {
  90. return $this->my_select($q);
  91. }
  92. else if ($this->mode=='mssql') {
  93. return $this->ms_select($q);
  94. }
  95. else if ($this->mode=='oracle') {
  96. return $this->ora_select($q, $opt);
  97. }
  98. return false;
  99. }
  100.  
  101. function insert($table, $values, $cols = null, $filename='', $filefield='') {
  102. // required: table: table name
  103. // values: values as array
  104. // optional: cols: column names, comma separated
  105. // filename, filefield: oracle file handling
  106.  
  107. if (!$this->tableExists($table)) { return false; }
  108. if ($this->mode=='mysql') {
  109. return $this->my_insert($table, $values, $cols);
  110. }
  111. else if ($this->mode=='mssql') {
  112. return $this->ms_insert($table, $values, $cols);
  113. }
  114. else if ($this->mode=='oracle') {
  115. if ($filename=='') {
  116. return $this->ora_insert($table, $values, $cols);
  117. }
  118. else {
  119. return $this->ora_insertFile($table, $values, $cols, $filename, $filefield);
  120. }
  121. }
  122. return false;
  123. }
  124.  
  125. function update($table, $where, $values, $filename='', $filefield='') {
  126. // required: table: tabel name
  127. // where: expresion as a string
  128. // values: values as array, column names as keys
  129. // filename, filefield: oracle file handling
  130.  
  131. if (!$this->tableExists($table)) { return false; }
  132. if ($where=='' || empty($values)) { return false; }
  133. if ($this->mode=='mysql') {
  134. return $this->my_update($table, $where, $values);
  135. }
  136. else if ($this->mode=='mssql') {
  137. return $this->ms_update($table, $where, $values);
  138. }
  139. if ($this->mode=='oracle') {
  140. if ($filename=='') {
  141. return $this->ora_update($table, $where, $values);
  142. }
  143. else {
  144. return $this->ora_updateFile($table, $where, $values, $filename, $filefield);
  145. }
  146. }
  147. return false;
  148. }
  149.  
  150. function delete($table, $where = null) {
  151.  
  152. if (!$this->tableExists($table)) { return false; }
  153. if ($where == null) { return false; }
  154. if ($this->mode=='mysql') {
  155. return $this->my_delete($table, $where);
  156. }
  157. else if ($this->mode=='mssql') {
  158. return $this->ms_delete($table, $where);
  159. }
  160. else if ($this->mode=='oracle') {
  161. return $this->ora_delete($table, $where);
  162. }
  163. return false;
  164. }
  165.  
  166. function selectAssociative($table, $keycolumn, $cols = '*', $where = null, $order = null, $opt = 0) {
  167.  
  168. if (!$this->tableExists($table)) { return false; }
  169. $q = 'SELECT '.$cols.' FROM '.$table;
  170. if ($where != null) { $q .= ' WHERE '.$where; }
  171. if ($order != null) { $q .= ' ORDER BY '.$order; }
  172. if ($this->mode=='mysql') {
  173. return $this->my_selectAssociative($q, $keycolumn);
  174. }
  175. else if ($this->mode=='oracle') {
  176. return $this->ora_selectAssociative($q, $keycolumn, $opt);
  177. }
  178. else if ($this->mode=='mssql') {
  179. return $this->ms_selectAssociative($q, $keycolumn, $table);
  180. }
  181. return false;
  182. }
  183.  
  184.  
  185. // ____________________________________________________________________________
  186.  
  187. function ms_select($q) { // mssql select
  188.  
  189. //echo $q;
  190.  
  191. $query = mssql_query($q);
  192. if (!$query) { return false; }
  193.  
  194. $this->rcount = mssql_num_rows($query);
  195. if ($this->rcount < 1) { return true; }
  196.  
  197. for ($i = 0; $i < $this->rcount; $i++) {
  198. $r = mssql_fetch_array($query, MSSQL_ASSOC);
  199. $this->keys = array_keys($r);
  200. $kcount = count($this->keys);
  201. for ($x = 0; $x < $kcount; $x++) {
  202.  
  203. //$v= iconv('UCS-2LE', 'UTF-8//IGNORE', $r[$this->keys[$x]]);
  204.  
  205. $v = $r[$this->keys[$x]];
  206.  
  207. if($this->rcount > 1) { $this->result[$i][$this->keys[$x]] = $v; }
  208. else { $this->result[$this->keys[$x]] = $r[$this->keys[$x]]; }
  209. }
  210. }
  211. return true;
  212. }
  213.  
  214. function ms_update($table, $where, $values) { // mssql update
  215.  
  216. $keys = array_keys($values);
  217. $vcount = count($values);
  218. $q = '';
  219. for ($i = 0; $i < $vcount; $i++) {
  220. if ($q!=''){ $q .= ','; }
  221. $q .= $keys[$i].'='.$values[$keys[$i]];
  222. }
  223. $q = 'UPDATE '.$table.' SET '.$q;
  224. $q .= ' WHERE '.$where;
  225. $ok = mssql_query($q);
  226. return $ok;
  227. }
  228.  
  229. function ms_insert($table, $values, $cols) { // mssql insert
  230.  
  231. $q = 'INSERT INTO '.$table;
  232. if ($cols != null) { $q .= ' ('.$cols.')'; }
  233. $values = implode(',',$values);
  234. $q .= ' VALUES ('.$values.')';
  235. $ok = mssql_query($q);
  236. return $ok;
  237. }
  238.  
  239.  
  240. function ms_selectAssociative($q, $keycolumn, $table) { // mssql select associative
  241.  
  242. $query = mssql_query($q);
  243. if (!$query) { return false;}
  244. $this->rcount = mssql_num_rows($query);
  245. if ($this->rcount < 1) { return true; }
  246. $doconv = $this->columnsToConvert($table);
  247. for ($i = 0; $i < $this->rcount; $i++) {
  248. $r = mssql_fetch_array($query, MSSQL_ASSOC);
  249. $this->keys = array_keys($r);
  250. if (!in_array($keycolumn, $this->keys)) { return false; }
  251. $kcount = count($this->keys);
  252. for ($x = 0; $x < $kcount; $x++) {
  253. if ($doconv[$this->keys[$x]]) {
  254. $v= iconv('UCS-2LE', 'UTF-8//IGNORE', $r[$this->keys[$x]]);
  255. }
  256. else {
  257. $v = $r[$this->keys[$x]];
  258. }
  259. $this->result[$r[$keycolumn]][$this->keys[$x]] = $v;
  260. }
  261. }
  262. return true;
  263. }
  264.  
  265. function ms_delete($table, $where) { // mssql delete
  266.  
  267. $q = 'DELETE FROM '.$table.' WHERE '.$where;
  268. $ok = mssql_query($q);
  269. return $ok;
  270. }
  271.  
  272. // ____________________________________________________________________________
  273.  
  274.  
  275. function my_select($q) { // mysql select
  276.  
  277. $query = mysql_query('SET CHARACTER SET utf8;');
  278. $query = mysql_query($q);
  279. if (!$query) { return false;}
  280. $this->rcount = mysql_num_rows($query);
  281. if ($this->rcount < 1) { return true; }
  282. for ($i = 0; $i < $this->rcount; $i++) {
  283. $r = mysql_fetch_array($query);
  284. $this->keys = array_keys($r);
  285. $kcount = count($this->keys);
  286. for ($x = 0; $x < $kcount; $x++) {
  287. if (!is_int($this->keys[$x])) { // Sanitizes keys so only alphavalues are allowed
  288. if($this->rcount > 1) { $this->result[$i][$this->keys[$x]] = $r[$this->keys[$x]]; }
  289. else { $this->result[$this->keys[$x]] = $r[$this->keys[$x]]; }
  290. }
  291. else {
  292. unset($this->keys[$x]);
  293. }
  294. }
  295. }
  296. return true;
  297. }
  298.  
  299. function my_update($table, $where, $values) { // mysql update
  300.  
  301. $keys = array_keys($values);
  302. $vcount = count($values);
  303. $q = '';
  304. for ($i = 0; $i < $vcount; $i++) {
  305. if ($q==''){ $q = $keys[$i].'='.$values[$keys[$i]]; }
  306. else { $q .= ','.$keys[$i].'='.$values[$keys[$i]]; }
  307. }
  308. $q = 'UPDATE '.$table.' SET '.$q;
  309. $q .= ' WHERE '.$where;
  310. $query = mysql_query('SET CHARACTER SET utf8;');
  311. $query = mysql_query($q);
  312. if ($query) { return true; }
  313. else { return false; }
  314. }
  315.  
  316. function my_insert($table, $values, $cols) { // mysql insert
  317.  
  318. $q = 'INSERT INTO '.$table;
  319. if ($cols != null) { $q .= ' ('.$cols.')'; }
  320. $values = implode(',',$values);
  321. $q .= ' VALUES ('.$values.')';
  322. $ok = mysql_query('SET CHARACTER SET utf8;');
  323. if ($ok) { $ok = mysql_query($q); }
  324. return $ok;
  325. }
  326.  
  327. function my_selectAssociative($q, $keycolumn) { // mysql select associative
  328.  
  329. $query = mysql_query('SET CHARACTER SET utf8;');
  330. $query = mysql_query($q);
  331. if (!$query) { return false;}
  332. $this->rcount = mysql_num_rows($query);
  333. if ($this->rcount < 1) { return true; }
  334. for ($i = 0; $i < $this->rcount; $i++) {
  335. $r = mysql_fetch_array($query);
  336. $this->keys = array_keys($r);
  337. if (!in_array($keycolumn, $this->keys)) { return false; }
  338. $kcount = count($this->keys);
  339. for ($x = 0; $x < $kcount; $x++) {
  340. if (!is_int($this->keys[$x])) { // Sanitizes keys so only alphavalues are allowed
  341. $this->result[$r[$keycolumn]][$this->keys[$x]] = $r[$this->keys[$x]];
  342. }
  343. else {
  344. unset($this->keys[$x]);
  345. }
  346. }
  347. }
  348. return true;
  349. }
  350.  
  351. function my_delete($table, $where) { // mysql delete
  352.  
  353. $q = 'DELETE FROM '.$table.' WHERE '.$where;
  354. $ok = mysql_query('SET CHARACTER SET utf8;');
  355. if ($ok) { $ok = mysql_query($q); }
  356. return $ok;
  357. }
  358.  
  359. // ____________________________________________________________________________
  360.  
  361.  
  362. function ora_select($q, $opt) { // oracle select
  363.  
  364. $this->orast = oci_parse($this->oracon, $q);
  365. if (!$this->orast) { $this->errDetails($this->oracon); return false; }
  366. if (!oci_execute($this->orast)) { $this->errDetails($this->orast); return false; }
  367. $oramode = OCI_ASSOC+OCI_RETURN_NULLS+OCI_RETURN_LOBS;
  368. if ($opt!=0) { $oramode = $opt; }
  369. $this->rcount = 0;
  370. while ($r = oci_fetch_array($this->orast, $oramode)) {
  371. if (empty($this->keys)) {
  372. $this->keys = array_keys($r);
  373. $kcount = count($this->keys);
  374. }
  375. for ($x = 0; $x < $kcount; $x++) {
  376. if (is_int($this->keys[$x])) { return false; } // only alphavalues are allowed, OCI_ASSOC
  377. $this->result[$this->rcount][$this->keys[$x]] = $r[$this->keys[$x]];
  378. }
  379. $this->rcount++;
  380. }
  381. if($this->rcount == 1) { // columns as keys on one
  382. $t = $this->result[0];
  383. $this->result = array();
  384. for ($x = 0; $x < $kcount; $x++) {
  385. $this->result[$this->keys[$x]]=$t[$this->keys[$x]];
  386. }
  387. }
  388. return true;
  389. }
  390.  
  391. function ora_update($table, $where, $values) { // oracle update
  392.  
  393. $keys = array_keys($values);
  394. $vcount = count($values);
  395. $q = '';
  396. for ($i = 0; $i < $vcount; $i++) {
  397. if ($q==''){ $q = $keys[$i].'='.$values[$keys[$i]]; }
  398. else { $q .= ','.$keys[$i].'='.$values[$keys[$i]]; }
  399. }
  400. $q = 'UPDATE '.$table.' SET '.$q;
  401. $q .= ' WHERE '.$where;
  402. $this->orast = oci_parse($this->oracon, $q);
  403. if (!$this->orast) { $this->errDetails($this->oracon); return false; }
  404. if (!oci_execute($this->orast)) { $this->errDetails($this->orast); return false; }
  405. return true;
  406. }
  407.  
  408. function ora_updateFile($table, $where, $values, $filename, $filefield) { // oracle update file
  409.  
  410. $keys = array_keys($values);
  411. $vcount = count($values);
  412. $q = '';
  413. for ($i = 0; $i < $vcount; $i++) {
  414. $pair = $keys[$i].'= '.$values[$keys[$i]];
  415. if ($keys[$i]==$filefield) { $pair=$filefield.'= empty_blob()'; }
  416. if ($q==''){ $q = $pair; }
  417. else { $q .= ','.$pair; }
  418. }
  419. $q = 'UPDATE '.$table.' SET '.$q;
  420. $q .= ' WHERE '.$where;
  421. $q .= ' returning '.$filefield.' into :blobdata';
  422. $this->orast = oci_parse($this->oracon, $q);
  423. if (!$this->orast) { $this->errDetails($this->oracon); return false; }
  424. $lob = oci_new_descriptor($this->oracon, OCI_D_LOB);
  425. oci_bind_by_name($this->orast, ':blobdata', $lob, -1, OCI_B_BLOB);
  426. if (!oci_execute($this->orast, OCI_DEFAULT)) { $this->errDetails($this->orast); return false; }
  427. $lob->import($filename);
  428. oci_commit($this->oracon);
  429. $lob->close();
  430. return true;
  431. }
  432.  
  433. function ora_insert($table, $values, $cols) { // oracle insert
  434. global $oraseq;
  435.  
  436. $q = 'INSERT INTO '.$table;
  437. if ($cols != null) { $q .= ' (ID,'.$cols.')'; }
  438. $values = implode(',',$values);
  439. $q .= ' VALUES ('.$oraseq.'.NextVal, '.$values.')';
  440. $this->orast = oci_parse($this->oracon, $q);
  441. if (!$this->orast) { $this->errDetails($this->oracon); return false; }
  442. if (!oci_execute($this->orast)) { $this->errDetails($this->orast); return false; }
  443. return true;
  444. }
  445.  
  446. function ora_insertFile($table, $values, $cols, $filename, $filefield) { // oracle insert file
  447. global $oraseq;
  448.  
  449. $keys = array_keys($values);
  450. $vcount = count($values);
  451. $q = 'INSERT INTO '.$table;
  452. if ($cols != null) { $q .= ' (ID,'.$cols.')'; }
  453. $q .= ' VALUES ('.$oraseq.'.NextVal';
  454. for ($i = 0; $i < $vcount; $i++) {
  455. if ($keys[$i]==$filefield) { $q .= ',empty_blob()'; }
  456. else {$q .= ','.$values[$keys[$i]]; }
  457. }
  458. $q .= ') returning '.$filefield.' into :blobdata';
  459. $this->orast = oci_parse($this->oracon, $q);
  460. if (!$this->orast) { $this->errDetails($this->oracon); return false; }
  461. $lob = oci_new_descriptor($this->oracon, OCI_D_LOB);
  462. oci_bind_by_name($this->orast, ':blobdata', $lob, -1, OCI_B_BLOB);
  463. if (!oci_execute($this->orast, OCI_DEFAULT)) { $this->errDetails($this->orast); return false; }
  464. $lob->import($filename);
  465. oci_commit($this->oracon);
  466. $lob->close();
  467. return true;
  468. }
  469.  
  470. function ora_selectAssociative($q, $keycolumn, $opt) { // oracle select associative
  471.  
  472. $this->orast = oci_parse($this->oracon, $q);
  473. if (!$this->orast) { $this->errDetails($this->oracon); return false; }
  474. if (!oci_execute($this->orast)) { $this->errDetails($this->orast); return false; }
  475. $oramode = OCI_ASSOC+OCI_RETURN_NULLS;
  476. if ($opt!=0) { $oramode = $opt; }
  477. $this->rcount = 0;
  478. while ($r = oci_fetch_array($this->orast, $oramode)) {
  479. if (empty($this->keys)) {
  480. $this->keys = array_keys($r);
  481. $kcount = count($this->keys);
  482. if (!in_array($keycolumn, $this->keys)) { return false; }
  483. }
  484. for ($x = 0; $x < $kcount; $x++) {
  485. if (is_int($this->keys[$x])) { return false; } // only alphavalues are allowed, OCI_ASSOC
  486. $this->result[$r[$keycolumn]][$this->keys[$x]] = $r[$this->keys[$x]];
  487. }
  488. $this->rcount++;
  489. }
  490. return true;
  491. }
  492.  
  493. function ora_delete($table, $where) { // oracle delete
  494.  
  495. $q = 'DELETE FROM '.$table.' WHERE '.$where;
  496. $this->orast = oci_parse($this->oracon, $q);
  497. if (!$this->orast) { $this->errDetails($this->oracon); return false; }
  498. if (!oci_execute($this->orast)) { $this->errDetails($this->orast); return false; }
  499. return true;
  500. }
  501.  
  502. // ____________________________________________________________________________
  503.  
  504.  
  505. function getResult() {
  506. return $this->result;
  507. }
  508.  
  509. function setConnection($host, $user, $pass, $dbname) {
  510. global $dbmode;
  511.  
  512. $this->mode = $dbmode;
  513. if ($this->con) {
  514. $ok = $this->disconnect();
  515. if(!$ok) { return false; }
  516. }
  517. $this->oracon = null;
  518. $this->orast = null;
  519. $this->con = false;
  520. $this->rcount = -1;
  521. $this->keys = array();
  522. $this->result = array();
  523. $this->db_host = $host;
  524. $this->db_user = $user;
  525. $this->db_pass = $pass;
  526. $this->db_name = $dbname;
  527. $this->result = array();
  528. $ok = $this->connect(); // $this->con==true on success
  529. return $ok;
  530. }
  531.  
  532.  
  533. function setDatabase($dbname) {
  534. // change database
  535. global $dbmode;
  536.  
  537. $this->mode = $dbmode;
  538. if ($this->con) {
  539. $ok = $this->disconnect();
  540. if(!$ok) return false;
  541. }
  542. $this->con = false;
  543. $this->rcount = -1;
  544. $this->keys = array();
  545. $this->result = array();
  546. $this->db_name = $dbname;
  547. $ok = $this->connect(); // $this->con==true on success
  548. return $ok;
  549. }
  550.  
  551. function disconnect() {
  552.  
  553. $ok = false;
  554. if ($this->mode=='mysql') {
  555. $ok = mysql_close();
  556. }
  557. else if ($this->mode=='mssql') {
  558. $ok = mssql_close();
  559. }
  560. else if ($this->mode=='oracle') {
  561. if ($this->orast) { $ok = oci_free_statement($this->orast); }
  562. if ($ok && $this->oracon) { $ok = oci_close($this->oracon); }
  563. }
  564. $this->con = !$ok;
  565. return $ok;
  566. }
  567.  
  568. function connect() {
  569.  
  570. if ($this->db_host=='' || $this->db_user=='' || $this->db_pass=='' || $this->db_name=='') { return false; }
  571.  
  572. $ok = false;
  573. if ($this->mode=='mysql') {
  574. $dbh = mysql_connect($this->db_host,$this->db_user,$this->db_pass);
  575. if ($dbh) { $ok = mysql_select_db($this->db_name, $dbh); }
  576. }
  577. else if ($this->mode=='oracle') {
  578. $this->oracon = oci_connect($this->db_user,$this->db_pass,$this->db_host.'/'.$this->db_name);
  579. if (!$this->oracon) { $this->errDetails(); }
  580. else { $ok = true; }
  581. }
  582. else if ($this->mode=='mssql') {
  583. $dbh = mssql_connect($this->db_host,$this->db_user,$this->db_pass);
  584. if ($dbh) { $ok = mssql_select_db($this->db_name, $dbh); }
  585. }
  586. $this->con = $ok;
  587. return $ok;
  588. }
  589.  
  590. function tableExists($table) {
  591.  
  592. if (!$this->con || $table=='') { return false; }
  593. $this->result = array(); // resets
  594. $this->rcount = -1;
  595. $this->keys = array();
  596.  
  597. $ok = false;
  598.  
  599. if ($this->mode=='mysql') {
  600. $q = 'SHOW TABLES FROM '.$this->db_name.' LIKE "'.$table.'"';
  601. $q = mysql_query($q);
  602. if ($q) {
  603. if (mysql_num_rows($q)==1) { $ok = true; }
  604. }
  605. }
  606. else if ($this->mode=='oracle') {
  607. $q = "select * from all_objects where object_type in ('TABLE','VIEW') and object_name = '$table'";
  608. $this->orast = oci_parse($this->oracon, $q);
  609. if (!$this->orast) { $this->errDetails($this->oracon); }
  610. else if (!oci_execute($this->orast)) { $this->errDetails($this->orast); }
  611. else { $ok = true; }
  612. }
  613. else if ($this->mode=='mssql') {
  614. if (strpos($table, 'INNER JOIN')!=-1) { return true;}
  615. $table = str_replace(array('[',']'), '', $table);
  616. $q = "SELECT name FROM sys.sysobjects WHERE name='$table' AND OBJECTPROPERTY(id,'IsUserTable')=1";
  617. $q = mssql_query($q);
  618. if ($q) {
  619. if (mssql_num_rows($q)==1) { $ok = true; }
  620. }
  621. }
  622. return $ok;
  623. }
  624.  
  625. // ____________________________________________________________________________
  626.  
  627.  
  628. function cleanup($data, $write = false) {
  629.  
  630. if (is_array($data)) {
  631. foreach ($data as $key => $value) {
  632. $data[$key] = $this->cleanupSlashes($value, $write);
  633. }
  634. } else {
  635. $data = $this->cleanupSlashes($data, $write);
  636. }
  637. return $data;
  638. }
  639.  
  640. function cleanupSlashes($data, $write = false) {
  641.  
  642. if (isset($data)) { // preserve null
  643. $data = stripslashes($data);
  644. }
  645. if ($write && $this->mode=='mysql') {
  646. $data = mysql_real_escape_string($data);
  647. }
  648. }
  649. return $data;
  650. }
  651.  
  652. function errDetails($rsc=null) {
  653. global $debug;
  654.  
  655. if ($debug!==true) { return true; }
  656. if ($rsc==null) { $err = oci_error(); }
  657. else { $err = oci_error($rsc); }
  658. echo '<br /><font color="red"><strong>Oracle Error Details for '. $err['code'];
  659. echo '<br />message = ' . $err['message'];
  660. echo '<br />position = ' . $err['offset'];
  661. echo '<br />statement = ' . $err['sqltext'];
  662. echo '</strong></font><br /><br />';
  663. return true;
  664. }
  665.  
  666. // ____________________________________________________________________________
  667. // MS SQL UCS-2 text read/write,
  668. // declare fields in model: convert(varbinary(2*size), fieldname) as fieldname
  669.  
  670. function columnsToConvert($table) {
  671. // result as [ sColname=>bDoConv ]
  672. global $fields;
  673.  
  674. if (isset($fields[$table])) {
  675. $cols = array_keys($fields[$table]);
  676. }
  677. else {
  678. return array('itemkey'=>false, 'value'=>true);
  679. }
  680. $ret = array();
  681. foreach ($cols as $k=>$f) {
  682. if ($fields[$table][$f]['type']=='ntext') {
  683. $ret[$f] = true;
  684. }
  685. else {
  686. $ret[$f] = false;
  687. }
  688. }
  689. return $ret;
  690. }
  691.  
  692. /* not used */
  693.  
  694. function columnsDeclare($table) {
  695. // result as [ sColname=>declaration ]
  696. global $fields;
  697.  
  698. if (isset($fields[$table])) {
  699. $cols = array_keys($fields[$table]);
  700. }
  701. else {
  702. return array('itemkey'=>'itemkey', 'value'=>'convert(varbinary(200), value) as value');
  703. }
  704. $ret = array();
  705. foreach ($cols as $k=>$f) {
  706. $ret[$f] = $fields[$table][$f]['colname'];
  707. }
  708. return $ret;
  709. }
  710.  
  711. function encUCS2($str) {
  712. $ucs2 = iconv('UTF-8', 'UCS-2LE', $str);
  713. // converting UCS-2 string into "binary" hexadecimal form
  714. $arr = unpack('H*hex', $ucs2);
  715. return "0x{$arr['hex']}"; // include it in sql statement without quotes
  716. }
  717.  
  718. // iconv('UCS-2LE', 'UTF-8', $str) to decode
  719.  
  720. function ucs2html($str) {
  721. $str=trim($str);
  722. $len=strlen($str);
  723. $html='';
  724. for($i=0;$i<$len;$i+=2)
  725. $html.='&#'.hexdec(dechex(ord($str[$i+1])).sprintf("%02s",dechex(ord($str[$i])))).';';
  726. return($html);
  727. }
  728.  
  729. function hex2utf($UtfCharInHex) {
  730. $OutputChar = "";
  731. $UtfCharInDec = hexdec($UtfCharInHex);
  732. if($UtfCharInDec < 128) $OutputChar .= chr($UtfCharInDec);
  733. else if($UtfCharInDec < 2048) $OutputChar .= chr(($UtfCharInDec >> 6) + 192) . chr(($UtfCharInDec & 63) + 128);
  734. else if($UtfCharInDec < 65536) $OutputChar .= chr(($UtfCharInDec >> 12) + 224) . chr((($UtfCharInDec >> 6) & 63) + 128) . chr(($UtfCharInDec & 63) + 128);
  735. else if($UtfCharInDec < 2097152) $OutputChar .= chr($UtfCharInDec >> 18 + 240) . chr((($UtfCharInDec >> 12) & 63) + 128) . chr(($UtfCharInDec >> 6) & 63 + 128) . chr($UtfCharInDec & 63 + 128);
  736. return $OutputChar;
  737. }
  738.  
  739. }

Report this snippet  

You need to login to post a comment.