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');
?>
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());
}
before your execute your insert query, you should run this:
$conn->query("SET NAMES 'utf8'");
Related
I'm trying to send Hebrew content through to show up on phpmyadmin. English letters go through perfectly, but Hebrew gives me something like this: חן דו×ק.
phpmyadmin collation is set on utf8_unicode_ci (Also tried utf8_general_ci). How can I solve it?
This is my code:
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<?php
header('Content-Type: text/html; charset=utf-8');
$servername = "//";
$username = "//";
$password = "//";
$dbname = "//";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "INSERT INTO nigunim (name, time, day)
VALUES ('בדיקה', 'בדיקה', 'בדיקה')";
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
?>
Finally fixed by creating a new table and then adding mysql_set_charset("UTF8", $conn); along with making sure collation is set to utf8_general_ci.
This question already has answers here:
UTF-8 all the way through
(13 answers)
Closed 6 years ago.
i've set the meta and check mysql appears to be on utf 8 already, what seems to be the issue here?
HTML
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
PHP DB connection
$DB_NAME = 'ssl';
$DB_HOST = 'localhost';
$DB_USER = 'dbuser';
$DB_PASS = 'dbpass';
$mysqli = new mysqli($DB_HOST, $DB_USER, $DB_PASS, $DB_NAME);
// $mysqli->character_set_name();
mysqli_set_charset($mysqli,"utf8");
if (mysqli_connect_errno())
{
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
Querying for output
$query = $mysqli->query("select {$translate_to} as lang from locale_cn where {$translate_from} = '$phrase' ");
$row = mysqli_fetch_array($query);
Display
<h1><?php echo translate("In-Kind Donation"); ?></h1>
tried setting header('Content-Type: text/html; charset=UTF-8') as well but the result is still the same
Edit: add in translate function info
function translate($phrase) {
if(!isset($_SESSION))
{
session_start();
}
require 'db/mysql.php';
$_GET['lang'] = $_SESSION['lang'];
$lang_session = $_GET["lang"];
if($lang_session == 'CN') {
$translate_from = 'EN';
$translate_to = 'CN';
}
else {
$translate_from = 'CN';
$translate_to = "EN";
}
$query = $mysqli->query("select {$translate_to} as lang from locale_cn where {$translate_from} = '$phrase' ");
$row = mysqli_fetch_array($query);
if(isset($row['lang']) && $row['lang'] !== ""){
return $row['lang'];
}
else{
return $phrase;
}
}
Some Chinese characters need 4 bytes for encoding. MySQL's CHARACTER SET uf8 stops at the 3-byte encodings. Change to utf8mb4
mysqli_set_charset($mysqli,"utf8");
mysqli_set_charset($mysqli,"utf8mb4");
Your <meta ... charset=UTF-8" /> tag is correct as written.
(This also applies to Emoji.)
You may also need to set mb_internal_encoding("UTF-8");
This is about retriving the data in form of CSV from Mysql Table : -
Code , I tried :-
<?php
// mysql database connection details
$host = "localhost";
$username = "root";
$password = "hello";
$dbname = "mysql2csv";
// open connection to mysql database
$connection = mysqli_connect($host, $username, $password, $dbname) or die("Connection Error " . mysqli_error($connection));
// fetch mysql table rows
$sql = "select * from tbl_books";
$result = mysqli_query($connection, $sql) or die("Selection Error " . mysqli_error($connection));
$fp = fopen('books.csv', 'w');
while($row = mysqli_fetch_assoc($result))
{
fputcsv($fp, $row);
}
fclose($fp);
//close the db connection
mysqli_close($connection);
?>
Errors Obtained...
04:12:27.093 The character encoding of the HTML document was not declared. The document will render with garbled text in some browser configurations if the document contains characters from outside the US-ASCII range. The character encoding of the page must be declared in the document or in the transfer protocol.1 mysql2csv.php.
your help will be appreciated ...
Add those lines to your html header
<meta content="text/html;charset=utf-8" http-equiv="Content-Type">
<meta content="utf-8" http-equiv="encoding">
Edit:
If you are using PHP file:
header('Content-Type: text/html; charset=utf-8');
I did all changes as the answer instructed in
this post
in order to be able to print hebrew strings coming from the database but didnt work.
this is my php code:
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
<?php
header('Content-Type: text/html; charset=utf-8');
$servername = "127.0.0.1";
$username = "root";
$password = "";
$dbname = "dbName";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
mysql_query("SET NAMES 'utf8'");
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT * FROM users";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "id: " . $row["id"]. " - Name: " . $row["name"]. " - Score: " . $row["score"]. "<br>";
}
} else {
echo "0 results";
}
$conn->close();
?>
</body>
</html>
every thing works accept that insted of the hebrew strings i only see question marks (???).
any idea why??
Set the Browser's Character Settings to Hebrew.
FireFox:Tools=>Options=>Content=>Advanced=>Fallback Character Encoding = Hebrew
Google Chrome: Menu=>Settings=>Show Advanced Settings=>Language and input settings
UPDATE:
When you save the text first recode the text.
Try using GNU Recode?
$text = mysqli_real_escape_string(recode_string(characterSet,$text));
Where characterSet complies with RFC-1345, e.g. 'latin1'
Valid Character sets: http://www.faqs.org/rfcs/rfc1345.html
I set utf8 for my PHP code:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<?php
header("Content-Type: text/html; charset=UTF-8");
if (isset($_REQUEST['op'])){
$op = $_REQUEST['op'];
} else {
echo "Invalid op";
exit;
}
and also for database connection:
function db_connect(){
$con = mysqli_connect("localhost", "root", "", "ex");
mysqli_set_charset($con, 'utf8');
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
return $con;
}
and I saved my records as utf8 characters in my database and set collation as utf8_general and use SET NAMES utf8; in SQL.
but I see this unicode in the browser instead of my real records in db!
what can I do to see my real records as persian letters on the browser?
THANK YOU FOR ANSWERING SOON