Version: 1.02
Type: Class
Category: Databases
License: GNU General Public License
Description: Handling several databases at he same time easily
<? //========================================================================================== // By Umut Isik from Mavi Software Corporation // [email protected] // // I am waiting for your suggestions.The description of the object and functions are below the code // // // NOT : The usage of the insert() function is a bit different.Please read carefully. // and mail if needed. // // //========================================================================================== class database { var $database; var $link; var $table_num; var $tables= array(); function show() { $db=$this->database; $tables=$this->table_num; echo("Database: $db<br>There are $tables tables :<br>"); for($i=0;$i<$this->table_num;$i++) { $tr=$this->tables[$i]; echo("$tr <br>"); } } function construct($str,$link) { $this->link=$link; $db_list=mysql_list_dbs($this->link); $flag=0; while($row=mysql_fetch_object($db_list)) { if($row->Database==$str) $flag=1; } if($flag==0) { echo("<p><b>Error in function construct().<br>"); echo("No such database named $str.<b></p>"); exit(); } else $this->database=$str; $tables_query=mysql_list_tables($this->database); $i=0; while(list($table)=mysql_fetch_row($tables_query)) { $this->tables[$i]=$table; $i++; } $this->table_num=$i; mysql_select_db($str,$this->link); } function insert($table,$field_names,$values) { mysql_select_db($this->database,$this->link); $db_list=mysql_list_dbs($this->link); if(!in_array($table,$this->tables)) { echo("<p><b>Error in function insert().<br>"); echo("There exist no table named $table in $this->database</b></p>"); exit(); } $fields=explode(",",$field_names); $values=explode("~@",$values); //This part is the most important part.While making the $values by the valu //es you must use an identifier.Be carefull not to use an identifier that //can be in a normal string.I offer using "~@" if(count($fields)!=count($values)) { echo("<p><b>Error in function insert().<br>"); echo("Invalid number of arguments</b></p>"); exit(); } $i=0; $res=mysql_query("select * from $table"); $res_num=mysql_num_fields($res); while($i<$res_num) { $table_field_names[$i]=mysql_field_name($res,$i); $i++; } $i=0; while($fields[$i]) { if(!in_array($fields[$i],$table_field_names)) { echo("<p><b>Error in function insert().<br>"); echo("Error in field names argument (second argument of the function)"); echo("There exist no field named $fields[$i] in table $table of $this->database<b></p>"); //echo $table_names[$i]; exit(); } $i++; } $query="INSERT INTO $table "; $i=0; while($fields[$i]) { if($i==0) { $field_string=$fields[$i]; $values_string="'".$values[$i]."'"; } else { $field_string=$field_string.",".$fields[$i]; $values_string=$values_string.",'".$values[$i]."'"; } $i++; } $query=$query."(".$field_string.")"." VALUES (".$values_string.")"; mysql_query($query); } function select($table,$field_names,$additional_rules) { //strings with no braces mysql_select_db($this->database,$this->link); if(!in_array($table,$this->tables)) { echo("<p><b>Error in function select().<br>"); echo("Error there exist no table named $table in $this->database</b></p>"); exit(); } if($field_names!="*") { $fields=explode(",",$field_names); $i=0; $res=mysql_query("select * from $table"); $res_num=mysql_num_fields($res); while($i<$res_num) { $table_field_names[$i]=mysql_field_name($res,$i); $i++; } $i=0; while($fields[$i]) { if(!in_array($fields[$i],$table_field_names)) { echo("<p><b>Error in function select().<br>"); echo("Error in field names argument (second argument of the function)"); echo("There exist no field named $fields[$i] in table $table of $this->database</b></p>"); //echo $table_names[$i]; exit(); } $i++; } } if($additional_rules) $query="SELECT ".$field_names." FROM ".$table." WHERE ".$additional_rules; else $query="SELECT ".$field_names." FROM ".$table; $result=mysql_query($query); $i=0; while($row=mysql_fetch_array($result)) { $array[$i]=$row; $i++; } return $array; } function update($table,$field_names,$values,$additional_rules) { mysql_select_db($this->database,$this->link); if(!in_array($table,$this->tables)) { echo("<p><b>Error in function update().<br>"); echo("Error there exist no table named $table in $this->database</b></p>"); exit(); } $fields=explode(",",$field_names); $values=explode("~@",$values); //This part is the most important part.While making the $values by the valu //es you must use an identifier.Be carefull not to use an identifier that //can be in a normal string.I offer using "~@" if(count($fields)!=count($values)) { echo("<p><b>Error in function update().<br>"); echo("Error invalid number of argiments</b></p>"); exit(); } $i=0; $res=mysql_query("select * from $table"); $res_num=mysql_num_fields($res); while($i<$res_num) { $table_field_names[$i]=mysql_field_name($res,$i); $i++; } $i=0; $num=count($fields); while($fields[$i]) { if(!in_array($fields[$i],$table_field_names)) { echo("<p><b>Error in function update().<br>"); echo("Error in field names argument (second argument of the update function)"); echo("There exist no field named $fields[$i] in table $table of $this->database<b></p>"); //echo $table_names[$i]; exit(); } if($i==0) $and=""; else $and=","; if(($values[$i][0]=="+") || ($values[$i][0]=="-") ) $update_str=$update_str." $and ".$fields[$i]."=".$fields[$i].$values[$i]; else $update_str=$update_str." $and ".$fields[$i]."='".$values[$i]."'"; $i++; } $query="UPDATE ".$table." SET ".$update_str; if($additional_rules) $query=$query." WHERE ".$additional_rules; mysql_query($query); } function remove ($table,$additional_rules) { mysql_select_db($this->database,$this->link); if(!in_array($table,$this->tables)) { echo("<p><b>Error in function remove().<br>"); echo("Error there exist no table named $table in $this->database</b></p>"); exit(); } $query="DELETE FROM ".$table." WHERE ".$additional_rules; mysql_query($query); } function field_name($table,$index) { mysql_select_db($this->database,$this->link); if(!in_array($table,$this->tables)) { echo("<p><b>Error in function field_name().<br>"); echo("Error there exist no table named $table in $this->database</b></p>"); exit(); } $fields=mysql_list_fields("$this->database","$table",$this->link); return mysql_field_name($fields,$i); } function field_num($table) { mysql_select_db($this->database,$this->link); if(!in_array($table,$this->tables)) { echo("<p><b>Error in function field_num().<br>"); echo("Error there exist no table named $table in $this->database</b></p>"); exit(); } $fields=mysql_list_fields("$this->database","$table",$this->link); return mysql_num_fields($fields); } function field_names($table) { mysql_select_db($this->database,$this->link); if(!in_array($table,$this->tables)) { echo("<p><b>Error in function field_names().<br>"); echo("Error there exist no table named $table in $this->database<br></p>"); exit(); } $fields=mysql_list_fields("$this->database","$table",$this->link); $columns=mysql_num_fields($fields); for ($i=0;$i<$columns;$i++) $fields_array[$i]=mysql_field_name($fields,$i); return $fields_array; } function insert_id() { return mysql_insert_id(); } } //========================================================================================== // This library helps you to make several database connections and to handle database operations // at the same time.The functions are explained below. // // 1) show() takes no arguments and prints the information of the object(The name of the db and the names of tables // 2) construct() takes two arguments.The name of the database and MySQL link identifier of the connection.It constructs the // object. // 3) insert() takes three arguments.The name of the table , the field names and the field values.The field names and the // field values must be string type. // For example: // insert("table_name","field1,field2,field3","value1i~@value2~@value3") // !!!Separating the values with "~@" is important.You may use a different separator. // !!!You have to modify insert function.The place is signed. // // // 4) select() takes three arguments.The name of the table , the field names and the rules for selecting. // This function returns an array pointer (The number os selected fields times the number of rows. // For example : // select("table_name","field1,field2,field3","field4='1' AND ...."); // select("table_name","*","field4='1' AND ...."); // 5) update() takes four arguments.The name of the table , the field names , the field values and the rules for updating // For example : // update("tablename","field1,field2,field3","v1~@+v2~@-v3","field4='1'"); // v1 sets the field1 value to v1 // +v2 increments field2 by v2 // -v3 decrements field3 by v3 // !!!Separating the values with "~@" is important.You may use a different separator. // !!!You have to modify insert function.The place is signed. // // 6) remove() takes two arguments.The name of the table and the rules for deleting.For example // remove("table_name","field='1' AND ...."); // 7) field_name() takes two arguments.The name of the table and the index of the field.It returns the name of the field // with the index in the table. // 8) field_num() returns the number of field in the table. // 9) field_names() returns an array of the names of the fields of the table // 10) insert_id() returns the id of the last insertion operation. // // // // //============Example=========================================== //<? //$link=mysql_connect("localhost","root","password"); // //$db1=new database; //creating a new object //$db1->construct("db1",$link); //constructing the connections // //$db2=new database; //creating a new object //$db2->construct("db2",$link); //constructing the connections // //$array=$db1->field_names("table1"); //$i=0; //while($array[$i]) // echo $array[$i]."<br>"; // //echo $db2->field_name("table1",0); //$db1->insert("table1","field1,field2","v1,v2"); //?> //========================================================================================== ?>