Inserting data in sql db

I have a snippet of code which opens up a webpage with fopen and im grabbing the content from the page. I have 2 questions to this mystery:
1)How do I filter out the content I need. Do I use a switch operation to achieve that?
2)After I get the content, how do insert it into a db?

This is my code so far:
$file = fopen(“http://www.mysite.com/index.php/misc/aff/10","r”);

while(! feof($file))
{
$titles[]=fgets($file);
$titles= str_replace(":affid2:",$_SERVER[‘QUERY_STRING’], $titles);

  while(list($index,$name) = each($titles))
    {
    
        echo $name;
    
    }

}

fclose($file);

Your help is greatly appreciated!