-1

I need to add usernames from Facebook to database and these names have UTF-8 characters like (ą,č,ę,ė,į,š,ų,ū). When I add It to database for example letter š looks like Å¡

In myPhpAdmin I found table setting to set encoding UTF-8, I did that but the same problem. I added mysql_set_charset('utf8'); to my code, but this wont helped too. Code now looks like:

<?php 
    $time = $_POST['time'];
    $username = $_POST['userName'];

    session_start();
    $name = $_SESSION['vardas']; 
    $times = gmdate('H:m:s', $time);

    mysql_set_charset('utf8');
    $mysqli = new mysqli("localhost","my_db","pass","my_db");
if ($stmt = $mysqli->prepare("INSERT into eurokos (time, userName) VALUE (?,?) ")) {

$stmt->bind_param('is', $time, $name);
  // $stmt->bind_param("s", $username);

   $stmt->execute();

   if ($stmt->error != '') {
       echo ' error:'.$stmt->error;
   } else {
       echo 'success';
   }
   $stmt->close();
} else {
   echo 'error:'.$mysqli->error;
}

Also this php file converted to UTF-8 encoding, but wont helped.

When I use this code echo "Name: " . $user_profile['name']; Facebook correctly printing username with normal letters in UTF-8. So problem is with inserting usernames to database.

This I use to send username variable from fb.php to db.php (maybe here I need to use any encoding?)

$name = $user_profile['name'];
session_start();
$_SESSION['vardas'] = $name;

Could you help me? Thanks.

  • possible duplicate of [utf 8 - PHP and MySQLi UTF8](http://stackoverflow.com/questions/10331883/utf-8-php-and-mysqli-utf8) – deceze May 10 '13 at 14:24
  • `mysql_set_charset` is applicable if you're using the *mysql* extension; you are using *mysqli* and have to use the corresponding mysqli way of setting the connection charset. – deceze May 10 '13 at 14:26
  • I got error: `Fatal error: Call to a member function set_charset() on a non-object...` when use this: `$mysqli->set_charset("utf8");` –  May 10 '13 at 15:41

1 Answers1

1

This solved my problem:

if (!$mysqli->set_charset("utf8")) {
    printf("Error loading character set utf8: %s\n", $mysqli->error);
} else {
    printf("Current character set: %s\n", $mysqli->character_set_name());
}