/1/enp_baza_bezado/bd-2008-03-14 TO DOdo wykorzystania i dodania do klas/mysql_class_v1_1.php
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
- <?php
-
- /* version 1.1
- * DATABASE CONNECTION CLASS For MySQL
- * This program is under the BSD License
- * http://www.opensource.org/licenses/bsd-license.html
- *
- * Copyright (C) 2003, Fernando Ribeiro <r.fernando@netc.pt>
- * All rights reserved.
- *
- */
-
- class html {
- var $hide_name;
- var $size;
-
- function input_box($name,$type,$size='20',$value='',$maxlength=''){
-
- if (!is_int($size + 0)){
- $size =" size='20'";
- }else{
- $size =" size='$size'";
- }
-
- if (!is_int($maxlength + 0) || trim($maxlength) == '' ){
- $maxlength = '';
- }else{
- $maxlength=" maxlength='$maxlength'";
- }
-
- if (trim($type)=='') return false;
-
- switch ($type){
- case 'password': case 'text': case 'hidden': case 'checkbox':
- case 'radio': case 'submit' : case 'reset':
- $type=" type='".$type."'";
- break;
- default: return false;
- }
-
- if (trim($value)!=''){
- $value = " value='$value'";
- }else{
- $value='';
- }
-
- if(trim($name)=='') return false;
-
- $name1 = trim($name);
- $name1 = "name='$name1'";
-
- if ($this->hide_name == 0 ) $inpname = htmlentities(ucfirst($name));
-
- return ("<b> $inpname </b> <input $name1 $type $value $size $maxlength >\n");
-
- }
-
- function text_area($name,$type,$size,$value='',$maxlength=''){
-
- if(trim($size)!='' && substr_count($size,"x") == 1 ){
- $this->size=$size;
- }
-
- if ($this->size==''){
- $this->size ='6x50';
- }
-
- if ($data = explode ("x",$this->size)){
- $size =" rows='$data[0]' cols='$data[1]' ";
- }
-
- if (!is_int($maxlength + 0) || trim($maxlength) == ''){
- $maxlength = '';
- }else{
- $maxlength=" maxlength='$maxlength'";
- }
-
- if (trim($value)==''){
-
- }
-
- if(trim($name)=='') return false;
-
- $name1 = trim($name);
- $name1 = "name='$name1'";
-
- $inpname = htmlentities(ucfirst($name));
-
- return ("<b> $inpname </b><br><textarea $name1 $size $maxlength >$value </textarea>\n");
- }
- }
-
- class bd extends html {
-
- var $conn; /* keeps the connection handler*/
- var $dbase;
- var $_database;
- var $_user;
- var $_pass;
- var $_server;
- var $ep; /* define entry point */
- var $confile; /* configuration file */
- var $encoding; /* enconding used by the database */
-
- function ligarBD($_database,$_user,$_pass,$_server){
- $this->$_database=$_database;
- $this->$_user=$_user;
- $this->$_pass=$_pass;
- $this->$_server=$_server;
-
- $this->ep = "func";
-
- $this->connect();
- }
- /* function connect();
- * to be called from
- * subclasses to read a conf file and
- * connect to a database
- */
- function connect(){
- $this->confile = "http://localhost/traderd/db.conf";
-
- if ($this->ep != "func") $this->readconf($this->confile);
-
- if (!extension_loaded('mysql')) {
- if (!dl('mysql.so')) {
- exit("Unable to load extension.\n<br>");
- }
- }
-
- if (!@$this->conn = mysql_connect($this->_server,$this->_user,$this->_pass)) die ("I'm Sorry the MySQL Server could not be reached !");
-
- if ($this->conn<0) {
- return -1;
- }
-
- $this->dbase=$this->_database;
- if (mysql_select_db($this->dbase,$this->conn)==false){
- $this->error("ERROR TRYING TO IDENTIFY DATABASE, mysql_select_db(netcrash)");
- return -1 ;
- }
-
- $this->encoding = mysql_client_encoding($this->conn);
-
- }
- /* function readconf($file)
- * read the configuration file
- * style conf like:
- * server=localhost
- * user=Username
- * pass=password
- * database=mydatabase
- */
- function readconf($file){
- //if (!file_exists($file) || !is_readable($file)) die ("Error reading configuration file. ");
- $def = file($file);
- $SIZEDEF = sizeof($def);
- for ($i=0 ; $i < $SIZEDEF ; $i++){
- $this->parse($def[$i]);
- }
-
- }
- /* parse the line of configuration file */
- function parse($line){
- if (substr_count($line,"=") == 1 ){
- $def = explode("=",$line);
- $var = trim($def[0]); /* name of variable */
- $value = trim($def[1]); /* value of variable */
-
- switch ($var){
- case 'server': $this->_server = $value ;
- break;
- case 'user' : $this->_user = $value;
- break;
- case 'pass' : $this->_pass = $value;
- break;
- case 'database': $this->_database = $value;
- break;
- }
- }
- }
-
- function error($function){
- echo "<hr size='1' width='50%' height='1px'>";
- echo "<br>Error in function $function .<br>";
- echo "Description...<br>";
- echo "Error number ... ".mysql_errno()."<br>";
- echo mysql_error()."<br>";
- }
-
- function parseResultObj($resultado){
- $n_rows= mysql_num_rows($resultado);
- if ($n_rows > 0 ){
- unset($entidades);
- /* Inicializa o array $entidades */
- for($i=0; $i< $n_rows ; $i++)
- {
- $entidades[$i]=mysql_fetch_Object($resultado);
- }
- unset($resultado);
- return $entidades;
- /* Retorna o array $entidades */
- }
- }
- /* Not tested execSQL_toarray()*/
- function execSQL_toarray($sql_command){
- //echo $sql_command."<br>";
- $sql_command = mysql_real_escape_string($sql_command,$this->conn);
- if ($resultado = mysql_db_query($this->dbase,$sql_command, $this->conn)){
- //echo "\n - Resultado - ".$resultado." <br>";
- $n_rows= mysql_num_rows($resultado);
- $n_fields = mysql_num_fields($resultado);
-
- if (!$row = mysql_fetch_array($result, MYSQL_BOTH)){
- exit ($this->error("mysql_fetch_array() in execSQL_toarray"));
- }
-
- //if ($n_rows < 1 && $n_fields < 1 ) exit ("Unable to results");
-
- return $row;
-
- }else{
- $this->error("execSQL()");
- return false;
- }
- }
-
- function execSQL($sql_command) {
- if ($resultado = mysql_db_query($this->dbase,$sql_command, $this->conn)){
-
- return ($this->parseResultObj($resultado));
- }else{
- $this->error("execSQL()");
- return false;
- }
- }
-
- function exec($sql_command) {
- if ($resultado = mysql_db_query($this->dbase,$sql_command, $this->conn)){
- return $resultado;
- }else{
- $this->error("exec()");
- return false;
- }
- }
- /* lists the fields of a table into an array */
- function listfields($table){
- if (trim($table)=='') return false;
- $fields = mysql_list_fields($this->_database, $table, $this->conn);
- $n_col = mysql_num_fields($fields);
-
- for ($i = 0; $i < $n_col; $i++) {
- $field_array[$i] = mysql_field_name($fields, $i);
- }
- return $field_array;
- }
- /* Lists all fields of a table and lists it's values
- in a html table.
- */
- function listtable($table){
- if (!$fields = $this->listfields($table)) return false;
- $FIELDS = count($fields);
-
- echo "<table border='1' cellspacing='4' cellpadding='0'>\n";
-
- echo "<tr>\n";
- for ( $i = 0 ; $i < $FIELDS ; $i++){
- echo "<td> $fields[$i] </td>";
- }
- echo "</tr>\n";
-
- if (!$values = $this->execSQL("select * from ".$table)) return false;
- $VALUES = count ($values);
- $j=0;
- for ($i=0; $i < $VALUES ; $i++ ){
- echo "<tr>";
- $j=0;
- while ($j < $FIELDS ){ echo "<td valign='top'>".$values[$i]->$fields[$j]."</td>"; $j++; }
- echo "</tr>\n";
- }
-
- echo "</table>\n";
-
- }
-
- /* buildform($table[,$form_action,$method,$action])
- * $action is insert update based
- * $form_action is this example (action='index.php')
- * BETA TESTED WORKS GOOD ON SMALL CODE
- * using the name of the fields in a table builds a form.
- */
- function buildform($table,$form_action='',$method='post',$action='insert'){
- echo "<form action='$form_action' method='$method'>\n
- <table>\n";
-
- $text = array("tinyint","int","smallint","mediumint","integer",
- "bigint","double","float","real","decimal","dec","numeric","fixed",
- "char","varchar","string");
-
- $longtext = array ("tinyblob","tinytext","blob","text","mediumblob",
- "mediumtext","longblob","longtext");
-
- $table = trim ($table);
-
- if (!$fields = $this->listfields($table)) return false;
- $FIELDS = count($fields);
- $result = $this->exec("select * from $table ");
- for ($i=0; $i < $FIELDS ; $i++ ){
-
- $flags = mysql_field_flags($result,$i);
-
- $data = explode(" ",$flags);
-
- $type = mysql_field_type($result, $i);
-
- $type = strtolower($type);
- //echo $type." ".strlen($type)."\n<br>";
- if (array_search('primary_key',$data)) {
- $style='hidden';
- $this->hide_name = 1;
- echo $this->input_box($fields[$i],'hidden','','');
- $this->hide_name = 0;
- }else{
- unset($style);
- }
- if (!isset($style)){
-
- if (array_search($type,$text)) echo "<tr><td>".$this->input_box($fields[$i],'text','','')."</td></tr>";
- //input($name,$type,$size='20',$value,$maxlength);
- if (array_search($type,$longtext)) echo "<tr><td>".$this->text_area($fields[$i],'textarea','','')."</td></tr>";
-
- }
-
- unset($style);
- }
-
- echo "<tr><td><input type='submit' name='".$table."_input' value='Insert values'></td></tr>\n";
- echo "<tr><td><input type='reset' name='".$table."_reset' value='Reset values'></td></tr>\n";
- echo "</table>\n";
- echo "</form>\n";
-
- }
-
- /* obtain maximum value of a field in a given table */
- function max_in_field($field,$table){
- $sql = "select MAX($field) as max from $table";
- $res = $this->execSQL($sql);
-
- return ($res[0]->max);
- }
-
- /* remove last character from string ex: the "," */
- function rm_last($string){
- $num = strlen($string);
- $string = substr_replace($string,"",($num-1),($num+1));
- return $string;
- }
-
- /*
- * insert's into fields array( "field" => specific
- * value $value ); in the form of an array
- */
- function mysql_insert($table,$array){
- $table = trim($table);
-
- if ($table == '') die ("Erro tabela n???o defenida");
-
- foreach ( $array as $key => $value){
- $campos.=$key.",";
- $value='"'.$value.'"';
- $valores.=$value.",";
- }
-
- $campos = substr($campos,0 , -1);
- $valores = substr($valores,0 , -1);
-
- $sql="INSERT INTO $table ($campos) VALUES ($valores)";
-
- if($this->exec($sql)){
- return mysql_insert_id($this->conn);
- }
-
- }
-
- function mysql_update($table,$array,$condition){
- $table = trim($table);
- if (sizeof($condition)<0 )exit($this->error("no condition defined"));
- if ($table == '') exit($this->error("Erro table or condition not defenide,mysql_update()"));
-
- foreach ( $array as $key => $value){
- $campos.=$key."='".$value."',";
- }
- $campos = $this->rm_last($campos);
-
- $sql="UPDATE $table SET $campos WHERE $condition ";
-
- return $this->exec($sql);
- }
-
- /* To close the connection to the db */
- function fin(){
- mysql_close($this->conn);
- }
-
- function finalize(){
- $this->fin();
- }
-
- }
-
- ?>