values is part of mysql insert syntax.
let's forget about the csv and look at sql syntax. let's say we have a 2 field contacts table "firstname" "lastname". to insert a row into a database, my sql query would look something like this:
Code:
INSERT INTO contacts VALUES('John','Smith')
since there is only 2 fields, we dont' have to identify field names. the following sql would do the same thing:
Code:
INSERT INTO contacts (firstname,lastname) VALUES('John','Smith')
this time we are specifying what fields we are inserting into. if there were more than just firstname lastname, and we were only inserting those 2 fields, we would have to specify field names before values.
in your '$sql=' line, you are just creating a query string that you are inserting into mysql. $fcontents is an array, and if you just echo $fcontents, it will return
Array. remember, we are just creating a STRING to send to mysql.
for example, if you did what you show in your #2 question, your sql statement would look like this:
Code:
INSERT INTO contacts VALUES(Array)
now i'm probably going backwards here, but what explode does is takes a string, and creates an array using the argument supplied as a seperator. 1 line of csv may look like this:
$arr=explode(","$line) would be the same as this:
PHP Code:
<?
$arr[0] = "john";
$arr[1] = "smith";
?>
you can see that it takes what is in between each comma and creates a new element of the array.
sooo, the big question is why do i explode it if i just turn right around and impode it right? well, in sql syntax, you need to seperate each value in between single quotes. look at my first example above, both names have single quotes around it.
since your csv file does not have single quotes around the names, your script creates an array, then cleverly impodes the array, except it doesn't just add back in the comma that was there before, it adds in the single quotes too.
implode("','", $arr);
it's difficult to see exactly what is there, but the first argument is a single quote, comma, single quote. so for element 1,
john, it will make it
'john',
did that make sense?