Import data from TXT file to Mysql


/ Published in: PHP

This piece of code will store each line of a txt file into a mysql database


Copy this code and paste it in your HTML
  1. <?php
  2. $conn = mysql_connect('localhost', 'username', 'password') or die ('Error connecting to mysql');
  3. mysql_select_db('dbname', $conn) or die ('Error connecting to database');
  4. $query = 'insert into titulos (nome) values ("%s");';
  5.  
  6. $lines = file('filmes.txt');//your filename
  7. for($i=0; $i < count($lines); $i++){
  8. mysql_query(sprintf($query, mysql_real_escape_string($lines[$i]))) or die(mysql_error() . '<br><br>' . '<b>Query:</b> ' . $query);
  9. }
  10. ?>

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.