0

I'm importing a csv file into a mysql table with php. Now I'm going to put some lines as I have in the csv. csv has no header.

enter image description here

I leave the script of how I created the table in mysql:

CREATE TABLE `Areceber` (
  `Id` int NOT NULL AUTO_INCREMENT,
  `N_utente` varchar(45) DEFAULT NULL,
  `Ano` varchar(10) DEFAULT NULL,
  PRIMARY KEY (`Id`)
) ENGINE=InnoDB AUTO_INCREMENT=0 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

now I will put the html script:

<form method="post" action="conexaoexcel1.php" enctype="multipart/form-data">
  <input type="file" name="file"/>
  <input type="submit" name="submit_file" value="Submit"/>
</form>

and now the page conexaoexcel1.php:

$file = $_FILES["file"]["tmp_name"];
$file_open = fopen($file,"r");
while(($csv = fgetcsv($file_open, 1000, ";")) !== false)
{

    foreach ($csv as $key => $value){   

        $Id = str_getcsv($value[0], ',');       
        var_dump($Id);

    }
}

Now when I var_dump the first column it returns like this:

array(1) { [0]=> string(1) "1" } array(1) { [0]=> string(1) "2" } array(1) { [0]=> string(1) "3" } array(1) { [0]=> string(1) "4" } array(1) { [0]=> string(1) "5" } array(1) { [0]=> string(1) "6" } array(1) { [0]=> string(1) "7" } array(1) { [0]=> string(1) "8" } array(1) { [0]=> string(1) "9" } array(1) { [0]=> string(1) "1" } array(1) { [0]=> string(1) "1" } array(1) { [0]=> string(1) "1" } array(1) { [0]=> string(1) "1" } array(1) { [0]=> string(1) "1" } array(1) { [0]=> string(1) "1" } array(1) { [0]=> string(1) "1" } array(1) { [0]=> string(1) "1" } array(1) { [0]=> string(1) "1" } array(1) { [0]=> string(1) "1" } array(1) { [0]=> string(1) "2" }

Up to the line that has the number nine it returns fine, but when it starts on the line that has the number 10, it only returns the number 1 without the zero and so on. Can you help solve the problem?

6
  • 1
    $value will already contain 10 for that column in that row, your mistake is that you are looking at the first character (or rather, byte) of it only, with $value[0]. And that you are calling str_getcsv at this point, makes no sense to begin with - fgetcsv already parsed the line into an array. Commented Nov 24, 2022 at 11:40
  • why are you doing $value[0] ? That'll return the first character of the string. Commented Nov 24, 2022 at 11:40
  • @CBroe But if I put $Id = $value;, it returns everything in the csv. When I insert into the database, it gives an error, because it tries to insert everything in the first column of the database Commented Nov 24, 2022 at 11:44
  • That's because your foreach loop goes over the values of all columns of the line you just read. If you only want to work with the value of the first column - well then don't loop over all of them, but just access the first one directly. Commented Nov 24, 2022 at 11:48
  • @C Broe I intend to insert the first column of the csv in the ´Id´ column of mysql, the second column of the csv in the N_utente column of mysql and the third column of the csv in the Ano column of mysql. Did you understand? Commented Nov 24, 2022 at 11:54

1 Answer 1

1

Your $csv is already an array of data, and to output first column, use $csv[0] without looping.

$file = $_FILES["file"]["tmp_name"];
$file_open = fopen($file,"r");
while(($csv = fgetcsv($file_open, 1000, ";")) !== false)
{

    $Id = $csv[0];       
    var_dump($Id);
}

Just check if your delimiter is right. If it's comma delimited change line

while(($csv = fgetcsv($file_open, 1000, ";")) !== false)

to

while(($csv = fgetcsv($file_open, 1000, ",")) !== false)

Otherwise it should work.

Sign up to request clarification or add additional context in comments.

2 Comments

Did you check delimiter thing? It will return all data if its wrong delimiter you setted
I was using a comma and had to put the ;. It was just that. Thanks

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.