62

I'm trying to get a ".csv" file onto an SQL database with phpMyAdmin. However, whenever I import it, I get the error: Invalid column count in CSV input on line 1. I've spent all day playing around with different options to try and get it to work but with no avail. There are exactly 47 columns in my .csv file. I have created 47 columns in my SQL table. The names however aren't exactly the same as the ones in the file. Whenever I import, it keeps giving me that error. Any help would be greatly appreciated! ~Carpetfizz One thing I thought might be causing the problem was that the first column isn't named anything in my excel document. Could this be causing an issue?

EDIT 12:30AM: phpMyAdmin is already the latest version available, via (apt-get install phpmyadmin) (phpmyadmin is already latest version)

Space in A1

Here is the .csv file, if that helps.

10
  • 1
    the error guide you completely. it says on line 1 you have more columns than you created in your mysql table.
    – Amir
    Commented Apr 27, 2013 at 3:29
  • what is your code for doing so, possibly that too, try giving name to first column and then import
    – mukund
    Commented Apr 27, 2013 at 3:57
  • @Amir thanks for pointing that out. In the spreadsheet (which I haven't created), the column numbers skip from 47 to 89, and they are highlighted in blue, any thoughts on what that means? Here is a screen of it: i.imgur.com/IJmTZGO.png
    – Carpetfizz
    Commented Apr 27, 2013 at 4:40
  • When using substr_count() on the commas in your pastebin csv, I show 87 commas, not the 46 that would be in your 47 columns. - phpfiddle.org/main/code/7ui-wc9
    – Sean
    Commented Apr 27, 2013 at 5:02
  • Looking at your screen image of the table (i.imgur.com/IJmTZGO.png), if it is jumping from 47 to 89, there is most likely values in 48-88, but the columns have been colapsed. Try expanding them all again, and you will see the extra columns in each row.
    – Sean
    Commented Apr 27, 2013 at 5:10

14 Answers 14

108

If your DB table already exists and you do NOT want to include all the table's columns in your CSV file, then when you run PHP Admin Import, you'll need fill in the Column Names field in the Format-Specific Options for CSV - Shown here at the bottom of the following screenshot.

In summary:

  • Choose a CSV file
  • Set the Format to CSV
  • Fill in the Column Names field with the names of the columns in your CSV
  • If your CSV file has the column names listed in row 1, set "Skip this number of queries (for SQL) or lines (for other formats), starting from the first one" to 1

enter image description here

5
  • 7
    should mark as correct answer. You need to mention the column names.
    – Ans
    Commented Jan 17, 2017 at 11:45
  • 3
    Best solution for me if you have a different number of columns in the .CSV file. Commented Jun 27, 2017 at 10:20
  • 3
    Thank you! All my searching and this is the only right answer I found. This should be marked as correct answer
    – BitBug
    Commented Dec 30, 2017 at 3:03
  • Yes, input Column names it's working well
    – Bang Andre
    Commented Oct 25, 2022 at 2:01
  • Just a hint that MS Excel in some languages (Swedish for example) will by default use semicolon instead of colon for separating columns in a csv, so the field "columns separated with" in above image will also need modification. Commented Sep 23, 2023 at 8:20
45

Fixed! I basically just selected "Import" without even making a table myself. phpMyAdmin created the table for me, with all the right column names, from the original document.

3
  • 13
    Also, I ran into the same problem you had. I fixed it just as you did but, you left out an important detail. When you click "Import", make sure a table in the database is NOT selected. The database name SHOULD be selected instead.
    – kgui
    Commented Jan 4, 2016 at 18:15
  • 2
    @Crt: your comment should be an answer, if not the accepted one! Commented Jun 2, 2018 at 11:32
  • And if the table has the same name then you can click on that table name, go to operations, copy table, and just append a number to make it different then drop the original table.
    – Eoin
    Commented Aug 7, 2018 at 11:43
15

I got the same error when importing a .csv file using phpMyAdmin.

Solution to my problem was that my computer saved the .csv file with ; (semi-colon) as delimiter instead of , (commas).

In the Format-Specific Options you can however chose "columns separated:" and select ; instead of , (comma).

In order to see what your computer stores the file in, open the .csv file in an text editor.

2
  • Excellent suggestion! Opening my .csv file in a text editor showed that my file had 12 additional columns, which was triggering the Invalid column count error.
    – JimB814
    Commented Aug 31, 2018 at 20:13
  • Thanks! I've actually combined all the answers: skip number of queries, add the columns names and changed the "Column separator" value to ; (semi-colon). Worked great!
    – ArtFranco
    Commented Feb 25, 2021 at 14:15
10

This is actually pretty simple to fix, I originally wrote about the fix ~10 years ago over here https://ataiva.com/phpmyadmin-invalid-field-count-in-csv-input-on-line-1/

What you want to do is change "Fields terminated by" from ";" to "," and then make sure that the "Use LOCAL keyword" is selected.

enter image description here

2
  • 1
    THIS is a super underrated answer. Fixed my problem. Thank you!! :-) Commented Sep 25, 2020 at 9:43
  • "#1148 - The used command is not allowed with this MySQL version", it says (my MySQL version seems to be "5.7.29-32-log").
    – anpami
    Commented Dec 19, 2020 at 13:33
