Question : PHP MYsql only last line of csv file is imported

Hi there I have a CSV file that I am trying to import into a database. All the sql side of things is well and I can import no problem. However it only seems to import the last line of the file. Here is the code I am using.
1:
2:
3:
4:
5:
6:
7:
8:
foreach($arr as $str)
{
	list($name,$price,$Description)=explode(",",$str);
}
$sql="INSERT INTO test1 (name, price, Description) VALUES ('$name', '$price', '$Description')";

$result=mysql_query($sql);


It seems to work fine when I print the output to screen as follows.

1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
$arr=file("conts.csv");

    foreach($arr as $str)
    {
    list($name,$price,$Description)=explode(",",$str);

    echo "<li>".$name;
    echo "<li>".$price;
    echo "<li>".$Description;

    echo "<hr>";
    }


Anyone have ideas about what may be causing my issue?

Answer : PHP MYsql only last line of csv file is imported

This is untested code, and it's kind of minimalist from the perspective of error-handling, but maybe it will provide some guidance on the usual patterns involved in loading a data base with information from a CSV file.

HTH, ~Ray
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
<?php // RAY_csv_to_db.php
error_reporting(E_ALL);
echo "<pre>\n";  //READABILITY FOR var_dump()


// CONNECTION AND SELECTION VARIABLES FOR THE DATABASE
$db_host = "localhost"; // PROBABLY THIS IS OK
$db_name = "??";        // GET THESE FROM YOUR HOSTING COMPANY
$db_user = "??";
$db_word = "??";

// OPEN A CONNECTION TO THE DATA BASE SERVER
// MAN PAGE: http://us2.php.net/manual/en/function.mysql-connect.php
if (!$db_connection = mysql_connect("$db_host", "$db_user", "$db_word"))
{
    $errmsg = mysql_errno() . ' ' . mysql_error();
    echo "<br/>NO DB CONNECTION: ";
    echo "<br/> $errmsg <br/>";
}

// SELECT THE MYSQL DATA BASE
// MAN PAGE: http://us2.php.net/manual/en/function.mysql-select-db.php
if (!$db_sel = mysql_select_db($db_name, $db_connection))
{
    $errmsg = mysql_errno() . ' ' . mysql_error();
    echo "<br/>NO DB SELECTION: ";
    echo "<br/> $errmsg <br/>";
    die('NO DATA BASE');
}
// IF WE GOT THIS FAR WE CAN DO QUERIES



// THE NAMES OF OUR COLUMNS
$query_cols = "name, price, Description";

// TEST DATA URL
$csv = "http://path/to/my.csv";
$fpo = fopen($csv, 'r');
if (!$fpo ) die('CRUMP');

// SET A ROW COUNTER
$counter = 0;

// KEEP TRACK OF ROWS THAT HAVE THE WRONG NUMBER OF FIELDS
$errors = array();

// LOOP THROUGH THE CSV RECORDS PERFORMING CERTAIN TESTS
while (!feof($fpo))
{
    $counter++;

    // GET A RECORD
    $csvdata = fgetcsv($fpo);

    // SKIP OVER EMPTY ROWS
    if (empty($csvdata)) continue;

    // ESCAPE THE INFORMATION FOR USE IN THE QUERY
    foreach ($csvdata as $ptr => $value)
    {
        $csvdata[$ptr] = mysql_real_escape_string($value);
    }

    // SET UP VALUE FIELDS
    $query_data = "'" . implode("', '", $csvdata) . "'";

    // SET UP A QUERY
    $sql = "REPLACE INTO myTable ( $query_cols ) VALUES ( $query_data )";

    // RUN THE QUERY HERE....
    $res = mysql_query($sql);
    
    // IF mysql_query() RETURNS FALSE, GET THE ERROR REASONS
    // MAN PAGE: http://us.php.net/manual/en/function.mysql-error.php
    if (!$res)
    {
        $errmsg = mysql_errno() . ' ' . mysql_error();
        echo "<br/>QUERY FAIL: ";
        echo "<br/>$sql <br/>";
        die($errmsg);
    }

}


// SHOW THE NUMBER OF ROWS PROCESSED
echo "<br/>RECORDS PROCESSED $counter \n";
Random Solutions  
 
programming4us programming4us