#native_company# #native_desc#
#native_cta#

cmysql_mset

By markitos
on January 17, 2001

Version: 1.0

Type: Class

Category: Databases

License: GNU General Public License

Description: A simple mysql class to connect to mysql server, search in a Database and run SQL statement.

<?
/*
Name....: CMySQL_MSet
Author..: mArkitos
Date....: 23/10/2000
Contact.: [email protected]
*/

class CMySQL_MSet {

	var $DB_Name;
	var $DB_User;
	var $DB_Pass;
	var $DB_Host;
	var $DB_Link;
	var $CM_TRUE = 1;
	var $CM_FALSE = 0;

	Function CMySQL_MSet ($Nombre_DB, $User_DB, $Pass_DB, $Host_DB){
		$this->DB_Name = $Nombre_DB;
		$this->DB_User = $User_DB;
		$this->DB_Pass = $Pass_DB;
        $this->DB_Host = $Host_DB;
		return $this->CM_TRUE;
	}
	Function OpenConnection(){
		$this->DB_Link = mysql_connect ("$this->DB_Host", "$this->DB_User", "$this->DB_Pass");
		if (!$this->DB_Link){
			return $this->CM_FALSE;
		} else {
            mysql_select_db ($this->DB_Name);
		}
		return $this -> CM_TRUE;
	}
	Function CloseConnection(){
		if ($this->DB_Link){
		   	mysql_close ($this->DB_Link);
		}
		return $this -> CM_TRUE;
	}
	Function ExecSQL ($Sql){
		if (($Sql == '') || (strlen ($Sql)< 10) || (!$Sql)){
			return $this->CM_FALSE;
		}
		$State = mysql_query ("$Sql", $this->DB_Link);
		unset ($Sql);
		return $State;
	}
	Function SearchIn ($NombreTabla, $NombreCampo, $Patron, $Completa){
		if ((strlen ($Patron)<1) || $Patron == ''){
			return $this->CM_FALSE;
		}
		$Resultado = mysql_list_tables ($this->DB_Name, $this->DB_Link);
		$Contador = 0;
 		while ($Contador < mysql_num_rows ($Resultado)) {
     		$Nombre_Tabla[$Contador] = mysql_tablename ($Resultado, $Contador);
            if ($Nombre_Tabla[$Contador] == $NombreTabla){
				 $Existe = 1;
			}
     		$Contador++;
		}
		if (! isset ($Existe)){
			return $this->CM_FALSE;
		} else {
			unset ($Exite);
			unset ($Contador);
			unset ($Nombre_Tabla);

			$SQL = "SELECT ";
			$SQL .= $NombreCampo;
			$SQL .= " FROM ";
			$SQL .= $NombreTabla;
			$Resultado = mysql_query ("$SQL");
			while ($QuItems = mysql_fetch_row ($Resultado)){
				if ($Completa == 1){
					if (eregi ("^$Patron$", $QuItems[1])){
						$Founds[] = $QuItems[0];
					}
				} else {
        	if (eregi ("$Patron", $QuItems[1])){
          	$Founds[] = $QuItems[0];
					}
				}
			}
			unset ($Completa);
			unset ($Resultado);
			unset ($SQL);
			return $Founds;
		}
	}
	Function SearchAdv ($NombreTabla, $NombreCampo, $Patron, $Completa, $Is_CaseS, $Condicion_Is, $Condicion){
		if ((strlen ($Patron)<1) || $Patron == ''){
			return $this->CM_FALSE;
		}
		$Resultado = mysql_list_tables ($this->DB_Name, $this->DB_Link);
		$Contador = 0;
 		while ($Contador < mysql_num_rows ($Resultado)) {
     		$Nombre_Tabla[$Contador] = mysql_tablename ($Resultado, $Contador);
        if ($Nombre_Tabla[$Contador] == $NombreTabla){
					$Existe = 1;
				}
     		$Contador++;
		}
		if (! isset ($Existe)){
			return $this->CM_FALSE;
		} else {
			unset ($Exite);
			unset ($Contador);
			unset ($Nombre_Tabla);
			$SQL = "SELECT ";
			$SQL .= $NombreCampo;
			$SQL .= " FROM ";
			$SQL .= $NombreTabla;
			if ($Condicion_Is == 1){
				$SQL .= " ";
				$SQL .= $Condicion;
				unset ($Condicion_Is);
				unset ($Condicion);
			}
			$Resultado = mysql_query ("$SQL");
			while ($QuItems = mysql_fetch_row ($Resultado)){
				if ($Completa == 1){
					if ($Is_CaseS == 1){
						if ($Patron == $QuItems[1]){
							$Founds[] = $QuItems[0];
						}
					} else {
						if (eregi ("^$Patron$", $QuItems[1])){
							$Founds[] = $QuItems[0];
						}
					}
				} else {
					if ($Is_CaseS == 1){
						if (ereg ("$Patron", $QuItems[1])){
							$Founds[] = $QuItems[0];
						}
					} else {
        		if (eregi ("$Patron", $QuItems[1])){
         			$Founds[] = $QuItems[0];
						}
					}
				}
			}
			unset ($Is_CaseS);
			unset ($Resultado);
			unset ($SQL);
			return $Founds;
		}
	}
}?>