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
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.";

 

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.