Location: PHPKode > scripts > Datamodul > class.mysql_datamodul_php5_v1.3.1.inc.php
<?php

/******************** class.mysql_datamodul_php5_v1.3.1.inc.php ***************
 *                                                                            *
 *                                                                            *
 *                                                                            *
 *                     MySQL Database managing countener class under PHP5     *
 *                                                                            *
 *                                                                            *
 *                                           Author: Tamás Hernádi            *
 *                                           Last updated: 2008.05.17         *
 *                                           Version: 1.3.1                   *
 *                                                                            *
 *                                                                            *
 ******************************************************************************/

/*
PHPclasses.org rating of my class:

This class is a MySQL database access wrapper with metadata retrieval functions.

It can establish MySQL database server connections, execute SQL queries, iterate
over query results and retrieve its data as arrays.

The class can also retrieve metadata like the database table names
and the respective fields.

This class won't show field names if "inner join, left join, right join" SQL structures are used!
These will be probably implemented next time.

*/

/*
History:
2006.03.31: The first version of my datamodul project was finished.
2006.07.30: The datamodul class was uploaded to PHPclasses.org first.
2006.08.04: The datamodul class was approved in PHPclasses.org.
2006.08.05: Execute_sql function can already execute the 'describe' and the 'help' mysql commands.
2006.08.06: 4 new functions were added to this class: get_sql_affected_row_count, get_db_names, get_db_count;
	    datamodul constructor was modified and it is not compatible the previous version of my class
2006.08.08: 4 newer functions were added to this class: dm_info, dm_select_result_info, get_sql_executed_count, get_sql_string
2006.08.09: Some simple modification was performed in the datamodul inside functions
2007.04.18.: set_select_db was out from this class, because in the same connection couldn't be changed correct the database.
2007.05.07 : delete_row method was added to the class, it can delete the actual record from recordset(sql_out_rows)
2008.05.17: Some simple modification was performed in the datamodul inside functions
*/


/*
I am curious for your opinion, so please rate my class me to know how usefull it is. Thank you very much.
*/

/*

    Class Datamodul usable function list:

    dm_info
    dm_select_result_info
    get_server
    get_selected_db
    get_user
    get_pass
    get_db_names
    get_db_table_names
    get_db_table_field_names
    get_db_table_field_name
    get_db_table_field_types
    get_db_table_field_nulls
    get_db_table_field_keys
    get_db_table_field_defaults
    get_db_table_field_extras
    get_db_count
    get_db_table_count
    get_db_table_field_counts
    get_db_table_field_count
    execute_sql
    get_sql_string
    set_sql_row_index
    get_sql_row_index
    get_sql_rows
    get_sql_table_names
    get_sql_field_names
    get_sql_table_alias_names
    get_sql_table_count
    get_sql_field_count
    get_sql_row_count
    delete_row
    get_sql_row_first
    get_sql_row_next
    get_sql_row_back
    get_sql_row_last
    get_sql_field_index
    get_sql_affected_row_count
    get_sql_executed_count

*/

class DataModul //class DataModul begin
{

 //data members

 //connection data

 protected $server;                 //(string) server where the mysql database is located
 protected $user;                   //(string) user name
 protected $pass;                   //(string) and user password of mysql connection
 protected $selected_db;            //(string) selected database

 // database data

 protected $db_names;                //(array) all databases in the server
 protected $db_connection;           //(string) database connection
 protected $db_table_names;          //(array) the tablenames of the selected database
 protected $db_table_field_names;    //(array) the fieldnames of tables of the selected database are groupped by tablenames
 protected $db_table_field_types;    //(array) the fieldtypes of tables of the selected database are groupped by tablenames
 protected $db_table_field_nulls;    //(array) the fieldnulls of tables of the selected database are groupped by tablenames
 protected $db_table_field_keys;     //(array) the fieldkeys of tables of the selected database are groupped by tablenames
 protected $db_table_field_defaults; //(array) the fielddefaults of tables of the selected database are groupped by tablenames
 protected $db_table_field_extras;   //(array) the fieldextras of tables of the selected database are groupped by tablenames

