简体   繁体   中英

uploading csv to database table using php

I'm quite new to this, so hopefully I'm not way off base here:

I have a database named BT Database, with a table in it called cases_cases. I want to be able to automatically upload a .csv file into it using a php script. I made a php script and a test csv file, put them in together on the desktop of the XUbuntu machine that's hosting the database.

I'm testing them by opening terminal, navigating to desktop, then using php -e (testimport.php) to run the script. I'm getting no errors, just a blank line, so it seems to be running okay, but when I go into the table, the data from the csv hasn't been uploaded. can anyone advise as to what I'm doing wrong here? my php script is below:

<?php
//connect to the database
    $connect = mysql_connect("localhost","user","password")
    or die("Could not connect.")
;
    mysql_select_db("BT Database",$connect);

//get the csv file 
    $handle = fopen("import.csv","r")
    or die("Couldn't find or open csv file")
;

//set up the query to load data into the table, then run it.
    $loadcases = "LOAD DATA INFILE 'import.csv'
                  INTO TABLE cases_cases
                  FIELDS TERMINATED BY ''
                  OPTIONALLY ENCLOSED BY '\"'
                  LINES TERMINATED BY ',,,\\r\\n'
                  IGNORE 1 LINES
                  (name, @dummy, case_specific_notes, type, initiation_date, @dummy,)"
    or die("error loading file.")
;
    mysql_query($loadcases)
;
?>

EDIT 1: Following everyone's good advice, I removed the comma, upgraded my script to mysqli, and called the actual mysql errors in my script. This showed a few previously unseen issues which were easily fixed.

However, now I'm getting the same issue as before, namely that it runs and gives no errors, but nothing shows up in the database table. The new script is below. Any ideas what might be going wrong?

<?php

$mysqli  =  new mysqli("localhost","xampp","bt4521","BT Database");
/* check connection */
if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}

$sql = "LOAD DATA INFILE 'sfimport/sfdumptest.csv' INTO TABLE cases_cases
        FIELDS TERMINATED BY ','
        LINES TERMINATED BY '\\r\\n'
        IGNORE 1 LINES
        (name, @dummy, case_specific_notes, type, initiation_date, @dummy, @dummy)
";

//Try to execute query (not stmt) and catch mysqli error from engine and php error
if (!($stmt = $mysqli->query($sql))) {
    echo "\nQuery execute failed: ERRNO: (" . $mysqli->errno . ") " . $mysqli->error;
};
?>

Seeing you may not be getting notifications for comments left under your question or if you've read them or not, am submitting the following in the answers area instead.

The trailing comma in @dummy,) shouldn't be there.

Using die(mysql_error($connect)) would have thrown you a syntax error, rather than using a generic echo'd text.


Sidenote: Sure hoping that wasn't a bad paste/typo.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM