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

/1/enp_baza_bezado/bd-2008-03-14 TO DOdo wykorzystania i dodania do klas/mysql_class_v1_1.php

http://htdocstest.googlecode.com/
PHP | 411 lines | 313 code | 69 blank | 29 comment | 42 complexity | 3a4aad1dd433f9346cc2588d89e731ed MD5 | raw file
Possible License(s): LGPL-2.1, BSD-3-Clause, BSD-2-Clause
  1. <?php
  2. /* version 1.1
  3. * DATABASE CONNECTION CLASS For MySQL
  4. * This program is under the BSD License
  5. * http://www.opensource.org/licenses/bsd-license.html
  6. *
  7. * Copyright (C) 2003, Fernando Ribeiro <r.fernando@netc.pt>
  8. * All rights reserved.
  9. *
  10. */
  11. class html {
  12. var $hide_name;
  13. var $size;
  14. function input_box($name,$type,$size='20',$value='',$maxlength=''){
  15. if (!is_int($size + 0)){
  16. $size =" size='20'";
  17. }else{
  18. $size =" size='$size'";
  19. }
  20. if (!is_int($maxlength + 0) || trim($maxlength) == '' ){
  21. $maxlength = '';
  22. }else{
  23. $maxlength=" maxlength='$maxlength'";
  24. }
  25. if (trim($type)=='') return false;
  26. switch ($type){
  27. case 'password': case 'text': case 'hidden': case 'checkbox':
  28. case 'radio': case 'submit' : case 'reset':
  29. $type=" type='".$type."'";
  30. break;
  31. default: return false;
  32. }
  33. if (trim($value)!=''){
  34. $value = " value='$value'";
  35. }else{
  36. $value='';
  37. }
  38. if(trim($name)=='') return false;
  39. $name1 = trim($name);
  40. $name1 = "name='$name1'";
  41. if ($this->hide_name == 0 ) $inpname = htmlentities(ucfirst($name));
  42. return ("<b> $inpname </b>&nbsp;<input $name1 $type $value $size $maxlength >\n");
  43. }
  44. function text_area($name,$type,$size,$value='',$maxlength=''){
  45. if(trim($size)!='' && substr_count($size,"x") == 1 ){
  46. $this->size=$size;
  47. }
  48. if ($this->size==''){
  49. $this->size ='6x50';
  50. }
  51. if ($data = explode ("x",$this->size)){
  52. $size =" rows='$data[0]' cols='$data[1]' ";
  53. }
  54. if (!is_int($maxlength + 0) || trim($maxlength) == ''){
  55. $maxlength = '';
  56. }else{
  57. $maxlength=" maxlength='$maxlength'";
  58. }
  59. if (trim($value)==''){
  60. }
  61. if(trim($name)=='') return false;
  62. $name1 = trim($name);
  63. $name1 = "name='$name1'";
  64. $inpname = htmlentities(ucfirst($name));
  65. return ("<b> $inpname </b><br><textarea $name1 $size $maxlength >$value </textarea>\n");
  66. }
  67. }
  68. class bd extends html {
  69. var $conn; /* keeps the connection handler*/
  70. var $dbase;
  71. var $_database;
  72. var $_user;
  73. var $_pass;
  74. var $_server;
  75. var $ep; /* define entry point */
  76. var $confile; /* configuration file */
  77. var $encoding; /* enconding used by the database */
  78. function ligarBD($_database,$_user,$_pass,$_server){
  79. $this->$_database=$_database;
  80. $this->$_user=$_user;
  81. $this->$_pass=$_pass;
  82. $this->$_server=$_server;
  83. $this->ep = "func";
  84. $this->connect();
  85. }
  86. /* function connect();
  87. * to be called from
  88. * subclasses to read a conf file and
  89. * connect to a database
  90. */
  91. function connect(){
  92. $this->confile = "http://localhost/traderd/db.conf";
  93. if ($this->ep != "func") $this->readconf($this->confile);
  94. if (!extension_loaded('mysql')) {
  95. if (!dl('mysql.so')) {
  96. exit("Unable to load extension.\n<br>");
  97. }
  98. }
  99. if (!@$this->conn = mysql_connect($this->_server,$this->_user,$this->_pass)) die ("I'm Sorry the MySQL Server could not be reached !");
  100. if ($this->conn<0) {
  101. return -1;
  102. }
  103. $this->dbase=$this->_database;
  104. if (mysql_select_db($this->dbase,$this->conn)==false){
  105. $this->error("ERROR TRYING TO IDENTIFY DATABASE, mysql_select_db(netcrash)");
  106. return -1 ;
  107. }
  108. $this->encoding = mysql_client_encoding($this->conn);
  109. }
  110. /* function readconf($file)
  111. * read the configuration file
  112. * style conf like:
  113. * server=localhost
  114. * user=Username
  115. * pass=password
  116. * database=mydatabase
  117. */
  118. function readconf($file){
  119. //if (!file_exists($file) || !is_readable($file)) die ("Error reading configuration file. ");
  120. $def = file($file);
  121. $SIZEDEF = sizeof($def);
  122. for ($i=0 ; $i < $SIZEDEF ; $i++){
  123. $this->parse($def[$i]);
  124. }
  125. }
  126. /* parse the line of configuration file */
  127. function parse($line){
  128. if (substr_count($line,"=") == 1 ){
  129. $def = explode("=",$line);
  130. $var = trim($def[0]); /* name of variable */
  131. $value = trim($def[1]); /* value of variable */
  132. switch ($var){
  133. case 'server': $this->_server = $value ;
  134. break;
  135. case 'user' : $this->_user = $value;
  136. break;
  137. case 'pass' : $this->_pass = $value;
  138. break;
  139. case 'database': $this->_database = $value;
  140. break;
  141. }
  142. }
  143. }
  144. function error($function){
  145. echo "<hr size='1' width='50%' height='1px'>";
  146. echo "<br>Error in function $function .<br>";
  147. echo "Description...<br>";
  148. echo "Error number ... ".mysql_errno()."<br>";
  149. echo mysql_error()."<br>";
  150. }
  151. function parseResultObj($resultado){
  152. $n_rows= mysql_num_rows($resultado);
  153. if ($n_rows > 0 ){
  154. unset($entidades);
  155. /* Inicializa o array $entidades */
  156. for($i=0; $i< $n_rows ; $i++)
  157. {
  158. $entidades[$i]=mysql_fetch_Object($resultado);
  159. }
  160. unset($resultado);
  161. return $entidades;
  162. /* Retorna o array $entidades */
  163. }
  164. }
  165. /* Not tested execSQL_toarray()*/
  166. function execSQL_toarray($sql_command){
  167. //echo $sql_command."<br>";
  168. $sql_command = mysql_real_escape_string($sql_command,$this->conn);
  169. if ($resultado = mysql_db_query($this->dbase,$sql_command, $this->conn)){
  170. //echo "\n - Resultado - ".$resultado." <br>";
  171. $n_rows= mysql_num_rows($resultado);
  172. $n_fields = mysql_num_fields($resultado);
  173. if (!$row = mysql_fetch_array($result, MYSQL_BOTH)){
  174. exit ($this->error("mysql_fetch_array() in execSQL_toarray"));
  175. }
  176. //if ($n_rows < 1 && $n_fields < 1 ) exit ("Unable to results");
  177. return $row;
  178. }else{
  179. $this->error("execSQL()");
  180. return false;
  181. }
  182. }
  183. function execSQL($sql_command) {
  184. if ($resultado = mysql_db_query($this->dbase,$sql_command, $this->conn)){
  185. return ($this->parseResultObj($resultado));
  186. }else{
  187. $this->error("execSQL()");
  188. return false;
  189. }
  190. }
  191. function exec($sql_command) {
  192. if ($resultado = mysql_db_query($this->dbase,$sql_command, $this->conn)){
  193. return $resultado;
  194. }else{
  195. $this->error("exec()");
  196. return false;
  197. }
  198. }
  199. /* lists the fields of a table into an array */
  200. function listfields($table){
  201. if (trim($table)=='') return false;
  202. $fields = mysql_list_fields($this->_database, $table, $this->conn);
  203. $n_col = mysql_num_fields($fields);
  204. for ($i = 0; $i < $n_col; $i++) {
  205. $field_array[$i] = mysql_field_name($fields, $i);
  206. }
  207. return $field_array;
  208. }
  209. /* Lists all fields of a table and lists it's values
  210. in a html table.
  211. */
  212. function listtable($table){
  213. if (!$fields = $this->listfields($table)) return false;
  214. $FIELDS = count($fields);
  215. echo "<table border='1' cellspacing='4' cellpadding='0'>\n";
  216. echo "<tr>\n";
  217. for ( $i = 0 ; $i < $FIELDS ; $i++){
  218. echo "<td> $fields[$i] </td>";
  219. }
  220. echo "</tr>\n";
  221. if (!$values = $this->execSQL("select * from ".$table)) return false;
  222. $VALUES = count ($values);
  223. $j=0;
  224. for ($i=0; $i < $VALUES ; $i++ ){
  225. echo "<tr>";
  226. $j=0;
  227. while ($j < $FIELDS ){ echo "<td valign='top'>".$values[$i]->$fields[$j]."</td>"; $j++; }
  228. echo "</tr>\n";
  229. }
  230. echo "</table>\n";
  231. }
  232. /* buildform($table[,$form_action,$method,$action])
  233. * $action is insert update based
  234. * $form_action is this example (action='index.php')
  235. * BETA TESTED WORKS GOOD ON SMALL CODE
  236. * using the name of the fields in a table builds a form.
  237. */
  238. function buildform($table,$form_action='',$method='post',$action='insert'){
  239. echo "<form action='$form_action' method='$method'>\n
  240. <table>\n";
  241. $text = array("tinyint","int","smallint","mediumint","integer",
  242. "bigint","double","float","real","decimal","dec","numeric","fixed",
  243. "char","varchar","string");
  244. $longtext = array ("tinyblob","tinytext","blob","text","mediumblob",
  245. "mediumtext","longblob","longtext");
  246. $table = trim ($table);
  247. if (!$fields = $this->listfields($table)) return false;
  248. $FIELDS = count($fields);
  249. $result = $this->exec("select * from $table ");
  250. for ($i=0; $i < $FIELDS ; $i++ ){
  251. $flags = mysql_field_flags($result,$i);
  252. $data = explode(" ",$flags);
  253. $type = mysql_field_type($result, $i);
  254. $type = strtolower($type);
  255. //echo $type." ".strlen($type)."\n<br>";
  256. if (array_search('primary_key',$data)) {
  257. $style='hidden';
  258. $this->hide_name = 1;
  259. echo $this->input_box($fields[$i],'hidden','','');
  260. $this->hide_name = 0;
  261. }else{
  262. unset($style);
  263. }
  264. if (!isset($style)){
  265. if (array_search($type,$text)) echo "<tr><td>".$this->input_box($fields[$i],'text','','')."</td></tr>";
  266. //input($name,$type,$size='20',$value,$maxlength);
  267. if (array_search($type,$longtext)) echo "<tr><td>".$this->text_area($fields[$i],'textarea','','')."</td></tr>";
  268. }
  269. unset($style);
  270. }
  271. echo "<tr><td><input type='submit' name='".$table."_input' value='Insert values'></td></tr>\n";
  272. echo "<tr><td><input type='reset' name='".$table."_reset' value='Reset values'></td></tr>\n";
  273. echo "</table>\n";
  274. echo "</form>\n";
  275. }
  276. /* obtain maximum value of a field in a given table */
  277. function max_in_field($field,$table){
  278. $sql = "select MAX($field) as max from $table";
  279. $res = $this->execSQL($sql);
  280. return ($res[0]->max);
  281. }
  282. /* remove last character from string ex: the "," */
  283. function rm_last($string){
  284. $num = strlen($string);
  285. $string = substr_replace($string,"",($num-1),($num+1));
  286. return $string;
  287. }
  288. /*
  289. * insert's into fields array( "field" => specific
  290. * value $value ); in the form of an array
  291. */
  292. function mysql_insert($table,$array){
  293. $table = trim($table);
  294. if ($table == '') die ("Erro tabela n???o defenida");
  295. foreach ( $array as $key => $value){
  296. $campos.=$key.",";
  297. $value='"'.$value.'"';
  298. $valores.=$value.",";
  299. }
  300. $campos = substr($campos,0 , -1);
  301. $valores = substr($valores,0 , -1);
  302. $sql="INSERT INTO $table ($campos) VALUES ($valores)";
  303. if($this->exec($sql)){
  304. return mysql_insert_id($this->conn);
  305. }
  306. }
  307. function mysql_update($table,$array,$condition){
  308. $table = trim($table);
  309. if (sizeof($condition)<0 )exit($this->error("no condition defined"));
  310. if ($table == '') exit($this->error("Erro table or condition not defenide,mysql_update()"));
  311. foreach ( $array as $key => $value){
  312. $campos.=$key."='".$value."',";
  313. }
  314. $campos = $this->rm_last($campos);
  315. $sql="UPDATE $table SET $campos WHERE $condition ";
  316. return $this->exec($sql);
  317. }
  318. /* To close the connection to the db */
  319. function fin(){
  320. mysql_close($this->conn);
  321. }
  322. function finalize(){
  323. $this->fin();
  324. }
  325. }
  326. ?>