 protected $db_count;                //(int) counter for databases of the connected server
 protected $db_table_count;          //(int) counter for the selected database tables
 protected $db_table_field_count;    //(array) counter for tablefields of the selected database are groupped by tablenames

 //actual query data

 protected $sql_string;              //(string) last executed sql query string
 protected $sql_out_res;             //(string) sql query resoure
 protected $sql_out_rows;            //(array) sql query rows
 protected $sql_out_table_names;     //(array) sql query table names
 protected $sql_out_table_alias_names;//(array) sql query table alias names
 protected $sql_out_table_count;     //(int) sql query table counter
 protected $sql_out_field_names;     //(array) sql query fields names
 protected $sql_out_field_count;     //(int) sql query field counter
 protected $sql_out_row_count;       //(int) sql query row counter
 protected $sql_out_row_index;       //(int) sql query row index
 protected $sql_affected_row_count;  //(int) sql query affected row counter (UPDATE,INSERT,DELETE)
 protected $sql_executed_count;      //(int) counter for executed sql queries by the user

 protected $ready;                   //(int) If its value is 1 datamodul will be ready to operation


//methods

//public methods

 /**
 * @param string $server :  server where the mysql database is located
 * @param string $user : user name
 * @param string $pass : and user password of mysql connection
 * @param string $database : selected database
 */
 public function DataModul($server,$user,$pass,$sel_database) //class DataModul constructor
   {

     $this->ready=0;
     $this->server=$server;
     $this->user=$user;
     $this->pass=$pass;
     $this->selected_db=$sel_database;
     $this->open_db_connection();

     $this->set_db_names();


     $this->set_db_table_names();
     $this->set_db_table_field_names();

     $this->sql_out_res=0;
     $this->sql_out_rows=array();
     $this->sql_out_table_names=array();
     $this->sql_out_table_alias_names=array();
     $this->sql_out_table_count=0;
     $this->sql_out_field_names=array();
     $this->sql_out_field_count=0;
     $this->sql_out_row_count=0;
     $this->sql_out_row_index=-1;
     $this->sql_affected_row_count=0;
     $this->sql_executed_count=0;
     $this->sql_string='';
     $this->ready=1;
   }


/**
* this function displays graphically your mysql connection data and selected database metadata
*/
 public function dm_info()
   {
     echo '<p><u>DATAMODUL INFORMATION</u><p>';
     echo '<table border="0" cellspacing="2" cellpadding="2">';
     echo '<tr bgcolor="#DDDDDD"><td width="200"><b>DATABASES:</b></td></tr>';
     if ($this->db_count>0)
       {
         for ($i=0; $i<$this->db_count; $i++)
     	    {
              echo '<tr bgcolor="#DDDDDD"><td>',$this->db_names[$i],'</td></tr>';

         	}
        }

     echo '<tr height="10"><td></td></tr>';
     echo '<tr bgcolor="#DDDDDD"><td width="200"><b>CONNECTED SERVER:</b></td><td width="200">',$this->server,'</td></tr>';
     echo '<tr bgcolor="#DDDDDD"><td><b>USER NAME:</b></td><td>',$this->user,'</td></tr>';
          echo '<tr bgcolor="#DDDDDD"><td><b>SELECTED DATABASE:</b></td><td>',$this->selected_db,'</td></tr>';
	 	  echo '<tr height="10"><td></td></tr>';
          echo '<tr bgcolor="#DDDDDD"><td><b>TABLES:</b></td><td><b>FIELDS:</b></td><td width="200"><b>TYPES:</b></td><td width="200"><b>NULLS:</b></td>';
          echo '<td width="200"><b>KEYS:</b></td><td width="200"><b>DEFAULTS:</b></td><td width="200"><b>EXTRAS:</b></td></tr>';
          if ($this->db_table_count>0)
            {
              for ($i=0; $i<$this->db_table_count; $i++)
     	         {
                   echo '<tr><td bgcolor="#DDDDDD">',$this->db_table_names[$i],'</td>';
                   for ($j=0; $j<$this->db_table_field_count[$this->db_table_names[$i]]; $j++)
           	         {
                       echo '<td bgcolor="#DDDDDD">',$this->db_table_field_names[$this->db_table_names[$i]][$j],'</td>';
                       echo '<td bgcolor="#DDDDDD">',$this->db_table_field_types[$this->db_table_names[$i]][$j],'</td>';
                       echo '<td bgcolor="#DDDDDD">',$this->db_table_field_nulls[$this->db_table_names[$i]][$j],'</td>';
                       echo '<td bgcolor="#DDDDDD">',$this->db_table_field_keys[$this->db_table_names[$i]][$j],'</td>';
                       echo '<td bgcolor="#DDDDDD">',$this->db_table_field_defaults[$this->db_table_names[$i]][$j],'</td>';
                       echo '<td bgcolor="#DDDDDD">',$this->db_table_field_extras[$this->db_table_names[$i]][$j],'</td>';
                       echo '</tr><tr><td></td>';
                     }
                   echo '</tr>';
                 }
            }
          else
            {
			   echo '<tr bgcolor="#DDDDDD"><td><font color="#FF0000">There are no tables in your selected database!</font></td></tr>';
            }
     echo '</table>';
   }

/**
* this function displays graphically your last executed select query in a webbrowser
*/
 public function dm_select_result_info()
   {
      echo '<p><u>DATAMODUL EXECUTED SELECT RESULT INFORMATION</u><p>';
      if (strtolower(substr(trim($this->sql_string),0,6))=='select')
	  	{
          echo '<table border="0" cellspacing="2" cellpadding="2">';
          echo '<tr bgcolor="#DDDDDD"><td width="200"><b>SELECT STRING:</b></td><td width="200">',$this->sql_string,'</td></tr>';
          echo '</table>';
          echo '<table border="0" cellspacing="2" cellpadding="2">';
          echo '<tr height="10"><td></td></tr>';
          echo '<tr bgcolor="#DDDDDD"><td></td>';
          for ($i=0; $i<$this->sql_out_field_count; $i++)
            {
            	echo '<td><b>',$this->sql_out_field_names[$i],'</b></td>';
            }
          echo '</tr>';

          if ($this->sql_out_row_count>0)
            {
              $row=$this->get_sql_row_first();
              do
                {
                  echo  '<tr bgcolor="#DDDDDD"><td>',$this->sql_out_row_index+1,'</td>';
                  for ($i=0; $i<$this->sql_out_field_count; $i++)
              	    {
                      echo '<td>',$row[$i],'</td>';
              	    }
                  echo  '</tr>';

                }
              while ($row=$this->get_sql_row_next());
            }
          else
            {
               echo '<tr bgcolor="#DDDDDD"><td><font color="#FF0000">No records in the result!</font></td></tr>';
            }
          echo '</table>';

        }
      else
      	 {
           echo '<table border="0" cellspacing="2" cellpadding="2">';
           echo '<tr bgcolor="#DDDDDD"><td><font color="#FF0000">';
           echo 'The Datamodul has no SELECT query result!</font></td></tr></table>';
         }
   }


/**
* @return string : return the MySQL server's address if it exists
*/
 public function get_server()
   {

     return $this->server;
   }


/**
* @return string : return the actual selected database name
*/
 public function get_selected_db()
   {

     return $this->selected_db;
   }




/**
* @return string : return the username of the mysql connection
*/
 public function get_user()
   {

     return $this->user;
   }


/**
* @return string : return the user password of the mysql connection
*/
 public function get_pass()
   {

     return $this->pass;
   }


/**
* @return array : return the all databases of the connected server
*/
 public function get_db_names()
   {

     return $this->db_names;
   }


/**
* @return int : return the countered databases of the connected server
*/
 public function get_db_count()
   {

     return $this->db_count;
   }


/**
* @return array : return the tablenames of the database
*/
 public function get_db_table_names()
   {

      return $this->db_table_names;

   }


/**
* @param int $index : tablename's index
* @return string : return the tablename of database which index is in parameter
*/
 public function get_db_table_name($index)
   {

      return $this->db_table_names[$index];

   }


/**
* @param string $table_name : tablename, which fields are needed, default parameter (*), which
* means the all tables
* @return array : return the fieldnames of table or tables which is/are in parameter
*/
 public function get_db_table_field_names($table_name='*')
   {

      if ($table_name=='*')
        {
          return $this->db_table_field_names;
        }
      else
        {
          return $this->db_table_field_names[$table_name];
        }
   }


/**
* @param string $table_name : tablename
* @param int $field_index : fieldindex of the table
* @return string : return the fieldname
*/
 public function get_db_table_field_name($table_name,$field_index)
   {

 	  return $this->db_table_field_names[$table_name][$field_index];
   }



/**
* @param string $table_name : tablename, which fieldtypes are needed, default parameter (*), which
* means the all tables
* @return array : return the fieldtypes of table or tables which is/are in parameter
*/
 public function get_db_table_field_types($table_name='*')
   {
      if ($table_name=='*')
        {
          return $this->db_table_field_types;
        }
      else
        {
          return $this->db_table_field_types[$table_name];
        }
   }


/**
* @param string $table_name : tablename, which fieldnulls are needed, default parameter (*), which
* means the all tables
* @return array : return the fieldnulls of table or tables which is/are in parameter
*/
 public function get_db_table_field_nulls($table_name='*')
   {
      if ($table_name=='*')
        {
          return $this->db_table_field_nulls;
        }
      else
        {
          return $this->db_table_field_nulls[$table_name];
        }
   }


/**
* @param string $table_name : tablename, which fieldkeys are needed, default parameter (*), which
* means the all tables
* @return array : return the fieldkeys of table or tables which is/are in parameter
*/
 public function get_db_table_field_keys($table_name='*')
   {
      if ($table_name=='*')
        {
          return $this->db_table_field_keys;
        }
      else
        {
          return $this->db_table_field_keys[$table_name];
        }
   }


/**
* @param string $table_name : tablename, which fielddefaults are needed, default parameter (*), which
* means the all tables
* @return array : return the fielddefaults of table or tables which is/are in parameter
*/
 public function get_db_table_field_defaults($table_name='*')
   {
      if ($table_name=='*')
        {
          return $this->db_table_field_defaults;
        }
      else
        {
          return $this->db_table_field_defaults[$table_name];
        }
   }


/**
* @param string $table_name : tablename, which fieldextras are needed, default parameter (*), which
* means the all tables
* @return array : return the fieldextras of table or tables which is/are in parameter
*/
 public function get_db_table_field_extras($table_name='*')
   {
      if ($table_name=='*')
        {
          return $this->db_table_field_extras;
        }
      else
        {
          return $this->db_table_field_extras[$table_name];
        }
   }


/**
* @return int : return the countered tables of the database
*/
 public function get_db_table_count()
   {

 	  return $this->db_table_count;
   }


/**
* @return array : return the all countered fields of the all tables are groupped by tablenames
*/
 public function get_db_table_field_counts()
   {

      return $this->db_table_field_count;
   }


/**
* @param string $table_name : tablename
* @return int : return the countered fields of table
*/
 public function get_db_table_field_count($table_name)
   {

      return $this->db_table_field_count[$table_name];
   }


/**
* @return int : return the countered executed sql queries by the user
*/
 public function get_sql_executed_count()
 	{
      return $this->sql_executed_count;
    }

/**
* @return string : return the last executed sql query string
*/
 public function get_sql_string()
 	{
      return $this->sql_string;
    }


/**
* @return array : return the tablenames which are consisted by the sql query
*/
 public function get_sql_table_names()
   {

      return $this->sql_out_table_names;
   }


/**
* @return array : return the tablenames aliases which are consisted by the sql query
*/
 public function get_sql_table_alias_names()
   {

      return $this->sql_out_table_alias_names;
   }


/**
* @return array : return the fieldnames which are consisted by the sql query
*/
 public function get_sql_field_names()
   {

     return $this->sql_out_field_names;
   }


/**
* @return int : return the countered tables which are consisted by the sql query
*/
 public function get_sql_table_count()
   {

     return $this->sql_out_table_count;
   }


/**
* @return int : return the countered fields which are consisted by the sql query
*/
 public function get_sql_field_count()
   {

     return $this->sql_out_field_count;
   }


/**
* @return int : return the countered rows which are consisted by the sql query
*/
 public function get_sql_row_count()
   {

     return $this->sql_out_row_count;
   }


/**
* @param string $field_name : fieldname, which we know its index    !! must give in lowercase format !!
* @return int : return the fieldindex of base of $field_name, if the sql query doesn't consist it, then return -1
*/
 public function get_sql_field_index($field_name)
   {

     $ind=0;
     while (($ind!=$this->sql_out_field_count) and ($field_name!=$this->sql_out_field_names[$ind]))
       {
         $ind++;
       }
     if ($ind!=$this->sql_out_field_count)
       {
         return $ind;
       }
     else
       {
         return -1;
       }
   }


/**
* @return int : return the actual index of the sql query
*/
 public function get_sql_row_index()
   {

     return $this->sql_out_row_index;
   }


/**
* @param int $rindex : changing the sql query rowindex (sql_out_row_index)
*/
 public function set_sql_row_index($rindex)
   {

     if (($rindex<=$this->sql_out_row_count) and ($rindex>=-1))
      $this->sql_out_row_index=$rindex;
   }


/**
* @return array : return the all rows of the sql query
*/
 public function get_sql_rows()
   {

     return $this->sql_out_rows;
   }


/**
* @param int $rindex : row index, which identifies the sql query's row
* @return array/int : return the row, or 0 if $rindex parameter consisted illegal value
*/
 public function get_sql_row($rindex)
   {

     if (($rindex<$this->sql_out_row_count) and ($rindex>-1))
       {
         $this->sql_out_row_index=$rindex;
         return $this->sql_out_rows[$rindex];
       }
     else
       {
         return 0;
       }

   }


/**
* delete the actual record from the result array, where the row_index stands
*/
 public function delete_row()
   {
     if (($this->sql_out_row_index<$this->sql_out_row_count) and ($this->sql_out_row_index>-1))
      {
       $this->sql_out_rows = array_merge(array_slice($this->sql_out_rows, 0, $this->sql_out_row_index), array_slice($this->sql_out_rows, $this->sql_out_row_index+1));
       $this->sql_out_row_count=count($this->sql_out_rows);
      }
   }



/**
* @return array/int : return the first row of the sql query, or 0 if the sql query had no result
*/
 public function get_sql_row_first()
   {

    if ($this->sql_out_row_index!=-1)
      {
         $this->sql_out_row_index=0;
         return $this->sql_out_rows[0];
       }
     else
       {
         return 0;
       }
   }


/**
* @return array/int : return the last row of the sql query, or 0 if the sql query had no result
*/
 public function get_sql_row_last()
   {

     if ($this->sql_out_row_index!=-1)
       {
         $this->sql_out_row_index=$this->sql_out_row_count-1;
         return $this->sql_out_rows[$this->sql_out_row_count-1];
       }
     else
       {
         return 0;
       }
   }


/**
* @return array/int : return the next row of the sql query, or 0 if the sql query had no result, or no more rows
*/
 public function get_sql_row_next()
   {

     if ($this->sql_out_row_index<$this->sql_out_row_count-1)
       {
         $this->sql_out_row_index++;
         return $this->sql_out_rows[$this->sql_out_row_index];
       }
     else
       {
         return 0;
       }
   }


/**
* @return array/int : return the previous row of the sql query, or 0 if the sql query had no result, or no more rows
*/
 public function get_sql_row_back()
   {

     if ($this->sql_out_row_index>0)
       {
         $this->sql_out_row_index--;
         return $this->sql_out_rows[$this->sql_out_row_index];
       }
     else
       {
         return 0;
       }
   }


/**
* @return int : return affected rows count of the sql query (UPDATE,INSERT,DELETE)
*/
 public function get_sql_affected_row_count()
   {
     return $this->sql_affected_row_count;
   }



/**
* set_db_names, set_db_table_names, set_db_table_field_names private functions call, and this is the most important callable function
* executes SQL command;
* @param string $sql : SQL command string, which will be executed
*/
 public function execute_sql($sql)
   {

      $this->sql_out_rows=array();
      $this->sql_out_row_index=-1;
      $this->sql_out_row_count=0;
      $this->sql_affected_row_count=0;

      $this->sql_out_res=mysql_query($sql,$this->db_connection) or die('SQL execute error!<br>'. mysql_errno($this->db_connection).": ".mysql_error($this->db_connection));

      if ($this->ready==1)
        {
      		$this->sql_string=$sql;
      		$this->sql_executed_count++;

            $this->set_sql_table_names($sql); // table alias together
      		$this->set_sql_field_names($sql);
        }

      
      $this->sql_affected_row_count=@mysql_affected_rows();
      $this->sql_out_row_count=@mysql_num_rows($this->sql_out_res);

          if ($this->sql_out_row_count>0)
            {
               while ($row=mysql_fetch_row($this->sql_out_res))
                 {
                    foreach ($row as $value)
                      {
                        $row_result[]=$value;
                      }
                    $this->sql_out_rows[]=$row_result;
                    $this->sql_out_row_index++;
                    $row_result=array();
                 }
            }
        
  }



//protected methods

/**
* DataModul constructor function calls this;
* make a database connection
*/
 protected function open_db_connection()
   {
      $this->db_connection=@mysql_connect($this->server,$this->user,$this->pass) or die('MySQL connection opening error! Host '.$this->server.' is not accessible!');
      if ($this->selected_db!='')
      	{
      		@mysql_select_db($this->selected_db,$this->db_connection) or die('The MySQL database is not exist!<br>'. mysql_errno($this->db_connection).": ".mysql_error($this->db_connection));
        }
   }


//private methods


/**
* execute_sql function calls this
* if the sql command was a 'SELECT' then array of sql_out_table_names will be uploaded with the sql query's tablenames, and will be set the
* counter sql_out_table_count
* @param string $psql : SQL command
*/
 private function set_sql_table_names($psql)
   {
      $this->sql_out_table_names=array();
      $this->sql_out_table_alias_names=array();
      $this->sql_out_table_count=0;

      $psql=strtolower($psql);
      if (substr(trim($psql),0,6)=='select')
        {
          $result_array=explode(' from ',$psql);
          if (count($result_array)>=2)
            {
              $result=trim($result_array[1]);
              $ind=0;
              $tables_next_clause=''; // Clauses follower sql tables
              while ($ind!=strlen($result) and $tables_next_clause!='where' and $tables_next_clause!='group by'
                and $tables_next_clause!='order by' and $tables_next_clause!='limit' )
                {
                   if ($result[$ind]!=' ')
                     {
                        $tables_next_clause.=$result[$ind];
                     }
                   else
                     {
                       if (($tables_next_clause!='group') and ($tables_next_clause!='order'))
                         {
                            $tables_next_clause='';
                         }
                       else
                         {
                           $act_ind=$ind;
                           while ($act_ind!=strlen($result) and $result[$act_ind]==' ')
                             {
                               $act_ind++;
                             }
                           if ($act_ind!=strlen($result))
                             {
                               if (substr($result,$act_ind,2)=='by')
                                 {
                                   $tables_next_clause=$tables_next_clause.' by';
                                   $ind+=2;
                                 }

                             }
                         }
                     }
                   $ind++;
                }

              if ($tables_next_clause=='where'
              or $tables_next_clause=='group by'
              or $tables_next_clause=='order by'
              or $tables_next_clause=='limit')
                {
			       $ind=$ind-strlen($tables_next_clause)-1;         // decrement 1 because of the last $ind++
                }


              $result=trim(substr($result,0,$ind));
              $result_tables_array=explode(',',$result);

              for ($i=0; $i<count($result_tables_array); $i++)
                {

                   $result_array=explode(' ',trim($result_tables_array[$i]));

                   if (count($result_array)==2) // user uses table alias
                	{
                        $this->sql_out_table_names[]=trim($result_array[0]);
                    	$this->sql_out_table_alias_names[]= trim($result_array[1]); //alias table names
                   	}
                   else // user doesn't use table alias
                   	{
                        $this->sql_out_table_names[]=trim($result_array[0]);
                    	$this->sql_out_table_alias_names[]="NULL_".$i;
                    }
                }
            $this->sql_out_table_count=count($this->sql_out_table_names);
           }
        }
   }


/**
* execute_sql function calls this
* if the sql command was a 'SELECT' then array of sql_out_field_names will be uploaded with the sql query's fieldnames, and will be set the
* counter sql_out_field_count
* @param string $psql : SQL command
*/
private function set_sql_field_names($psql)
   {
     $this->sql_out_field_names=array();
     $this->sql_out_field_count=0;

      $psql=strtolower($psql);
      if (substr(trim($psql),0,6)=='select')
         {
             $psql=substr($psql,6);
             $result_array=explode(' from ',$psql);

             if (trim($result_array[0])=='*')
                {
                    for ($i=0; $i<$this->sql_out_table_count; $i++)
                      {
                          for ($j=0; $j<$this->get_db_table_field_count($this->sql_out_table_names[$i]); $j++)
                             {
                                    $fields_array=$this->get_db_table_field_names($this->sql_out_table_names[$i]);
                                    $fields[]=$fields_array[$j];
                             }

		              }

                }
             else
                {
                   $result_fields_array=explode(',',trim($result_array[0]));

                   for ($i=0; $i<count($result_fields_array); $i++)
                      {


                          	if ( count(explode('*',trim($result_fields_array[$i])))==2){

                                $result_table=explode('.',trim($result_fields_array[$i]));

                                for ($j=0; $result_table[0]!=$this->sql_out_table_alias_names[$j] and $j<count($this->sql_out_table_alias_names); $j++){};
                                if ($j!=count($this->sql_out_table_alias_names))
                                   {
                                        //$fields=$this->get_db_table_field_names($this->sql_out_table_names[$j]);
                                        $tmp_array=$this->get_db_table_field_names($this->sql_out_table_names[$j]);
                                        for ($k=0; $k<count($tmp_array); $k++) {
                                             $fields[]=$tmp_array[$k];
                                        };

                                   }
                                else
                                	{
                                		//$fields=$this->get_db_table_field_names($result_table[0]);
                                        $tmp_array=$this->get_db_table_field_names($result_table[0]);
                                        for ($k=0; $k<count($tmp_array); $k++) {
                                             $fields[]=$tmp_array[$k];
                                        };
                                   }

                            }
                           else
                           {
	                          $field_array=explode(' as ',trim($result_fields_array[$i]));
	                          if (count($field_array)==2)
	                            {
	                              $fields[]=trim($field_array[1]);
	                            }
	                          else
	                            {
                                  $res_field_array=explode('.',$result_fields_array[$i]);


                                  if (count($res_field_array)==2)
                                  {
								  	$fields[]=trim($res_field_array[1]);
                                  }
                                  else
                                  {
                                  	$fields[]=trim($res_field_array[0]);
                                  }

	                            }
                          }
                      }

                }
             $this->sql_out_field_names=$fields;
             $this->sql_out_field_count= mysql_num_fields($this->sql_out_res);
        }
   }


