Guest User

Untitled

a guest
Jul 22nd, 2018
432
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.90 KB | None | 0 0
  1. <?php
  2.  
  3. /*
  4.  
  5. * Session Management for PHP3
  6.  
  7. *
  8.  
  9. * Copyright (c) 1998-2000 NetUSE AG
  10.  
  11. * Boris Erdmann, Kristian Koehntopp
  12.  
  13. *
  14.  
  15. * $Id: db_mysql.inc,v 1.2 2000/07/12 18:22:34 kk Exp $
  16.  
  17. *
  18.  
  19. */
  20.  
  21.  
  22.  
  23. class DB_Sql {
  24.  
  25.  
  26.  
  27. /* public: connection parameters */
  28.  
  29. var $Host = "";
  30.  
  31. var $Database = "";
  32.  
  33. var $User = "";
  34.  
  35. var $Password = "";
  36.  
  37.  
  38.  
  39. /* public: configuration parameters */
  40.  
  41. var $Auto_Free = 0; ## Set to 1 for automatic mysql_free_result()
  42.  
  43. var $Debug = 0; ## Set to 1 for debugging messages.
  44.  
  45. var $Halt_On_Error = "yes"; ## "yes" (halt with message), "no" (ignore errors quietly), "report" (ignore errror, but spit a warning)
  46.  
  47. var $Seq_Table = "db_sequence";
  48.  
  49.  
  50.  
  51. /* public: result array and current row number */
  52.  
  53. var $Record = array();
  54.  
  55. var $Row;
  56.  
  57.  
  58.  
  59. /* public: current error number and error text */
  60.  
  61. var $Errno = 0;
  62.  
  63. var $Error = "";
  64.  
  65.  
  66.  
  67. /* public: this is an api revision, not a CVS revision. */
  68.  
  69. var $type = "mysql";
  70.  
  71. var $revision = "1.2";
  72.  
  73.  
  74.  
  75. /* private: link and query handles */
  76.  
  77. var $Link_ID = 0;
  78.  
  79. var $Query_ID = 0;
  80.  
  81.  
  82.  
  83.  
  84.  
  85.  
  86.  
  87. /* public: constructor */
  88.  
  89. function DB_Sql($query = "") {
  90.  
  91. $this->query($query);
  92.  
  93. }
  94.  
  95.  
  96.  
  97. /* public: some trivial reporting */
  98.  
  99. function link_id() {
  100.  
  101. return $this->Link_ID;
  102.  
  103. }
  104.  
  105.  
  106.  
  107. function query_id() {
  108.  
  109. return $this->Query_ID;
  110.  
  111. }
  112.  
  113.  
  114.  
  115. /* public: connection management */
  116.  
  117. function connect($Database = "", $Host = "", $User = "", $Password = "") {
  118.  
  119. /* Handle defaults */
  120.  
  121. if ("" == $Database)
  122.  
  123. $Database = $this->Database;
  124.  
  125. if ("" == $Host)
  126.  
  127. $Host = $this->Host;
  128.  
  129. if ("" == $User)
  130.  
  131. $User = $this->User;
  132.  
  133. if ("" == $Password)
  134.  
  135. $Password = $this->Password;
  136.  
  137.  
  138.  
  139. /* establish connection, select database */
  140.  
  141. if ( 0 == $this->Link_ID ) {
  142.  
  143. $this->Link_ID=mysql_pconnect($Host, $User, $Password);
  144.  
  145. if (!$this->Link_ID) {
  146.  
  147. $this->halt("connect($Host, $User, \$Password) failed.");
  148.  
  149. return 0;
  150.  
  151. }
  152.  
  153.  
  154.  
  155. if (!@mysql_select_db($Database,$this->Link_ID)) {
  156.  
  157. $this->halt("cannot use database ".$this->Database);
  158.  
  159. return 0;
  160.  
  161. }
  162.  
  163. }
  164.  
  165.  
  166.  
  167. return $this->Link_ID;
  168.  
  169. }
  170.  
  171.  
  172.  
  173. /* public: discard the query result */
  174.  
  175. function free() {
  176.  
  177. @mysql_free_result($this->Query_ID);
  178.  
  179. $this->Query_ID = 0;
  180.  
  181. }
  182.  
  183.  
  184.  
  185. /* public: perform a query */
  186.  
  187. function query($Query_String) {
  188.  
  189. /* No empty queries, please, since PHP4 chokes on them. */
  190.  
  191. if ($Query_String == "")
  192.  
  193. /* The empty query string is passed on from the constructor,
  194.  
  195. * when calling the class without a query, e.g. in situations
  196.  
  197. * like these: '$db = new DB_Sql_Subclass;'
  198.  
  199. */
  200.  
  201. return 0;
  202.  
  203.  
  204.  
  205. if (!$this->connect()) {
  206.  
  207. return 0; /* we already complained in connect() about that. */
  208.  
  209. };
  210.  
  211.  
  212.  
  213. # New query, discard previous result.
  214.  
  215. if ($this->Query_ID) {
  216.  
  217. $this->free();
  218.  
  219. }
  220.  
  221.  
  222.  
  223. if ($this->Debug)
  224.  
  225. printf("Debug: query = %s<br>\n", $Query_String);
  226.  
  227.  
  228.  
  229. $this->Query_ID = @mysql_query($Query_String,$this->Link_ID);
  230.  
  231. $this->Row = 0;
  232.  
  233. $this->Errno = mysql_errno();
  234.  
  235. $this->Error = mysql_error();
  236.  
  237. if (!$this->Query_ID) {
  238.  
  239. $this->halt("Invalid SQL: ".$Query_String);
  240.  
  241. }
  242.  
  243.  
  244.  
  245. # Will return nada if it fails. That's fine.
  246.  
  247. return $this->Query_ID;
  248.  
  249. }
  250.  
  251.  
  252.  
  253. /* public: walk result set */
  254.  
  255. function next_record() {
  256.  
  257. if (!$this->Query_ID) {
  258.  
  259. $this->halt("next_record called with no query pending.");
  260.  
  261. return 0;
  262.  
  263. }
  264.  
  265.  
  266.  
  267. $this->Record = @mysql_fetch_array($this->Query_ID);
  268.  
  269. $this->Row += 1;
  270.  
  271. $this->Errno = mysql_errno();
  272.  
  273. $this->Error = mysql_error();
  274.  
  275.  
  276.  
  277. $stat = is_array($this->Record);
  278.  
  279. if (!$stat && $this->Auto_Free) {
  280.  
  281. $this->free();
  282.  
  283. }
  284.  
  285. return $stat;
  286.  
  287. }
  288.  
  289.  
  290.  
  291. /* public: position in result set */
  292.  
  293. function seek($pos = 0) {
  294.  
  295. $status = @mysql_data_seek($this->Query_ID, $pos);
  296.  
  297. if ($status)
  298.  
  299. $this->Row = $pos;
  300.  
  301. else {
  302.  
  303. $this->halt("seek($pos) failed: result has ".$this->num_rows()." rows");
  304.  
  305.  
  306.  
  307. /* half assed attempt to save the day,
  308.  
  309. * but do not consider this documented or even
  310.  
  311. * desireable behaviour.
  312.  
  313. */
  314.  
  315. @mysql_data_seek($this->Query_ID, $this->num_rows());
  316.  
  317. $this->Row = $this->num_rows;
  318.  
  319. return 0;
  320.  
  321. }
  322.  
  323.  
  324.  
  325. return 1;
  326.  
  327. }
  328.  
  329.  
  330.  
  331. /* public: table locking */
  332.  
  333. function lock($table, $mode="write") {
  334.  
  335. $this->connect();
  336.  
  337.  
  338.  
  339. $query="lock tables ";
  340.  
  341. if (is_array($table)) {
  342.  
  343. while (list($key,$value)=each($table)) {
  344.  
  345. if ($key=="read" && $key!=0) {
  346.  
  347. $query.="$value read, ";
  348.  
  349. } else {
  350.  
  351. $query.="$value $mode, ";
  352.  
  353. }
  354.  
  355. }
  356.  
  357. $query=substr($query,0,-2);
  358.  
  359. } else {
  360.  
  361. $query.="$table $mode";
  362.  
  363. }
  364.  
  365. $res = @mysql_query($query, $this->Link_ID);
  366.  
  367. if (!$res) {
  368.  
  369. $this->halt("lock($table, $mode) failed.");
  370.  
  371. return 0;
  372.  
  373. }
  374.  
  375. return $res;
  376.  
  377. }
  378.  
  379.  
  380.  
  381. function unlock() {
  382.  
  383. $this->connect();
  384.  
  385.  
  386.  
  387. $res = @mysql_query("unlock tables");
  388.  
  389. if (!$res) {
  390.  
  391. $this->halt("unlock() failed.");
  392.  
  393. return 0;
  394.  
  395. }
  396.  
  397. return $res;
  398.  
  399. }
  400.  
  401.  
  402.  
  403.  
  404.  
  405. /* public: evaluate the result (size, width) */
  406.  
  407. function affected_rows() {
  408.  
  409. return @mysql_affected_rows($this->Link_ID);
  410.  
  411. }
  412.  
  413.  
  414.  
  415. function num_rows() {
  416.  
  417. return @mysql_num_rows($this->Query_ID);
  418.  
  419. }
  420.  
  421.  
  422.  
  423. function num_fields() {
  424.  
  425. return @mysql_num_fields($this->Query_ID);
  426.  
  427. }
  428.  
  429.  
  430.  
  431. /* public: shorthand notation */
  432.  
  433. function nf() {
  434.  
  435. return $this->num_rows();
  436.  
  437. }
  438.  
  439.  
  440.  
  441. function np() {
  442.  
  443. print $this->num_rows();
  444.  
  445. }
  446.  
  447.  
  448.  
  449. function f($Name) {
  450.  
  451. if(isset($this->Record[$Name]))
  452.  
  453. return $this->Record[$Name];
  454.  
  455. else
  456.  
  457. return "";
  458.  
  459. }
  460.  
  461.  
  462.  
  463. function p($Name) {
  464.  
  465. print $this->Record[$Name];
  466.  
  467. }
  468.  
  469.  
  470.  
  471. /* public: sequence numbers */
  472.  
  473. function nextid($seq_name) {
  474.  
  475. $this->connect();
  476.  
  477.  
  478.  
  479. if ($this->lock($this->Seq_Table)) {
  480.  
  481. /* get sequence number (locked) and increment */
  482.  
  483. $q = sprintf("select nextid from %s where seq_name = '%s'",
  484.  
  485. $this->Seq_Table,
  486.  
  487. $seq_name);
  488.  
  489. $id = @mysql_query($q, $this->Link_ID);
  490.  
  491. $res = @mysql_fetch_array($id);
  492.  
  493.  
  494.  
  495. /* No current value, make one */
  496.  
  497. if (!is_array($res)) {
  498.  
  499. $currentid = 0;
  500.  
  501. $q = sprintf("insert into %s values('%s', %s)",
  502.  
  503. $this->Seq_Table,
  504.  
  505. $seq_name,
  506.  
  507. $currentid);
  508.  
  509. $id = @mysql_query($q, $this->Link_ID);
  510.  
  511. } else {
  512.  
  513. $currentid = $res["nextid"];
  514.  
  515. }
  516.  
  517. $nextid = $currentid + 1;
  518.  
  519. $q = sprintf("update %s set nextid = '%s' where seq_name = '%s'",
  520.  
  521. $this->Seq_Table,
  522.  
  523. $nextid,
  524.  
  525. $seq_name);
  526.  
  527. $id = @mysql_query($q, $this->Link_ID);
  528.  
  529. $this->unlock();
  530.  
  531. } else {
  532.  
  533. $this->halt("cannot lock ".$this->Seq_Table." - has it been created?");
  534.  
  535. return 0;
  536.  
  537. }
  538.  
  539. return $nextid;
  540.  
  541. }
  542.  
  543.  
  544.  
  545. /* public: return table metadata */
  546.  
  547. function metadata($table='',$full=false) {
  548.  
  549. $count = 0;
  550.  
  551. $id = 0;
  552.  
  553. $res = array();
  554.  
  555.  
  556.  
  557. /*
  558.  
  559. * Due to compatibility problems with Table we changed the behavior
  560.  
  561. * of metadata();
  562.  
  563. * depending on $full, metadata returns the following values:
  564.  
  565. *
  566.  
  567. * - full is false (default):
  568.  
  569. * $result[]:
  570.  
  571. * [0]["table"] table name
  572.  
  573. * [0]["name"] field name
  574.  
  575. * [0]["type"] field type
  576.  
  577. * [0]["len"] field length
  578.  
  579. * [0]["flags"] field flags
  580.  
  581. *
  582.  
  583. * - full is true
  584.  
  585. * $result[]:
  586.  
  587. * ["num_fields"] number of metadata records
  588.  
  589. * [0]["table"] table name
  590.  
  591. * [0]["name"] field name
  592.  
  593. * [0]["type"] field type
  594.  
  595. * [0]["len"] field length
  596.  
  597. * [0]["flags"] field flags
  598.  
  599. * ["meta"][field name] index of field named "field name"
  600.  
  601. * The last one is used, if you have a field name, but no index.
  602.  
  603. * Test: if (isset($result['meta']['myfield'])) { ...
  604.  
  605. */
  606.  
  607.  
  608.  
  609. // if no $table specified, assume that we are working with a query
  610.  
  611. // result
  612.  
  613. if ($table) {
  614.  
  615. $this->connect();
  616.  
  617. $id = @mysql_list_fields($this->Database, $table);
  618.  
  619. if (!$id)
  620.  
  621. $this->halt("Metadata query failed.");
  622.  
  623. } else {
  624.  
  625. $id = $this->Query_ID;
  626.  
  627. if (!$id)
  628.  
  629. $this->halt("No query specified.");
  630.  
  631. }
  632.  
  633.  
  634.  
  635. $count = @mysql_num_fields($id);
  636.  
  637.  
  638.  
  639. // made this IF due to performance (one if is faster than $count if's)
  640.  
  641. if (!$full) {
  642.  
  643. for ($i=0; $i<$count; $i++) {
  644.  
  645. $res[$i]["table"] = @mysql_field_table ($id, $i);
  646.  
  647. $res[$i]["name"] = @mysql_field_name ($id, $i);
  648.  
  649. $res[$i]["type"] = @mysql_field_type ($id, $i);
  650.  
  651. $res[$i]["len"] = @mysql_field_len ($id, $i);
  652.  
  653. $res[$i]["flags"] = @mysql_field_flags ($id, $i);
  654.  
  655. }
  656.  
  657. } else { // full
  658.  
  659. $res["num_fields"]= $count;
  660.  
  661.  
  662.  
  663. for ($i=0; $i<$count; $i++) {
  664.  
  665. $res[$i]["table"] = @mysql_field_table ($id, $i);
  666.  
  667. $res[$i]["name"] = @mysql_field_name ($id, $i);
  668.  
  669. $res[$i]["type"] = @mysql_field_type ($id, $i);
  670.  
  671. $res[$i]["len"] = @mysql_field_len ($id, $i);
  672.  
  673. $res[$i]["flags"] = @mysql_field_flags ($id, $i);
  674.  
  675. $res["meta"][$res[$i]["name"]] = $i;
  676.  
  677. }
  678.  
  679. }
  680.  
  681.  
  682.  
  683. // free the result only if we were called on a table
  684.  
  685. if ($table) @mysql_free_result($id);
  686.  
  687. return $res;
  688.  
  689. }
  690.  
  691.  
  692.  
  693. /* private: error handling */
  694.  
  695. function halt($msg) {
  696.  
  697. $this->Error = @mysql_error($this->Link_ID);
  698.  
  699. $this->Errno = @mysql_errno($this->Link_ID);
  700.  
  701. if ($this->Halt_On_Error == "no")
  702.  
  703. return;
  704.  
  705.  
  706.  
  707. $this->haltmsg($msg);
  708.  
  709.  
  710.  
  711. if ($this->Halt_On_Error != "report")
  712.  
  713. die("Session halted.");
  714.  
  715. }
  716.  
  717.  
  718.  
  719. function haltmsg($msg) {
  720.  
  721. printf("</td></tr></table><b>Database error:</b> %s<br>\n", $msg);
  722.  
  723. printf("<b>MySQL Error</b>: %s (%s)<br>\n",
  724.  
  725. $this->Errno,
  726.  
  727. $this->Error);
  728.  
  729. }
  730.  
  731.  
  732.  
  733. function table_names() {
  734.  
  735. $this->query("SHOW TABLES");
  736.  
  737. $i=0;
  738.  
  739. while ($info=mysql_fetch_row($this->Query_ID))
  740.  
  741. {
  742.  
  743. $return[$i]["table_name"]= $info[0];
  744.  
  745. $return[$i]["tablespace_name"]=$this->Database;
  746.  
  747. $return[$i]["database"]=$this->Database;
  748.  
  749. $i++;
  750.  
  751. }
  752.  
  753. return $return;
  754.  
  755. }
  756.  
  757. }
  758.  
  759. ?>
Advertisement
Add Comment
Please, Sign In to add comment