9

You will need to skip first row (where column names are defined) and you will need to check which "character" is separating cells (usually is ,, but in my case was ;)

Bellow is the picture of my import:

enter image description here

1
  • 1
    Thank you. In my case I have need to ignore first column. Commented Feb 22, 2022 at 12:57
4

When facing errors with input files of any type, encoding issues are common.

A simple solution might be to open a new file, copy pasting your CSV text in it, then saving it as a new file.

2

I just had this issue and realized that there were empty columns being treated as columns with values, I saw this by opening my CSV in my text editor. To fix this, I opened my spreadsheet and deleted the columns after my last column, they looked completely empty but they were not. After I did this, the import worked perfectly.

0
2

Had the same problem and did two changes: (a) did not over-write existing data (not ideal if that is your intention but you can run a delete query beforehand), and (b) counted the columns and found that the csv had an empty column so it always pays to go back to your original work even though all 'seems' to look correct.

1

If the table was already created, and you were lazy enough not to specify the columns in the fields names input, then all you have to do is to select the empty columns at right to the file content and delete them.

0

Your solution seems to assume you wish to create an entirely new table.

However if you want to add content to an already existing table, look up the structure of the table and note the number of columns (the id column, if you have one still counts->even though it may be auto increment/unique)

So if you table looks like this id Name Age Sex

make sure your excel table looks like A1 id B1 Name C1 Age D1 Sex

and now they both have 4 columns.

Also right under partial import, beside the skip the number of queries.... increase the number to skip the appropriate line. choosing 1 will skip automatically the first line. For those who may have headers in the excel files

0

I had a similar problem with phpmyAdmin. The column count in the file to be imported matched the columns in the target database table. I tried importing files in both .csv and .ods format to no avail, getting a variety of errors including one arguing that the column count was wrong.

Both the .csv and .ods files were created with LibreOffice 5.204. Based on a bit of experience with import issues in years past, I decided to remake the files with the gnumeric spreadsheet, exporting the .ods in compliance with the "strict" format standard. Voila! No more import problem. While I haven't had time to investigate the issue further, I suspect that something has changed in the internal structure of LibreOffice's file output.

0

The final column of my database (it's column F in the spreadsheet) is not used and therefore empty. When I imported the excel CSV file I got the "column count" error.

This is because excel was only saving the columns I use. A-E

Adding a 0 to the first row in F solved the problem, then I deleted it after upload was successful.

Hope this helps and saves someone else time and loss of hair :)

0

The dumbest thing ever that will fix this error in Microsoft Excel (assuming you actually have everything else right):

Select your data and click "Borders All" in Excel (puts visual borders around your data) before saving the CSV. Sound pointless? I completely agree! However, it will fix this error. I use this trick at least three times a week.

0

I also faced this issue in phpMyAdmin. Using LOAD_DATA instead of LOAD solved the problem.

The columns were:

id,date,description,amount,payment_method 

with id as an AUTO_INCREMENT primary key. Records were like this one:

,2019-01-01,Car insurance for year 2019,321.00,bank

The table encoding was utf8_general_ci and the file was in utf-8 encoded.

LOAD also failed when setting manually a value for column id.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.