This question already has answers here:
UTF-8 all the way through
(13 answers)
Closed 6 years ago.
My problem is that how my query result unreadable for humans.
I get this result in browser:
ID: 5 Reg.date: 2016-02-29 18:57:52 C�si
And the name should be 'Cósi'.
The php is in UTF-8, the database is in utf8-hungarian-ci.
So I do the query and after it I put the results into a $user array, and I echo the first 3 item like: echo "ID: " . $user["userID"] . "Reg.date: " . $user["regdate"] . $user["name"];
I tried header('Content-type: text/html; charset=UTF-8'); but it neither works.
I have a innoDB phpmyadmin database, but the server is my father's computer. A xampp, should I search the problem there?
Here is the all php:
$con = mysqli_connect("127.0.0.1", "kxxx", "csxxx", "bxxx");
$password = "asd";
$username = "carrie#gmail.com";
$statement = mysqli_prepare($con, "SELECT * FROM user WHERE email = ? AND password = ?");
mysqli_stmt_bind_param($statement, "ss", $username, $password);
mysqli_stmt_execute($statement);
mysqli_stmt_store_result($statement);
mysqli_stmt_bind_result($statement, $userID, $reg_date, $name, $email, $password, $phonenumber);
$user = array();
while(mysqli_stmt_fetch($statement)){
$user["userID"] = $userID;
$user["regdate"] = $reg_date;
$user["name"] = $name; / which should be "Cósi"
$user["email"] = $email;
$user["password"] = $password;
$user["phonenumber"] = $phonenumber;
}
echo "ID: " . $user["userID"] . "Reg. dátum: " . $user["regdate"] . $user["name"];
echo json_encode($user, JSON_UNESCAPED_UNICODE);
The json_encode is disappears, if empty [] should displayed, but that's not, only the first 3 item of the array.
So I want the C�si to be Cósi. What should I do? I tried to changed meta tag to charset='utf-8', mysql_query("SET NAMES 'utf8'") and the header thing what I mentioned above, all the columns are utf8-hungarian-ci, all the tables and the database is too.... So maybe the server configurations are bed?
When I insert into the database over a php, in the database the 'Cósi' is displays well, everything saved in the database right.
Thank you guys, I resolved it wtih:
mysqli_set_charset($con, 'utf8mb4');
You need the proper html tags:
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Your Page Title</title>
</head>
<body>
YOUR CONTENT HERE
</body>
</html>
Check mb_convert_encoding in php.
$str = mb_convert_encoding($str, "UTF-8", "utf8-hungarian-ci");
Related
I am getting my urls and titles from a post's content, but the titles no longer seem to be UTF-8 and include some funky characters such as "Â" when I echo the result. Any idea why the correct charset isn't being used? My headers do use the right metadata.
I tried some of the solutions on here, but none seems to work so I thought I'd add my code below - just in case I'm missing something.
$servername = "localhost";
$database = "xxxx";
$username = "xxxxx";
$password = "xxxx";
$conn = mysqli_connect($servername, $username, $password, $database);
$post_id = 228;
$content_post = get_post($post_id);
$content = $content_post->post_content;
$doc = new DOMDocument();
$doc->loadHTML('<?xml encoding="utf-8" ?>' . $content);
$links = $doc->getElementsByTagName('a');
$counter = 0;
foreach ($links as $link){
$href = $link->getAttribute('href');
$avoid = array('.jpg', '.png', '.gif', '.jpeg');
if ($href == str_replace($avoid, '', $href)) {
$title = $link->nodeValue;
$title = html_entity_decode($title, ENT_NOQUOTES, 'UTF-8');
$sql = "INSERT INTO wp_urls_download (title, url) VALUES ('$title', '$href')";
if (mysqli_query($conn, $sql)) {
$counter++;
echo "Entry" . $counter . ": $title" . "<br>";
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
}
}
Updated Echo string - changed this after I initially uploaded the code. I have already tried the solutions in the other posts and was not successful.
Did you try to set the utf8 charset on the connection?
$conn->set_charset('utf8');
For more information: http://php.net/manual/en/mysqli.set-charset.php
It seems that you have "double-encoding". What you expected was
Transverse Abdominis (TVA)
But what you have for the space before the parenthesis is a special space that probably came from Microsoft Word, then got converted to utf8 twice. In hex: A0 -> c2a0 -> c382c2a0.
Yes, the link to "utf8 all the way through" would ultimately provide the fix, but I think you need more help.
The A0 was converted from latin1 to utf8, then treating those bytes as if they were latin1 and repeating the conversion.
The connection provide the client's encoding via mysqli_obj->set_charset('utf8') (or similar).
Then the column in the table should be CHARACTER SET utf8mb4 (or utf8). Verify with SHOW CREATE TABLE. (It is probably latin1 currently.)
HTML should start with <meta charset=UTF-8>.
Trouble with UTF-8 characters; what I see is not what I stored
I have an sql database with sql_latin1_general_cp1_ci_as collation.
the database will contain about 3 language (English,Turkish,Arabic).
At the main page I set the charset to utf-8 as:
header('Content-Type: text/html; charset=UTF-8');
<meta charset="utf-8">
And I use this code in my php page:
$sql_server = "server";
$sql_user = "user";
$sql_password = "password";
$sql_database = "database";
$sql_conn_string = 'Driver={SQL Server};Server='.$sql_server.';Database='.$sql_database.';';
if ($sql_conn = odbc_connect($sql_conn_string, $sql_user, $sql_password)){
echo 'Connected';
$name = $_POST['cust_name'];
if (odbc_exec($sql_conn,"INSERT INTO inv01 (cust) VALUES (N'".$name."')")){
echo 'is inserted';
}
else{
echo 'is not inserted';
}
odbc_close($sql_conn);
}
else{
echo 'Connection Error';
}
When I enter an English words there is no problem
But when I use the other languages the words is inserted as unclear characters
I tried to use iconv but it's not working
$name = iconv("UTF_8","Windows-1252",$name);
//and
$name = iconv("UTF_8","UCS-2LE",$name);
What is the best collation in my case if sql_latin1_general_cp1_ci_as is not, and is there another solution without change the collation
I hope that my question is not repeated because I have read many questions here and I have not found a correct answer.
Thanks.
You can try with this:
Ensure that your 'cust' column is nvarchar().
Encode your html and php files in UTF-8 (I usually use Notepad++ for this step).
Convert input data.
Code (based on your example):
<html>
<head>
<meta http-equiv="X-UA-Compatible" content="IE=edge"/>
<meta charset="utf-8">
<?php
...
$name = $_POST['cust_name'];
$name = iconv('UTF-8', 'UTF-16LE', $name);
$name = bin2hex($name);
$sql = "INSERT INTO inv01 (cust) VALUES (CONVERT(nvarchar(MAX), 0x".$value."))";
...
?>
</head>
<body></body>
</html>
I'm simply trying to store a URI in a MySQL table. When I retrieve it the ":" disappears. So http://www.example.com becomes http//www.example.com I've researched it and there are solutions for ; (semicolons), , (commas), quote marks et etc., but not for colons.
I've tried http \ ://www.xyz.com (no spaces) but that converts it http/://www.example.com I'm using php. Any suggestions?
Here is the code:
<?php
$id = $_GET["id"];
$conn = new mysqli($servername, $username, $password, $dbname); //security parameters not shown
$sql = "SELECT * FROM uri WHERE myURI='" . $id . "'";
$result = $conn->query($sql);
if ($result) {
$row = $result->fetch_assoc();
if ($row) {
$SendTo = $row["SendTo"];
header("Location: " . $SendTo);
}
}
?>
Note: I am not showing insertion code because at this stage I am simply entering the URI manually to create a POC.
Use mysqli_real_escape_string() function to escape slashes and store safely in database
For e.g.
$url = "http://www.example.com";
$safeURL = mysqli_real_escape_string($url);
and then use $safeURL var in SQL to store.
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