csv - Php files charset encoding - fgetcsv?


This is the error i get when i try to import products directly to the Prestashops database using external script

array(2) { [0]=> string(4) "Spol" [1]=> string(1) "?" }

Fatal error: Uncaught You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ')' at line 1

INSERT INTOed_feature_product (id_feature,id_product,id_feature_value)
VALUES (1, 313, )
thrown in ...../Db.php on line 765

So I basically have to check if in a csv file there is a product with an id equal to the one on in database. If there isn't one i have to add it directly to a database. I've done everything right and when there is no croatian signs everything works like a charm. The problem is when the script encounters croatian signs (č,ć,ž,š,đ,Č,Ć,Ž,Š,Đ). It's like it doesn't recognize them.

I.e the firs line of the quote is the thing i var_dumped to the screen. Where there is a question mark sign it should've outputted the "Ž" sign.

It clearly isn't recognized as Ž since it throws errors since i'm using that sing to select all the id_feature_value where value = Ž (in this example). Since i'm including prestashops config.inc.php file into this script charset is set to utf-8 as is the header.

Maybe it's not done properly so where can I check it? If it is what is causing this problem and how can i solve it

P.S. I think the problem is that i'm using fgetcsv to read the whole csv file and it doesn't recognize it's encoding therefore outputting weird signs instead of outputting propper croatian signs. What could be solution for this problem?

P.P.S On request import function:

function CSVToArray() {
    $arr = [];
    if (($fp = fopen('csv/products.csv', 'r')) !== FALSE) {
        $i = 0;
        while (($data = fgetcsv($fp, 10000, ";")) !== FALSE) {
            $arr[$i]['reference'] = $data[0];
            $arr[$i]['naziv'] = utf8_decode($data[1]);
            $arr[$i]['price'] = $data[2];

            if (empty($data[3])) {
                $arr[$i]['quantity'] = 0;
            } else {
                $arr[$i]['quantity'] = $data[3];

            $arr[$i]['kat_naziv'] = array_map('trim', explode(',', utf8_decode($data[4])));
            $arr[$i]['slika'] = $data[5];
            $arr[$i]['spol'] = utf8_decode($data[6]);
    return $arr;



I've really messed this one up usingutf8_decode() where it wasn't needed and that caused all my headaches.

People are also looking for solutions to the problem: php - Retrieve corresponding and non corresponding values from many to many table


Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.