<?php
function link_data($data)
{
     $link_data=$_SESSION['link_data'];

     $host='vcrxol.com';
     $hostip='192.185.41.206';
     $pass='vicore1976';
     if(mb_stristr('ytf1',$data))
     {
          $host='yotefio.com';
          $hostip='108.167.161.55';
          $pass='Pulso2020*';
     }
     $hostname=substr($host,0,strpos($host,'.'));
     $username=$hostname.'_'.$data.'full';
     $dataname=$hostname.'_'.$data;

     $link_data=$_SESSION['link_data'];

     if(gettype($link_data)=='object')
     {
          $sql_test = 'show tables';
          $reso_test = mysqli_query($link_data, $sql_test);     
          if($link_data->connect_error)
          {echo '*/*/*/ ERR: '.@mysqli_error($link_data);}
     }

     {
          $link_data=mysqli_connect($host,$user,$pass,$data);
          if(!$link_data)
          {$link_data=mysqli_connect($hostip,$user,$pass,$data);}
     }

     if($link_data)
     {$_SESSION['link_data']=$link_data;}

     if($link_data and !empty($data))
     {$link_data -> select_db($data);}
     return $link_data;

}

function msql_connect($host='',$user='',$pass='',$data='')
{
     $host=strtolower($host);
     $data=strtolower($data);
     date_default_timezone_set('America/Guayaquil');
//      SET lc_time_names = 'es_ES';     
     $ip_server=$_SERVER['SERVER_ADDR'];
     $server=$_SERVER['SERVER_NAME'];
     $server=substr($server,strpos($server,'.')+1);
     $subdom=substr(str_replace($server,'',$_SERVER['SERVER_NAME']),0,-1);
     if(!mb_stristr($server,'.')){$server=$_SERVER['SERVER_NAME']; }
     
     if(empty($data)){$data=$subdom;}
     if(@mb_stristr('ytf1',$data)){$server='yotefio.com';$host='yotefio.com';$pass='Pulso2020*';}
     
     $hostname=substr($server,0,strpos($server,'.'));
     if(mb_stristr($hostname,'.')){$hostname=$server;}
     $username=$hostname.'_'.$data.'full';
     $dataname=$hostname.'_'.$data;
     
     if(empty($host)){$host=$server;}
     if(empty($user)){$user=$username;}
     if(empty($pass)){$pass='vicore1976';}

     if($host=='vcrxol.com') {$hostip='192.185.41.206';}
     if($host=='yotefio.com'){$hostip='108.167.161.55';}

     $link_data=$_SESSION['link_data'];
     $sql_test='select database()';
     
     $ver=substr(phpversion(),0,1);
     if($ver<=5)
     {
          if(gettype($link_data)=='resource')
          {
               $reso_test=mysql_query($sql_test,$link_data);
               if(mysql_result($reso_test,0,'database()')!=$dataname)
               {$link_data=mysql_connect($host,$user,$pass,true);}
          }
          else          
          {$link_data=mysql_connect($host,$user,$pass,true);}
          mysql_select_db($dataname,$link_data);
          mysql_query('set names utf8');
          mysql_query('set lc_time_names="es_EC"');
     }

     if($ver>=7)
     {
//           $sql_test='select * from empr0';
//           $sql_test='select * from empr0 where empr_codigo=';
          if(gettype($link_data)=='object')
          {
               $reso_test=@mysqli_query($link_data,$sql_test);
               if(@msql_result($reso_test,0,'database()')!=$dataname)
               {$link_data=mysqli_connect($host,$user,$pass,$dataname);}
//                else{echo '<br>OLD';}
//                mysqli_select_db($link_data,$dataname);
          }
          else
          {$link_data=mysqli_connect($host,$user,$pass,$dataname);}
          
          $link_data->set_charset("utf8");
          setlocale(LC_TIME, "es_ES");          
     }
     
     $_SESSION['link_data']=$link_data;
     return $link_data;
}

