I need to fetch content from a file that is located at a URL say "http://example.com/sample.html?user=."
This file will contain data in the following format:-
Item =Name|Unit=1 QTY|Market=INDORE|Price=5000|UpdatedDateTime=Jun 27 2012 8:149PM
After this line a <br> tag is used.
How do I store the content into database. What I actually wish to do is insert or update this content (such as Name, Indore, 5000, Jun 27 2012 8:14 PM) to their respective fields' name.
I am stuck up here. Any help would be appreciated.
I thought of using php's f-read mechanism but am not aware of how to pause after various symbols such as =, |, <br> .
Please help me !

Recommended Answers

All 3 Replies

I have also tried using 'LOAD DATA INFILE' for MYSQL.The only problem that I face now is. I am not able to remove field names from the rows that are being added at the table of database.

<?php
$string = file_get_contents("http://www.mysite.com/conte.php?id=diun", "r");
$myFile = "/content.txt";
$fh = fopen($myFile, 'w') or die("Could not open: " . mysql_error());
fwrite($fh, $string);
fclose($fh);

$sql = mysql_connect("localhost", "root", "");
if (!$sql) {
    die("Could not connect: " . mysql_error());
}
mysql_select_db("my_new_db");
$result = mysql_query("LOAD DATA INFILE '$myFile'" ." INTO TABLE test FIELDS TERMINATED BY '|'");
if (!$result) {
    die("Could not load. " . mysql_error());
}
?>

Please make constructive suggestions and help me at this stage.

Something basic I just wrote:

<?php

$data = file_get_contents('url here');

$lines = explode( '<br />',$data );
foreach( $lines as $line ) {
    $line = explode( '|',$line );
    $datum = array();
    foreach( $line as $part ) {
        list( $key,$value ) = explode( '=',$part,2 );
        $datum[$key] = $value;
    }
    //insert query here using $datum['Item'], $datum['Unit'], etc.
}

?>

Might of overlooked something, but that should get you in the right direction.

Member Avatar for diafol

Is it really a <br /> in the file or a newline like \n ?
Could you give a sample of the content of the file (a few lines)?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.