Jump to content

LOAD DATA INFILE not working in script


m2e

Recommended Posts

Hello there,

 

I have a script which uses the LOAD DATA LOCAL INFILE command see below:

 

<?php

//connect to your database
mysql_connect("localhost", "xxx", "xxx"); //(host, username, password)

//specify database 
mysql_select_db("xxx") or die("Unable to select database"); //select which database we're using

// Build SQL Query  
$query = "LOAD DATA INFILE  '/public_html/admin/files/test-jp-stock.csv' INTO TABLE 'jpaero_stocksearch' FIELDS TERMINATED BY  ',' LINES TERMINATED BY  '\r\n' IGNORE 1 LINES";
     
if(mysql_query($query)){

echo ">> New Stock Data has now been uploaded. Database is now live and searchable.";}
else{

echo "Upload failed. Please contact support.";}

?> 

 

Initially I uploaded the csv file through phpmyadmin and everything worked fine - so I then used the SQL generated inside my script, changing the location to where the file actually is - however now nothing happens at all.

 

Any ideas on whats gone wrong, gratefully received!

 

Thanks,

Link to comment
https://forums.phpfreaks.com/topic/218991-load-data-infile-not-working-in-script/
Share on other sites

did you check that you are using the right path and that you have access to that file?

 

In your position I will modify your code a bit in this way  (partial code)

   /*** File to Load ***/
   $file = '/public_html/admin/files/test-jp-stock.csv';

    if (!is_readable($file)) {
        echo "Error : File  '$file' doesn't exist or is unreadable";
        exit();
    }

   // Build SQL Query  
  $query = "LOAD DATA INFILE  '$file' INTO TABLE jpaero_stocksearch 
                 FIELDS TERMINATED BY  ',' 
                 LINES TERMINATED BY  '\r\n' IGNORE 1 LINES";  // Notice that here you should not use ' around your table name
     
  mysql_query($query) or die("Error in Query : " . $query . "<br />Error Code = " . mysql_error() . "<br /> Upload Failed... Contact Support");

  echo ">> New Stock Data has now been uploaded. Database is now live and searchable.";

 

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.