0
$lines = file('array2.txt');
    foreach($lines as $line)
    {
        mysql_query("INSERT INTO test (Tweet, Date) VALUES ('$line', '22')");
    } 

Hi All,

I am trying to use the code above to use the file() function to add lines from a text file into an array, so I can then place it in a mysql database. The file I am using contains a 100 lines, but using the function above I end up with over 116,000. Does anyone known how what is causing this? Thank you in advance.

user2694239
  • 11
  • 1
  • 2
  • 1
    *PSA:* The `mysql_*` functions are [deprecated in PHP 5.5](http://php.net/manual/en/faq.databases.php#faq.databases.mysql.deprecated). It is not recommended for writing new code as it will prevent you from upgrading in the future. Instead, use either [MySQLi](http://php.net/manual/en/book.mysqli.php) or [PDO](http://php.net/manual/en/book.pdo.php) and [be a better PHP Developer](http://jason.pureconcepts.net/2012/08/better-php-developer/). – Jason McCreary Aug 26 '13 at 13:14
  • 3
    Show a few lines of the file data. – floriank Aug 26 '13 at 13:14
  • 1
    any chance that you file might contain empty lines? – Anda Iancu Aug 26 '13 at 13:15
  • #PowerdbyFord #Ford #Highboy http://t.co/pMqX RT @_LiftedTrucks_: #Ford http://t.co/cnrr `RT @_LiftedTrucks_: #Ford http://t.co/cnrr RT @_LiftedTrucks_: #Ford http://t.co/cnrr ‚Äö√Ñ√∫@_LiftedTrucks_: #Ford http://t.co/iYUIAZp7hb‚ "@_LiftedTrucks_: #Ford http://t.co/F4FRMbMqC4" I want this @baile` This is a few lines from the file. – user2694239 Aug 26 '13 at 13:18
  • I don't think your problem has anything to do with this piece of code. Post more of your code to give us context. –  Aug 26 '13 at 13:19
  • 1
    Please put array2.txt on PasteBin... – nl-x Aug 26 '13 at 13:20
  • By the way, you need to escape your $line parameter: `$line = mysql_real_escape_string($line);` Otherwise you will run into problems (injection) when any tweets contain a `'`. – nl-x Aug 26 '13 at 13:22

2 Answers2

1

Maybe try this way. You will read till file end line.

$file = fopen("file.txt", "r");
while (!feof($file)) {
    $line = fgets($file);
    // Code where you make DB INSERT
}
fclose($file);

For me this works, and I don't get any other records, only those 10 which i wrote in.

Arturs
  • 1,258
  • 5
  • 21
  • 28
0

try this:

<?php
$lines = file('array2.txt');
foreach($lines as $line)
{
  if(!empty($line)){
    $values[]= "('".mysql_real_escape_string($line)."', '22')";
  }
}
$query = "INSERT INTO test (Tweet, Date) VALUES ".implode(",",$values);
$result = mysql_query($query) or die(mysql_error());
?>

Note: I suggest to use mysqli or PDO because mysql function depreciated.

  • Hi there, I just tried your code, and I get the following error "MySQL server has gone away" – user2694239 Aug 26 '13 at 13:25
  • `echo $query;` before `$result` and see how the query printed –  Aug 26 '13 at 13:26
  • @user2694239 Sounds like you've exceeded the [`max_allowed_packet`](http://stackoverflow.com/questions/8062496/how-to-change-max-allowed-packet-size) setting though that can be adjusted. – tadman Aug 26 '13 at 14:18
  • 2
    Without `mysql_real_escape_string` this answer presumes a lot about the nature of the data being imported. – tadman Aug 26 '13 at 14:18