LOAD DATA INFILE woes!

any one able to help me to correct this at all please


function importdue()
{
				
				$importduetimes = $dbh->prepare("LOAD DATA LOCAL INFILE '/tmp/trains.csv' INTO TABLE Trains FIELDS TERMINATED BY ',' IGNORE 1 LINES (Due,Destination,Status,Platform,Details)");
				$importduetimes->execute();
				}

returns


Fatal error: Call to a member function prepare() on a non-object in /var/www/trains/functions.php on line 64

$dbh is not in the scope of importdue. You need to either pass it as a argument or make it a global. The former of course is preferred.

just been reading this should be a global as i’ve done an include before the function to set it as a global

just done GLOBAL $dbh now :slight_smile: getting closer!

thank you very much!


function importdue()
{
				GLOBAL $dbh;
				$sql = "LOAD DATA LOCAL INFILE '/tmp/trains.csv' INSERT INTO TABLE Trains FIELDS TERMINATED BY ',' LINES TERMINATED BY '\\r\
' IGNORE 1 LINES  (Due, Destination, Status, Platform, Details)";
				$importduetimes = $dbh->query($sql);
				$importduetimes->execute();
				
				}

still errors out and shows


Fatal error: Call to a member function execute() on a non-object in /var/www/trains/functions.php on line 67

run var_dump($dbh);exit; right after global and paste the result


object(PDO)#2 (0) { }

The query method returns false on failure. The reason for this is that the query is malformed/incorrect.

http://www.php.net/manual/en/pdo.query.php#refsect1-pdo.query-returnvalues

Use errorCode and errorInfo methods to debug query failure.

http://www.php.net/manual/en/pdo.errorcode.php
http://www.php.net/manual/en/pdo.errorinfo.php

You can also take the query out of the application and run it directly from the command line to see why it might be failing.

fixed :slight_smile:


GLOBAL $dbh;
				$importduetimes = $dbh->prepare("LOAD DATA INFILE '/tmp/trains.csv' INTO TABLE `Trains` FIELDS TERMINATED BY ',' IGNORE 1 LINES;");
				$importduetimes->execute();

That’s concerning that I and everyone else here didn’t see that the first time around, lol. Good job