Daniel
Daniel

Reputation: 3

Not sending cyrillic letters to mysql database with php

In my .html document where the form is i have

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 

in the .php file where i connect to the database i tried with

mysql_set_charset('utf8');   
// and 
// <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

and this:

header('Content-Type: text/html; charset=utf-8'); 

The database rows are with Collation: utf8_unicode_ci and charset utf8 So my issue is that when i send the code from the html form through the php i see this in my database: Ð˜Ð·Ð±ÐµÑ€ÐµÑ Here's the .php document code:

    <?php
// header('Content-Type: text/html; charset=utf-8');
header('Content-Type: text/html; charset=utf8');

// mysql_set_charset('utf8_unicode_ci');  
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "dbname";

$name = $_POST['name'];

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// mysql_set_charset('utf8');
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 

$sql = "INSERT INTO oglasi (Name)
VALUES ('$name')";

if ($conn->query($sql) === TRUE) {
    echo "";
} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}   
$conn->close();
// mysql_set_charset('utf8');
?>

Upvotes: 0

Views: 143

Answers (2)

DevWL
DevWL

Reputation: 18840

PROCEDURAL:

mysqli_set_charset(connection,charset);

$con=mysqli_connect("localhost","my_user","my_password","my_db");
mysqli_set_charset($con,"utf8");

OOP:

$mysqli = new mysqli("localhost", "my_user", "my_password", "test");
/* change character set to utf8 */
if (!$mysqli->set_charset("utf8")) {
    printf("Error loading character set utf8: %s\n", $mysqli->error);
    exit();
} else {
    printf("Current character set: %s\n", $mysqli->character_set_name());
}

Upvotes: 1

Gouda Elalfy
Gouda Elalfy

Reputation: 7023

before your execute your insert query, you should run this:

$conn->query("SET NAMES 'utf8'");

Upvotes: 0

Related Questions