Posted By

fackz on 08/25/09


Tagged

mysql import file txt text


Versions (?)

Who likes this?

3 people have marked this snippet as a favorite

fackz
vali29
umang_nine


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

  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  

You need to login to post a comment.