This example illustrates how to read a file. Txt, and insert a record into a database MSSQLSERVER.
mysql would be the same, by simply varying the user to access the database:
<?php //--------------------------- Read data from file ---------------------------- //read from file $myFile = "meteoclimatic.htm"; $fh = fopen($myFile, 'r'); $tamany=filesize($myFile); $theData = fread($fh,$tamany); fclose($fh); //llenO las variables $hora=substr( $theData ,strrpos($theData, "*UPD=") + 16, 5); $any=substr( $theData ,strrpos($theData, "*UPD=") + 11, 4); $mes=substr( $theData ,strrpos($theData, "*UPD=") + 8, 2); $dia=substr( $theData ,strrpos($theData, "*UPD=") + 5 ,2 ); $temperatura=substr( $theData ,strrpos($theData, "*TMP=") + 5, strrpos($theData, "*WND=")-(strrpos($theData, "*TMP=") + 7)); $pluja=substr( $theData ,strrpos($theData, "*DPCP=") + 6, strrpos($theData, "*WRUN=")-(strrpos($theData, "*DPCP=") + 8)); if (trim($pluja)=="") $pluja="0"; $humitat=substr( $theData ,strrpos($theData, "*HUM=") + 5, strrpos($theData, "*SUN=")-(strrpos($theData, "*HUM=") + 7)); $viento=substr( $theData ,strrpos($theData, "*WND=") + 5, strrpos($theData, "*AZI=")-(strrpos($theData, "*WND=") + 7)); $racha=substr( $theData ,strrpos($theData, "*WRUN=") + 6, strrpos($theData, "*MHTM=")-(strrpos($theData, "*WRUN=") + 8)); $maxracha=substr( $theData ,strrpos($theData, "*DGST=") + 6, strrpos($theData, "*DSUN=")-(strrpos($theData, "*DGST=") + 8)); $direc=substr( $theData ,strrpos($theData, "*AZI=") + 5, strrpos($theData, "*BAR=")-(strrpos($theData, "*AZI=") + 7)); $pressio=substr( $theData ,strrpos($theData, "*BAR=") + 5, strrpos($theData, "*HUM=")-(strrpos($theData, "*BAR=") + 7)); $AMDHM=$any . $mes . $dia . $hora ; //--------------------- save to slq server ---------------------------- $servidor="VISTA32"; $usuari="sa"; $password=""; $BBDD="RegistrosEstacion"; $link= mssql_connect($servidor,$usuari,$password); if(!$link || !mssql_select_db($BBDD, $link)) { mssql_close($link); die('Impossible abrir Base de dades!'); } $counter=0; $result=mssql_query("select * from Registros where AMDHM='" . $AMDHM . "'",$link); if ($row=mssql_fetch_array($result)) { mssql_close($link); die('Registro ya existente !!!!'); } $result=mssql_query("select TOP 1 PCP from Registros where DIA='" . $dia . "' AND MES='" . $mes . "' AND ANO='" . $any ."' ORDER BY AMDHM DESC",$link); if ($row=mssql_fetch_array($result)) { mssql_close($link); die('Registro ja existente !!!!'); } // FI PENDENT DE FER // no existe por lo tanto lo inserto !! $result=mssql_query("insert into Registros (AMDHM,DIA,MES,ANO,HORA,TMP,HUM,WND,GUST,AZI,BAR,PCP) values ('" . $AMDHM . "','" . $dia . "','" . $mes . "','" . $any . "','" . $hora . "'," . $temperatura . "," . $humitat . "," . $viento . "," . $racha . ",'" . $direc . "'," . $pressio . "," . $pluja . ")"); echo("insert into Registros (AMDHM,DIA,MES,ANO,HORA,TMP,HUM,WND,GUST,AZI,BAR,PCP) values ('" . $AMDHM . "','" . $dia . "','" . $mes . "','" . $any . "','" . $hora . "'," . $temperatura . "," . $humitat . "," . $viento . "," . $racha . ",'" . $direc . "'," . $pressio . "," . $pluja . ")"); mssql_close($link); ?>