   /**
* DataModul constructor function calls this;
* it sets the database names and its counts of the connected server
*/
 private function set_db_names()
	{
        $this->db_names=array();
        $this->db_count=0;

        $this->execute_sql("SHOW DATABASES");

        for ($i=0; $i<$this->sql_out_row_count; $i++)
          {
            $this->db_names[]=$this->sql_out_rows[$i][0];
          }
        $this->db_count=count($this->db_names);
    }


/**
* DataModul constructor function calls this;
* it sets the database's tablenames and counter of tables
*/
 private function set_db_table_names()
    {
       $this->db_table_names=array();
       $this->db_table_count=0;

       $sql="SHOW TABLES FROM $this->selected_db";
       $this->execute_sql($sql);
       for ($i=0; $i<$this->sql_out_row_count; $i++)
         {
            $this->db_table_names[]=$this->sql_out_rows[$i][0];
         }
        $this->db_table_count=count($this->db_table_names);
    }


/**
* DataModul constructor function calls this;
* it sets the database's fieldnames are groupped by tablenames and counter of fields are groupped by tablenames
*/
 private function set_db_table_field_names()
   {
      $this->db_table_field_names=array();
	  $this->db_table_field_types=array();
	  $this->db_table_field_nulls=array();
	  $this->db_table_field_keys=array();
	  $this->db_table_field_defaults=array();
	  $this->db_table_field_extras=array();
      $this->db_table_field_count=array();

      for ($i=0; $i<$this->db_table_count; $i++)
        {
           $table=$this->db_table_names[$i];
           $sql="DESCRIBE $table";
           $this->execute_sql($sql);
           for ($j=0; $j<$this->sql_out_row_count; $j++)
             {
                $this->db_table_field_names[$table][]=$this->sql_out_rows[$j][0];
                $this->db_table_field_types[$table][]=$this->sql_out_rows[$j][1];
                $this->db_table_field_nulls[$table][]=$this->sql_out_rows[$j][2];
 				$this->db_table_field_keys[$table][]=$this->sql_out_rows[$j][3];
 				$this->db_table_field_defaults[$table][]=$this->sql_out_rows[$j][4];
 				$this->db_table_field_extras[$table][]=$this->sql_out_rows[$j][5];
             }
           $this->db_table_field_count[$table]=count($this->db_table_field_names[$table]);
        }
   }


}// class DataModul end




