简体   繁体   中英

Result goes blank when reading from textfile and then to insert in mysql

Here, I want to fetch record from textfile and then insert into mysql database. I am getting result as blank. So, am I going right or wrong with my code?

$local_csvfile_path = $_SERVER['DOCUMENT_ROOT'] . '/test_demo/magento_prd';
$filename = $local_csvfile_path . "/orderitems100081669.txt";

if ($dh = opendir($local_csvfile_path)) {

    $cnt = 0;
    while (($lfile = readdir($dh)) !== false) {

        if ($lfile != "." && $lfile != ".." && $lfile != basename($_SERVER["SCRIPT_FILENAME"])) {
            $file = fopen($filename, "r");


            if ($file) {

                $size = filesize($filename);

                if ($size) {

                    $csvcontent = fread($file, $size);

                    fclose($file);
                    $output = explode("\n", $csvcontent);

                    foreach($output as $line) 
                    {
                        $var = explode('\t', $line, 2);
                        $arr[$var[0]] = $var[1];
                    }

                    print_r($arr);

                    $sql = mysql_query('INSERT INTO tbl_order_details(orderdet_id) VALUES ("'.$arr.'")');

                }
            } else {
                fclose($file);
            }
        }
    }
}

And my text file code:

order_id    orderdet_productsku orderdet_iscase orderdet_qty    item_sold_price
100081669   BS8FTX1                    N               1                 2.99

What happens is that you are saving every line in an array but you are not saving each array element in the database. You could run a query for each line in your foreach, or keep it like that but concatenate the array elements to form a complete INSERT with many values like I did.

foreach($output as $line) 
{
    $var = explode('\t', $line, 2);
    $arr[$var[0]] = $var[1];
}

print_r($arr);

$sql = mysql_query('INSERT INTO tbl_order_details(orderdet_id) VALUES ("'.implode('),(',$arr).'")');

Basically, implode will add ),( between each element. So to form VALUES(5),(6) if the values stored were 5 and 6.

You might want to play with it again as I see you are storing column one as key and column two as value, but do not state what you'd want each to be used for. So this current query will just use the values.

EDIT:

To make things simpler to start with so you can test, use this code instead, it will insert the lines one by one and make it more easy to try out first. But note that running X queries is slower than running just one (but not too much, depends on the numbers).

foreach($output as $line) 
{
    $var = explode('\t', $line, 2);
    $arr[$var[0]] = $var[1];
    $sql = mysql_query('INSERT INTO tbl_order_details(orderdet_id) VALUES ("'.$var[1].'")');
}

print_r($arr);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM