逐字阅读文本文件并相应地在mysql数据库中插入单词

i have a text file def.txt which looks like :-

Type total used free shared buffers cached

Mem: 4039 762 3277 0 59 251

i want to read the file word wise and insert these in mysql database as above format. please help me sort this out.

my current code is :-

$fh = fopen('F:\images\def.txt','r') or die($php_errormsg);

for($i=0;$i<=1;$i++)
 {
  if ($s = fgets($fh,1048576)) 
   {
    $words = preg_split('/\s+/',$s,-1,PREG_SPLIT_NO_EMPTY);
    $imp = implode(" ",$words);

    $con = mysqli_connect("localhost","root","root","test") or die("no connection".mysqli_connect_error());
    $a = "insert into test1 values('$imp')";
    if(mysqli_query($con,$a))
        {
            echo "done"."<br>";
        }
    else
        {
            echo "not done".mysqli_error($con)."<br>";
        }
    mysqli_close($con);

}
echo "<br>";
 }
 fclose($fh) or die($php_errormsg);

Seeing as you only use the first line (is that the correct behaviour?), this should do. It takes the numbers and inserts them one by one.

$fh = fopen('F:\images\def.txt','r')
    or die('Could not open file');

$con = mysqli_connect("localhost","root","root","test")
    or die("Could not access database. Error message: ".mysqli_connect_error());

$matches = array();
$line = fgets($fh);
preg_match_all('/\b[0-9]+\b/',$line,$matches);

foreach ( $matches as $word ) {
    $query = "insert into test1 (column_name) values('".
        mysqli_real_escape_string($con,$word)."')";
    if(mysqli_query($con,$query))
    {
        echo "Inserted $word<br/>";
    }
    else
    {
        echo "Failed to insert $word. Error message: ".mysqli_error($con)."<br/>";
    }
}

EDIT: Added mysqli_real_escape_string for security, although with these numbers it should not be that important. Just good practice.