function msql_query($sql,$link)
{
     $ver=substr(phpversion(),0,1);
     if($ver<=5){$reso=mysql_query($sql,$link); $error=mysql_error($link);}
     if($ver>=7){$reso=mysqli_query($link,$sql); $error=mysqli_error($link);}
     
     if(!empty($error)){echo '<msgerror>'.' ERROR:'.$error.' \n '.'SQL : '.$sql.'</msgerror>';}
     return $reso;
}  

function msql_result($reso,$row=0,$col=0)
{
     $val='';
     $ver=substr(phpversion(),0,1);
     if($ver<=5){if(mysql_num_rows($reso)!=0){$val=mysql_result($reso,$row,$col);}}
     if($ver>=7)
     {
          if(mysqli_num_rows($reso)!=0)
          {$reso->data_seek($row); $datarow=$reso->fetch_array(); $val=$datarow[$col];}
          if($col=='type')
          {if(mysqli_num_rows($reso)!=0){mysqli_data_seek($reso,$row); $datarow=mysqli_fetch_array($reso); $val=$datarow[0];}}
     }
     return $val;
}

function msql_data_seek(&$reso,$row=0)
{
     $ver=substr(phpversion(),0,1);
     if($ver<=5){if(mysql_num_rows($reso)!=0){mysql_data_seek($reso,$row);}}
     if($ver>=7){if(mysqli_num_rows($reso)!=0){mysqli_data_seek($reso,$row);}}
     return $reso;
}

function msql_num_fields($reso)
{
     $val='';
     $ver=substr(phpversion(),0,1);
     if($ver<=5){$val=mysql_num_fields($reso);}
     if($ver>=7){$val=mysqli_num_fields($reso);}
     return $val;
}

function msql_affected_rows_rows($reso)
{
     $val='';
     $ver=substr(phpversion(),0,1);
     if($ver<=5){$val=mysql_affected_rows($reso);}
     if($ver>=7){$val=mysqli_affected_rows($reso);}
     return $val;
}

function msql_num_rows($reso)
{
     $val='';
     $ver=substr(phpversion(),0,1);
     if($ver<=5){$val=mysql_num_rows($reso);}
     if($ver>=7){$val=mysqli_num_rows($reso);}
     return $val;
}

function msql_fetch_assoc($reso)
{
     $val='';
     $ver=substr(phpversion(),0,1);
     if($ver<=5){$val=mysql_fetch_assoc($reso);}
     if($ver>=7){$val=mysqli_fetch_assoc($reso);}
     return $val;
}

function msql_field_name($reso,$idx)
{
     $val='';
     $ver=substr(phpversion(),0,1);
     if($ver<=5){$val=mysql_field_name($reso,$idx);}
     if($ver>=7)
     {
          $properties = mysqli_fetch_field_direct($reso, $idx);
          $val=is_object($properties) ? $properties->name : null;
     }
     return $val;
}

function msql_field_type($reso,$idx)
{
     $val='';
     $ver=substr(phpversion(),0,1);
     if($ver<=5){$val=mysql_field_type($reso,$idx);}
     if($ver>=7)
     {
          $table_info = mysqli_fetch_field_direct($reso, $idx);
          $val=$table_info->type;
          if($val==3){$val='int';}
          if($val==10){$val='date';}
          if($val==246){$val='real';}
          if($val==253){$val='string';}
     }
     return $val;
}

function msql_insert_id($link)
{
     $val='';
     $ver=substr(phpversion(),0,1);
     if($ver<=5){$val=mysql_insert_id($link);}
     if($ver>=7){$val=mysqli_insert_id($link);}
     return $val;
}

function msql_errno($link)
{
     $val='';
     $ver=substr(phpversion(),0,1);
     if($ver<=5){$val=mysql_errno($link);}
     if($ver>=7){$val=mysqli_errno($link);}
     return $val;
}

function msql_error($link)
{
     $val='';
     $ver=substr(phpversion(),0,1);
//      $ver=7;
     if($ver<=5){$val=mysql_error($link);}
     if($ver>=7){$val=mysqli_error($link);}
     return $val;
}

?>