class NewDataModul extends DataModul

{ // class NewDataModul begin


 /**
 * @param DataModul $datamodul : we can create a new datamodul by an exist datamodul object
 * so fast and simple create a new datamodul
 */
 public function NewDataModul(&$datamodul)      //class NewDataModul constructor
  {
     $this->server=$datamodul->get_server();
     $this->user=$datamodul->get_user();
     $this->pass=$datamodul->get_pass();
     $this->selected_db=$datamodul->get_selected_db();
     $this->open_db_connection();

     $this->db_names=$datamodul->get_db_names();
     $this->db_table_names=$datamodul->get_db_table_names();
     $this->db_table_field_names=$datamodul->get_db_table_field_names();
     $this->db_table_field_types=$datamodul->get_db_table_field_types();
     $this->db_table_field_nulls=$datamodul->get_db_table_field_nulls();
     $this->db_table_field_keys=$datamodul->get_db_table_field_keys();
     $this->db_table_field_defaults=$datamodul->get_db_table_field_defaults();
     $this->db_table_field_extras=$datamodul->get_db_table_field_extras();
     $this->db_count=$datamodul->get_db_count();
     $this->db_table_count=$datamodul->get_db_table_count();
     $this->db_table_field_count=$datamodul->get_db_table_field_counts();

     $this->sql_out_res=0;
     $this->sql_out_rows=array();
     $this->sql_out_table_names=array();
     $this->sql_out_table_alias_names=array();
     $this->sql_out_table_count=0;
     $this->sql_out_field_names=array();
     $this->sql_out_field_count=0;
     $this->sql_out_row_count=0;
     $this->sql_out_row_index=-1;
     $this->sql_affected_row_count=0;
     $this->sql_executed_count=0;
     $this->sql_string='';
     $this->ready=1;
  }


}  // class NewDataModul end








?>
Return current item: Datamodul