2

I am making a script that uses the youtube api to search through videos and store details about some of them in a database. I use javascript to get the data and then store it in a mysql database by using php.

It works fine most of the time but some characters seem to cause syntax error when I assign the data to a javascript array at this line:

var videoList = <?=$videoList; ?>;

It is specifically when I assign it and not when I use it, I have tried resetting the array right after it is assigned and it still causes the error.

One of the strings I found that appears to have caused the error is this one: (I know this line caused the problem before because it started working when I manually deleted it)

Hoppas ni mÃ¥r jätte bra idag â¤ï¸undrar om ni vill att jag ska göra en annan video sÃ¥ fÃ¥r ni jätte Järna kommentera deâ

I use "utf8mb4_unicode_520_ci" in the database and charset="UTF-8" on my website. I am not sure if it is because of charset or some character it can't handle, maybe I need to convert the characters with some function.

Any idea how I can fix it?

Update:

I did like Dan Miller suggested and it didn't work at all.

I also tried what Adrianopolis suggested. I changed the collation of the table and column and already had the charset set in the header. It still cause the problem after changing collation.

3
  • 3
    Wrap it with quotes var videoList = "<?=$videoList; ?>"; Commented May 12, 2017 at 22:50
  • NO, don't wrap it in quotes. Use a proper encoder to build proper JS literals var videoList = <?=json_encode($videoList); ?>; Commented May 12, 2017 at 23:52
  • I use json_encode in php: return json_encode($return_arr); Commented May 13, 2017 at 0:00

1 Answer 1

1

Assuming you are using MB4? Change your cols from utf8mb4_unicode_520_ci to utf8mb4_general_ci and of course be sure you have <meta charset="utf-8"> meta tag in your header. That worked for me for all cases.

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

Comments

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.