Mysql
 SQL >> Database >  >> RDS >> Mysql

Script PHP legge CSV ed esegue query SQL

fgetcsv PHP gestisce parsing CSV e mysql_query esegue statement. Codice completo:

<?php $fin = fopen('catalog_product_entity.csv','r') or die('cant open file'); $link = mysql_connect('localhost', 'm118', 'pw'); If (!$link) { die ('Could not connect: ' . mysql_error()); } @mysql_select_db('m118') or die ('Unable to select database'); echo "Connection succeeded <br />\n"; while (($data=fgetcsv($fin,1000,","))!==FALSE) { $query = "UPDATE catalog_product_entity SET sku='$data[1]' WHERE entity_id='$data[0]'"; mysql_query($query); echo "Record updated <br />\n"; } fclose($fin); mysql_close(); ?> 

Il loop legge file riga per riga ed esegue UPDATE. In codice moderno sostituisci mysql_ deprecato con PDO/MySQLi e prepared statement.