m2e Posted November 17, 2010 Share Posted November 17, 2010 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, Quote Link to comment https://forums.phpfreaks.com/topic/218991-load-data-infile-not-working-in-script/ Share on other sites More sharing options...
mikosiko Posted November 17, 2010 Share Posted November 17, 2010 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."; Quote Link to comment https://forums.phpfreaks.com/topic/218991-load-data-infile-not-working-in-script/#findComment-1135702 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.