J
J
John Freeman2015-05-14 22:25:05
PHP
John Freeman, 2015-05-14 22:25:05

Import from CSV to MySQL?

Taxi, see there is a code:

$file_name = './uploads/IMPORT-TEXT.csv';
$columns = "`domain_name`,`source_category`,`competitor_relevance`,`organic_traffic`,`alexa_rank`";
if ( ($handle_o = fopen($file_name, "r") ) !== FALSE ) {
while ( ($data_o = fgetcsv($handle_o, 1000, ";")) !== FALSE) {
$insertValues = array();
foreach( $data_o as $v ) {
$insertValues[]="'".addslashes(trim($v))."'";
}
$values=implode(',',$insertValues);
$sql = "INSERT INTO `domain` ( $columns ) VALUES ( $values )";
mysql_query($sql) or die('SQL ERROR:'.mysql_error());
}
}
fclose($handle_o);

everything works fine in the database adds 9000 rows, but I have a question: the IMPORT-TEXT.csv
file looks like this:
site.net;site.com organic;20.34;584
site.com;site.com organic;15.55;1538
site.com;site.com organic;15.44;3053
site.com;site.com organic;14.71;160
site.com;site.com organic;11.21;111

how to add +1 value to each line in the file, which will then be written to the database
; I want to take the value from this code:
$domain_check = 'domain_name'; // сюда как то нужно записать первое слово из каждой строки, тоесть имя домена
$alexa = simplexml_load_file("http://data.alexa.com/data?cli=10&dat=snbamz&url=$domain_check");
$globalrank = $alexa->SD[1]->POPULARITY["TEXT"]; // то что нужно добавить к каждой строчке файла при записи в БД

help me decide!
Thanks in advance!

Answer the question

In order to leave comments, you need to log in

3 answer(s)
Z
Zakharov Alexander, 2015-05-14
@AsviS

Would it be ok to push into the $values ​​array?

$values=implode(',',$insertValues);
$domain_check = 'domain_name'; // сюда как то нужно записать первое слово из каждой строки, тоесть имя домена
$alexa = simplexml_load_file("http://data.alexa.com/data?cli=10&dat=snbamz&url=$domain_check");
$globalrank = $alexa->SD[1]->POPULARITY["TEXT"]; // то что нужно добавить к каждой строчке файла при записи в БД

array_push($values, $globalrank) // <- сюда

$sql = "INSERT INTO `domain` ( $columns ) VALUES ( $values )";

A
Andrey Mokhov, 2015-05-14
@mokhovcom

why are you reinventing the wheel?
there are ready-made solutions

A
Artyom Karetnikov, 2015-05-15
@art_karetnikov

I'm shy to ask: what's stopping you from just adding it to the database later?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question