F
F
First Name2017-06-14 08:29:27
PHP
First Name, 2017-06-14 08:29:27

How to change encoding when importing into mysql from csv file?

Good day to all.
add row to db

$handle = fopen($file, "r");
        while (($line = fgetcsv($handle, 100, ";")) !== FALSE) {

            $query = $db->prepare("INSERT INTO students (name,surname,age,city) VALUES (:name,:surname,:age,:city)");
            $data = array('name' => $line[0], 'surname' => $line[1], 'age' => $line[2], 'city' => $line[3]);
            $query->execute($data);
        }
        fclose($handle);

But if there is Cyrillic in csv, just an empty line is added EIdF3g1AnBY.jpg
. Example of a csv file:
udO6Q2sevAk.jpg
How can I change the encoding of a csv file?
Thank you in advance for your response.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
Roman Mirilaczvili, 2017-06-14
@2ord

Before importing, specify in the code:
There are other ways to import:

  1. LOAD DATA LOCAL INFILE
  2. mysqlimport
  3. HeidiSQL

F
Fortop, 2017-06-14
@Fortop

Not the file encoding, but the connection encoding.
What is the csv encoding?
If utf, then accordingly execute SET NAMES utf8
Before inserting into the database

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question