0

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 !

3
Contributors
3
Replies
4
Views
5 Years
Discussion Span
Last Post by diafol
0

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.

0

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.

Edited by somedude3488

0

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)?

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.